Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-07-29 : 04:17:03
|
| Hello allI use a query something like Select Count(*) from SomeTable in my application to get the number of rows in the table. This query takes the most time. Any alternatives to avoid SELECT COUNT(*) ??? |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-07-29 : 04:29:55
|
| instead of * use primary kay Select Count(pk) from SomeTableSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-07-29 : 04:32:25
|
| Using the PK also takes the same amount of time ... |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-29 : 04:34:24
|
| HiTo perform a query like SELECT COUNT(*), SQL Server will use the narrowestnon-clustered index to count the rows. If the table does not have anynon-clustered index, it will have to scan the table.you want a quick numberSELECT rowcnt FROM sysindexes WHERE object_name(id) = 'tablename'AND indid IN (9,1)This number may not be fully accurate, but close enough.-------------------------R.. |
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-07-29 : 04:47:10
|
Thanks you everyone...that helpedquote: Originally posted by rajdaksha HiTo perform a query like SELECT COUNT(*), SQL Server will use the narrowestnon-clustered index to count the rows. If the table does not have anynon-clustered index, it will have to scan the table.you want a quick numberSELECT rowcnt FROM sysindexes WHERE object_name(id) = 'tablename'AND indid IN (9,1)This number may not be fully accurate, but close enough.-------------------------R..
|
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-29 : 04:53:33
|
| HiSP_SPACEUSED <TABLE_NAME>It's return below column valuesNAME ROWS ---Here you can get the total number of recordsRESERVEDDATAINDEX_SIZEUNUSEDif there is no non-clustered in table, there is no better option than to scan all data pages.-------------------------R.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-29 : 07:41:44
|
| HiRows in SP_SPACEUSED is refering the belowSELECT ROWS FROM SYS.PARTITIONS WHERE OBJECT_ID = OBJECT_ID('TABLE_NAME')-------------------------R.. |
 |
|
|
|
|
|