June 2, 2010 at 7:47 pm
to display number of rows in each table for a db?
June 2, 2010 at 8:06 pm
June 2, 2010 at 8:15 pm
SELECT TableName = st.name, [# Rows] = sip.rows
FROM sys.system_internals_partitions sip
JOIN sys.tables st
ON st.object_id = sip.object_id
WHERE st.is_ms_shipped = 0
ORDER BY TableName
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
June 3, 2010 at 2:33 am
Try this
SELECT id, so.name, Rows FROM dbo.sysindexes si
INNER JOIN sys.objects so
ON so.[object_id] = si.ID
WHERE si.indid < 2 and so.Type = 'U'
ORDER BY 2
Hay WayneS let me know if it is not good way.
Nag
------------------------------------------------
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537
June 3, 2010 at 3:18 am
One more:
SELECT Derived.table_name,
row_count = SUM(P.[rows])
FROM sys.tables T
JOIN sys.partitions P
ON P.[object_id] = T.[object_id]
CROSS
APPLY (
SELECT QUOTENAME(SCHEMA_NAME(T.[schema_id])) + N'.' + QUOTENAME(T.name)
) Derived (table_name)
WHERE T.type_desc = N'USER_TABLE'
AND T.is_ms_shipped = 0
AND P.index_id IN (0, 1)
GROUP BY Derived.table_name
ORDER BY row_count DESC;
June 3, 2010 at 3:43 am
One more...
You can execute this in the DB where you want to check
execute sp_MSforeachtable @command1= 'select ''?'' TableName,count(*) NumberOfRecords from ?'
Thanks & Regards,
MC
June 3, 2010 at 6:17 am
only4mithunc (6/3/2010)
One more...You can execute this in the DB where you want to check
execute sp_MSforeachtable @command1= 'select ''?'' TableName,count(*) NumberOfRecords from ?'
You could do that, but it's horribly slow... the sysindexes query is much much quicker!
I ran the following query against an 18 row table (not really called tablenamehere):
select count(*) from tablenamehere
SELECT [rows]
FROM sysindexes
WHERE
id = object_id('tablenamehere')
AND indid IN (0,1)
The first query (select count(*)...) was responsible for 92% of the query cost.
Thomas Rushton
blog: https://thelonedba.wordpress.com
June 3, 2010 at 6:36 am
ThomasRushton (6/3/2010)
only4mithunc (6/3/2010)
One more...You can execute this in the DB where you want to check
execute sp_MSforeachtable @command1= 'select ''?'' TableName,count(*) NumberOfRecords from ?'
You could do that, but it's horribly slow... the sysindexes query is much much quicker!
Performance comes with the (small) cost of accuracy though
June 3, 2010 at 6:38 am
steveb. (6/3/2010)
ThomasRushton (6/3/2010)
only4mithunc (6/3/2010)
One more...You can execute this in the DB where you want to check
execute sp_MSforeachtable @command1= 'select ''?'' TableName,count(*) NumberOfRecords from ?'
You could do that, but it's horribly slow... the sysindexes query is much much quicker!
Performance comes with the (small) cost of accuracy though
Given the original question, though, I think that was a reasonable assumption... 😉
Thomas Rushton
blog: https://thelonedba.wordpress.com
June 3, 2010 at 6:43 am
ThomasRushton (6/3/2010)
steveb. (6/3/2010)
ThomasRushton (6/3/2010)
only4mithunc (6/3/2010)
One more...You can execute this in the DB where you want to check
execute sp_MSforeachtable @command1= 'select ''?'' TableName,count(*) NumberOfRecords from ?'
You could do that, but it's horribly slow... the sysindexes query is much much quicker!
Performance comes with the (small) cost of accuracy though
Given the original question, though, I think that was a reasonable assumption... 😉
well you know what they say about assumptions;-)
June 3, 2010 at 7:03 am
steveb. (6/3/2010)
ThomasRushton (6/3/2010)
steveb. (6/3/2010)
ThomasRushton (6/3/2010)
only4mithunc (6/3/2010)
One more...You can execute this in the DB where you want to check
execute sp_MSforeachtable @command1= 'select ''?'' TableName,count(*) NumberOfRecords from ?'
You could do that, but it's horribly slow... the sysindexes query is much much quicker!
Performance comes with the (small) cost of accuracy though
Given the original question, though, I think that was a reasonable assumption... 😉
well you know what they say about assumptions;-)
That's one to you.
So, to be serious for a moment (I know, it's not normal)... what's the problem with the sysindexes query?
Thomas Rushton
blog: https://thelonedba.wordpress.com
June 3, 2010 at 7:16 am
No real problem - the row count just isn't guaranteed to be absolutely spot on.
Oh and sysindexes is deprecated.
June 3, 2010 at 7:17 am
Two things
1) It's not guaranteed to be correct.
2) sysindexes is a deprecated system view, included only for backward compatability with SQL 2000 and will be removed from a future version.
On SQL 2005/2008, rather query sys.partitions for row counts if you don't want to actually query the table. Still not 100% certain to be 100% accurate 100% of the time, but close.
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
June 3, 2010 at 7:19 am
Sysindexes is deprecated - now that's a good reason.
Thomas Rushton
blog: https://thelonedba.wordpress.com
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply