SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find out the size of database tables, row count, data size, index growth


Find out the size of database tables, row count, data size, index growth

Author
Message
tinakothari
tinakothari
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 76
I want to create table and all columns. column names are database name, current date, row count, data size, index size.

in this table i want to insert about all the tables above information for one particular database.

I want to use DMV for this

let me know if you have any question.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89469 Visits: 45284
sys.databases
sys.tables
sys.indexes
sys.dm_db_partition_stats

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


tinakothari
tinakothari
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 76
do you have a script for this or can you send me the script please?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89469 Visits: 45284
Not offhand, no. It's not hard though, most of the data you want is in sys.dm_db_partition_stats, you just need to join to the others to get the names.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Revenant
Revenant
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7435 Visits: 4865
This might be a good start.


use MyDB
go

DECLARE @names TABLE
(
id INT IDENTITY(1,1),
name NVARCHAR(100) NULL
)
DECLARE @ROWCOUNT INT
DECLARE @i INT = 1
DECLARE @str nvarchar(100)

INSERT INTO @names(name) SELECT name FROM sys.Tables
SET @ROWCOUNT = @@ROWCOUNT

DECLARE @space TABLE
(
name NVARCHAR(100) NULL,
rows CHAR(11),
reserved NVARCHAR (15),
data NVARCHAR (18),
indexes NVARCHAR (18),
unused NVARCHAR (18)
)
WHILE @i <= @ROWCOUNT
BEGIN
SELECT @str = name FROM @names WHERE id = @i
INSERT INTO @space
EXEC sp_spaceused @str
SET @i += 1
END
SELECT * FROM @space
ORDER BY CONVERT( BIGINT, rows ) DESC
;


tinakothari
tinakothari
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 76
Thanks for the reply.
this is now working out.
can you help me with the script using dmv
It creates table and insert all the tables information about specified database
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88584 Visits: 41130
tinakothari (11/18/2012)
Thanks for the reply.
this is now working out.
can you help me with the script using dmv
It creates table and insert all the tables information about specified database


All of the information you seek is in the system objects that Gail Shaw noted in her post above. If you need the information you state, then you should understand that information, as well. The only way you'll understand all of that information is to read about each of those objects in Books Online and figure this one out on your own. You won't be sorry that you did. Think of it as a "rite of passage" that every DBA should go through. ;-) If you have problems during your attempt, I'm sure that folks would be happy to help you with specific prroblems instead of writing the entire query for you.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Perry Whittle
Perry Whittle
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20386 Visits: 17244
tinakothari (11/18/2012)
I want to create table and all columns. column names are database name, current date, row count, data size, index size.

in this table i want to insert about all the tables above information for one particular database.

I want to use DMV for this

let me know if you have any question.


You could try this


SELECT DB_NAME() AS DatabaseName
, object_name(i.object_id) AS TableName
, ISNULL(i.name, 'HEAP') AS IndexName
, i.index_id AS IndexID
, i.type_desc AS IndexType
, p.partition_number AS PartitionNo
, p.[rows] AS NumRows
, au.type_desc AS InType
, au.total_pages AS NumPages
, au.total_pages * 8 AS TotKBs
, au.used_pages * 8 AS UsedKBs
, au.data_pages * 8 AS DataKBs
FROM sys.indexes i INNER JOIN sys.partitions p
ON i.object_id = p.object_id AND i.index_id = p.index_id
INNER JOIN sys.allocation_units au ON
CASE
WHEN au.[type] in (1,3) THEN p.hobt_id
WHEN au.type = 2 THEN p.partition_id
end = au.container_id
INNER JOIN sys.objects o ON i.object_id = o.object_id
WHERE o.is_ms_shipped <> 1
ORDER BY TableName, i.index_id



-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs" ;-)
mark.the.dba
mark.the.dba
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 435
This is a very handy script, Thank you very much for it. I updated the script to handle tables in different schema's. I was using this today and found I have issues when the tables had different schema's.

--
use YourDatabaseName
go

--SELECT * FROM sys.Tables
--SELECT @@SERVERNAME;
--SELECT DB_NAME() AS DataBaseName;

DECLARE @names TABLE
(
id INT IDENTITY(1, 1)
, name NVARCHAR(100) NULL
);
DECLARE @ROWCOUNT INT;
DECLARE @i INT = 1;
DECLARE @str NVARCHAR(100);

INSERT INTO @names
( name
)
SELECT SCHEMA_NAME(schema_id) + '.' + name
FROM sys.tables;
--INSERT INTO @names(name) SELECT name FROM sys.Tables RebuttalStatusHistory ReviewHistory

SET @ROWCOUNT = @@ROWCOUNT;

DECLARE @space TABLE
(
name NVARCHAR(100) NULL
, rows CHAR(11)
, reserved NVARCHAR(15)
, data NVARCHAR(18)
, indexes NVARCHAR(18)
, unused NVARCHAR(18)
);
WHILE @i <= @ROWCOUNT
BEGIN
SELECT @str = name
FROM @names
WHERE id = @i;
INSERT INTO @space
EXEC sp_spaceused @str;
SET @i += 1;
END;
SELECT *
FROM @space
ORDER BY CONVERT(BIGINT, rows) DESC;
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88584 Visits: 41130
mark.the.dba (6/1/2015)
I updated the script to handle tables in different schema's. I was using this today and found I have issues when the tables had different schema's.


I hope they were all good issues because, if they were bad, you haven't given us a clue as to what the issues were.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search