Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Count is slow on large database

Posted on 2003-12-01
12
Medium Priority
?
331 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 750 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

927 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