Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 8
TSQL 7
#TSQL 5
SQL Server 4
Performance 4
#DBA 3
DBA 2
Tips and Tricks 2
T-SQL 2
Syndicate 2

Archive · View All
July 2010 3
June 2010 3
May 2010 3
August 2010 2
September 2010 1

dave ballantyne's Blog

SQL Server - Count(*) or Count(1) ? - Which is better?

Jul 27 2010 8:14AM by dave ballantyne   

Every so often on the on-line forums, the question of : Which is better Count(*) or Count(1) ? will occur.  So can it categorically be proved than one is better than the other ?

What we need to do is populate a table and use both count(*) and count(1) and see if we get any performance difference between the two.

Code Snippet
  1. drop table largeTable
  2. go
  3. create table largeTable
  4. (
  5.   id integer not null
  6. )
  7. go
  8. declare @v integer
  9. insert into largeTable (id)
  10. Select top 1000000 ROW_NUMBER() over(order by @v)
  11. from sys.columns a cross join sys.columns b cross join sys.columns c
  12. go
  13.  
  14. select COUNT(*) from largeTable
  15. go 100
  16. select COUNT(1) from largeTable
  17. go 100

After executing the above code count(*) executes in an average of 107 milliseconds and count(1) executes in an average of 108 milliseconds.  So, there is no difference in performance.

What if though we use both count(*) and count(1) in the same statement ?  The average execution sound be in the order of 200 milliseconds, right ?

Code Snippet
  1. select COUNT(*),COUNT(1) from largeTable
  2. go 100

No, we still get the same average execution time, 100ms (ish).  Why is that ?  Two operations at 100ms each should equal a total of 200ms.  The answer to this, as a lot of answers do, lies within the execution plan.

image

Lets look at the properties for the highlighted compute scalar operation.

image

Both of the output columns are derived from the SAME Expression, Expr1006,  which is the result of the stream aggregate.  Looking at the properties of that we see

image

No mention at all of count(1) anywhere.  Indeed, if we look at the query plan of

Code Snippet
  1. select COUNT(1) from largeTable

we still see the count(*) scalar operator used. 

So not only now have we proved that there is no performance difference between the two,  but to the engine, they are the same operation.

Tags: Performance, TSQL, BRH, #DBA, #TSQL, Best Practices, DBA,


dave ballantyne
111 · 1% · 462
4
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

4  Comments  

  • Hi Dave,

    I really liked your article. I totally agree with you. I have written something similar earlier over here: http://blog.sqlauthority.com/2008/02/26/sql-server-select-1-vs-select-an-interesting-observation/

    Kind Regards, Pinal

    commented on Jul 31 2010 6:18AM
    Pinal Dave
    146 · 1% · 326
  • I always thought the battle was between the aggregates COUNT(*) AND SUM(1). As they run pretty much at the same speed, I've always preferred COUNT() as it simply is easier to read, yet I never really looked at what happens under the bonnet. Although they don't reproduce the same result when there are no records returned (COUNT() returns 0, SUM() returns NULL), on sql2k8 they DO appear to share the same execution plan! The "obligatory" Compute Scalar that follows the stream aggregation in fact is a CASE that replaces the result with NULL when the number of rows counted for is 0... nifty ! =P

    commented on Aug 11 2010 6:15AM
    Roby Van Hoye
    218 · 1% · 207
  • Hi Roby, Very interesting...

    doing a select sum(1) from

    also causes a count(*) to be executed for the null test. Very odd , im sure there are reasons , i feel another blog coming on...

    commented on Aug 11 2010 9:33AM
    dave ballantyne
    111 · 1% · 462
  • Geez ! I have been living under another myth ! What if there were 28 columns and varied structure ? Still no differentials ? And I have trusted all thsi "wisdom" for sooo many years.. Rick

    commented on Aug 19 2010 12:46PM
    rwillemain
    657 · 0% · 50

Your Comment


Sign Up or Login to post a comment.

"SQL Server - Count(*) or Count(1) ? - Which is better?" rated 5 out of 5 by 4 readers
SQL Server - Count(*) or Count(1) ? - Which is better? , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]