Solved

Count is slow on large database

Posted on 2003-12-01
12
313 Views
Last Modified: 2012-05-04

Hi,

As part of helping our users to change their application, we perform an outomatic database reorganisation for tables that have changed. For most this is a relatively quick operation. However we have found that upto a million records takes 'less than a second ' (sorry cannot be more definitive) but a table of 140 million records takes more than 3 minutes. At (less than 1 sec) at worst it should be no  more than 2  minutes.

Any explanations  or ideas to speed up?

The command is a simple:    rec_count = count(*) from table

We could use a select rowcnt from system index tables, but then we are beholden to Microsoft to keep this feature, and it does not work for clustered index tables (or for other databases that we have to sometimes work with)

0
Comment
Question by:deldownunder
12 Comments
 
LVL 8

Accepted Solution

by:
Dishan Fernando earned 250 total points
ID: 9855762
use this...

SET STATISTICS IO ON
SELECT rows FROM sysindexes WHERE id = OBJECT_ID('table') AND indid < 2
SET STATISTICS IO OFF
GO

from..
http://www.databasejournal.com/features/mssql/article.php/1443571
0
 
LVL 34

Expert Comment

by:arbert
ID: 9855765
When you issue the above, what is the query plan?  Table scan?  Clustered Index scan?  Scan of another index.  Usually, if the query plan scans the smallest most unique index, you won't have that problem.  I can issue a count agains 70million rows and I get almost instant results when a non-clustered index is used.
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9860072
What I do for fast counts is used sp_spaceused (actually i use a custom variant of it) but sp_space used is a documented SP so its functionality shouldnt change much.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 34

Expert Comment

by:arbert
ID: 9860535
Selecting a count from sysindexes does not guarantee a valid record count if usage hasn't been maintained by SQL Server...
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9860615
sorry, as arbert says you need to update usage first.   this is wrapped in my custom version but forgot to mention.
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9860621
dbcc updateusage(0) with no_infomsgs
0
 
LVL 34

Expert Comment

by:arbert
ID: 9861410
Of course, the updateusage command can be just as slow as selecting the count from the tables :)
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9865628
Indeed.   I guess im in a fortunate position that the MSDE databases I use this method on are static apart from single daily updates as which time i call updateusage as part of the daily importing process :)
0
 
LVL 34

Expert Comment

by:arbert
ID: 9866604
Yep, I'm sure in most scenarios it would be almost instance.  However, like the data warehouse I'm loading, it takes about 2 hours to run :)
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9866627
Hmm.   so i guess the long and short is that this may or may not help him depending on his specific senario.  :S
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 10021166
Has your question been answered?   If so could you please close it.
0
 

Author Comment

by:deldownunder
ID: 10024019
Hi,
 Thanks for the pointers... Since the default set up is non-clusterd, and we need a close-nuff size (for calculating new size as we re-org the database, then we can use the sysindex count - but if not non clustered, the thigs will take time!

Appreciatethe Help
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question