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 2000 Forums
 Transact-SQL (2000)
 differnce between count(*) and count(COLUMN1)

Author  Topic 

mehedi
Starting Member

2 Posts

Posted - 2007-02-15 : 05:16:18
Hello Everyone,

1. SELECT count(*) from Table1
2. SELECT count(COLUMN1) from Table1

Is there any performance difference between the above queries?

M A A Mehedi Hasan


SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-15 : 05:20:28
COUNT(*) calculate all records
COUNT(Column1) calculated all records for which column1 is not null


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

mehedi
Starting Member

2 Posts

Posted - 2007-02-15 : 05:25:27
Dear Peter Larsson,
Thanks for the quick response. Anyway I want to know is there any performance difference between these two query?

M A A Mehedi Hasan
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-15 : 05:44:42
Try it. And if you do not like the result, try again.
Add index on the table over Column1 column and try again.
Remove the index and try again and compare the results.

Now add a clustered index and repeat the last process.
Compare the timings again.

Which result did you come to?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-02-15 : 06:34:30
"Anyway I want to know is there any performance difference between these two query?"

Yes, unless COLUMN1 is the only column in the Clustered Index in which case they are probably the same. Even then it might depend on what was cached in memory at the time though.

It might also depend on whether SQL Server was confident that DBCC UPDATEUSAGE was current.

And it will definitely depend on whether you have a WHERE clause, JOINs, or additional columns in the SELECT list, or not.

Kristen
Go to Top of Page
   

- Advertisement -