When counting number of rows in table it will take awful lot of time a sit has to scan through the table. For example I have a table with rows 18346967 and if I execute Select Count(*) from table1 It will take 32 seconds to give me the result. But if I run select rows from sysindexes where id = OBJECT_ID(table1) and indid < 2 It will take less than one second. However problem you have this with you cant get the group count. You can only use when you are counting all the records.
When i saw this query for the first time here in SQL-Server-Permonce.com under query hints section even i was surprised and when i ran the query on my tables it was flashy and ive added this query in my favourites.
There is only one thing to keep in mind here. While the results are usually correct, there is no guarantee that they always are. The only reliable way is unfortunately SELECT COUNT(*). -- Frank Kalis SQL Server MVP http://www.insidesql.de
The query from the index table is also the same result that will come back in a rowcount from sp_spacesused, and it of coures carries the same warning as Frank mentioned. Chris
Frank, Can u share the instances where this is failing, please quote:Originally posted by FrankKalis There is only one thing to keep in mind here. While the results are usually correct, there is no guarantee that they always are. The only reliable way is unfortunately SELECT COUNT(*). -- Frank Kalis SQL Server MVP http://www.insidesql.de
Failing is not the correct term here. It is usually accurate, but just not guaranteed to. If you experience inconsistencies here, DBCC UPDATEUSAGE will correct them. I think when you perform large INSERT or DELETE operations this number can get out of synch. -- Frank Kalis SQL Server MVP http://www.insidesql.de
True. Do a DBCC UPDATEUSAGE(0) on one of your "heavy" databases and you might be surpried by how much data is out of sync in sysindexes.
Thankx Frank quote:Originally posted by FrankKalis Failing is not the correct term here. It is usually accurate, but just not guaranteed to. If you experience inconsistencies here, DBCC UPDATEUSAGE will correct them. I think when you perform large INSERT or DELETE operations this number can get out of synch. -- Frank Kalis SQL Server MVP http://www.insidesql.de
Hi dineshashanka, I've a table say table in which i did bulk insert and then i ran select rows from sysindexes where id = OBJECT_ID(table1) and indid < 2 it gave me output as 111785, and when i ran EM and checked the properties of that table, even that shows me the same rows 111785. but when i ran select count(*) its giving me output 112209, so i'm facing the real time problem here. now ill run dbcc updateusage and check what happens.
friends i ran dbcc updateusage(0) as said by argyle and then i ran select rows from sysindexes where id = OBJECT_ID(table1) and indid < 2 which is still giving me 111785 but the count is 112209. what should i do to get the correct no. of rows by running the above flash query.
I also faced this problem and solved http://sql-server-performance.com/forum/topic.asp?TOPIC_ID=5583&SearchTerms=sysindexes Madhivanan Failing to plan is Planning to fail
Thanx Madhivanan, Finally i got solution to my q. I was waiting for the reply but no one replied. The link gave me the right solution which i was looking for. Thanx again.[8D] quote:Originally posted by Madhivanan I also faced this problem and solved http://sql-server-performance.com/forum/topic.asp?TOPIC_ID=5583&SearchTerms=sysindexes Madhivanan Failing to plan is Planning to fail
It is also worth to mention to filter counts with user tables and not to count indexes. Please find the syntax at Malcan - Rows counts - getting quickly, comparing databases. You may also find there the join query that gives you the difference of rows per table between two databases. Easy and good to remember