Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding rows count in a table without Select...


Finding rows count in a table without Select...

Author
Message
garyg-755799
garyg-755799
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 83
From my testing of these scripts, the row counts are only accurate if the table has a clustered index.

Gary
Greg Milner
Greg Milner
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 491
I believe the WHERE clause here:
Where indid < 2
means that it's on the clustered index. So wouldn't it alway be accurate regardless of statics, since that's the actual physical layout of the rows?

Let me know if that's not correct.

Thanks.



G. Milner
Greg Milner
Greg Milner
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 491
Hi, Manish.
I turned an email I wrote -- based on your post -- to a colleague (ETL designer) into a blog post on my site here, and cited you. Hope that's OK.

Thanks.



G. Milner
garyg-755799
garyg-755799
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 83
Regarding the clause "Where indid < 2", if you have a clustered index there will be a row in sysindexes with indid = 1. If you do not have a clustered index, there will be a row in sysindexes = 0.

From my testing, only the tables with a clustered index (indid = 1) are accurate.

Gary
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45058 Visits: 39903
If you need more accurate results, try using DBCC UPDATEUSAGE first. If you haven't done it in awhile, it could take some good bit of time and will take less time the more you use it.

--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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Chris Howarth-536003
Chris Howarth-536003
Old Hand
Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)Old Hand (329 reputation)

Group: General Forum Members
Points: 329 Visits: 1158
DBCC UPDATEUSAGE - doesn't that only have to be run on SQL 2000 or upon upgrade from 2000 -> 2005?

I thought the issue had been corrected in 2005 onwards?

DBCC UPDATEUSAGE BOL quote:

"In SQL Server 2005, these values are always maintained correctly. Databases created on SQL Server 2005 should never experience incorrect counts, however, databases upgraded to SQL Server 2005 may contain invalid counts."

As I mentioned before, using sys.dm_db_partition_stats is the most reliable, future-proof method.

Chris
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45058 Visits: 39903
Chris Howarth-536003 (3/27/2010)
DBCC UPDATEUSAGE - doesn't that only have to be run on SQL 2000 or upon upgrade from 2000 -> 2005?

I thought the issue had been corrected in 2005 onwards?

DBCC UPDATEUSAGE BOL quote:

"In SQL Server 2005, these values are always maintained correctly. Databases created on SQL Server 2005 should never experience incorrect counts, however, databases upgraded to SQL Server 2005 may contain invalid counts."

As I mentioned before, using sys.dm_db_partition_stats is the most reliable, future-proof method.

Chris


Actually, that's exactly correct, Chris. I've been stuck in a 2k world for much too long. Being in a bit of a hurry, all I saw was a reference to sysindexes and the word "inaccurate" and didn't realize folks were talking of 2k5+.

--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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Johnc
Johnc
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 31
DBCC UPDATEUSAGE - doesn't that only have to be run on SQL 2000 or upon upgrade from 2000 -> 2005?


Actually that may provide the explanation. My original post was about inaccurate counts in a 2000 database I had migrated to 2008 and I didn't know about the caveat, so that may well be the answer.

Thanks Jeff, Chris (and All)



Yanko Nikolov
Yanko Nikolov
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 268
Hello
This is the cool example, but you do not need to write own procedure to determine the tables row count. Just try this:

exec SP_MSFOREACHTABLE @command1=N' exec SP_SPACEUSED ''?'' '

Regards
Yankov Yanko
krassimir.kotzev
krassimir.kotzev
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 24
And th equestion again: How recent are your statistics, indexes, etc. ? If you can not tell, better use count(*) to get the numbers. It is slow but accurate.
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