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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 A simple select count statement takes about 15 min

Author  Topic 

daibaocun
Starting Member

9 Posts

Posted - 2011-01-21 : 10:02:06
1. MSSQL 2000 database has around 20 million records.
2. A simple select count statement takes about 15 minutes, although proper indexing has been created on the table and fields.

how to improve the preformance the simple select count(*) statement execute?

the server specs. for MSSQL 2000 database:
- Intel XEON
- 4-CPU Quad Cores
- 8-GB of physical memory.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-21 : 17:24:56
Do you have a clustered index on the table? We can't trust you when you say you've got proper indexing.

Have you checked blocking?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

namman
Constraint Violating Yak Guru

285 Posts

Posted - 2011-01-21 : 22:41:33
Select count(*) is simple to write code but not simple to execute. Do you have a non-clustered index on that table?
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-01-22 : 03:29:37
Count has to touch all qualifying rows in the table. On 20 million rows that'll take a while, especially if there's locking.
It will chose, if it can, the smallest nonclustered index on the table (not the cluster unless there are no other indexes)

Do you need a rough count of all rows in the table? If so, consider querying sys.partitions rather than counting yourself.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-01-22 : 08:05:11
SQL 2000 or 2008? You posted in the 2008 section, but said 2000.

If 2008, use sys.partitions as Gail said:
SELECT rows FROM sys.partitions WHERE object_id = object_id('TableNameHere');
Go to Top of Page

daibaocun
Starting Member

9 Posts

Posted - 2011-01-22 : 09:58:59
It is SQL server 2000. The topic should be put under "SQL Server 2000" Forums. Sorry for the confusing.

Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-01-22 : 10:18:12
You can do this:
select rowcnt from sysindexes where id = object_id('YourTable') and indid in(0, 1)
as Gail says, consider it a rough count as opposed to an exact count (though often times it will be exact).
Go to Top of Page

daibaocun
Starting Member

9 Posts

Posted - 2011-01-31 : 04:05:40
After create clustered index on the table, it takes about 2 seconds to execute select count statement against 17 million records. thanks.
Go to Top of Page
   

- Advertisement -