Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

SELECT COUNT(*) or COUNT(1) Performance or Benefits? Expand / Collapse
Author
Message
Posted Wednesday, May 11, 2005 12:52 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Saturday, February 28, 2009 6:51 AM
Points: 1,489, Visits: 7
Yes, you are right in that the queries return different results. The reasons were also mentioned earlier in this thread and in my article. As noted the reason is that COUNT(expression), where expression might simply be the name of a column, does not count occurences where expression evaluates to null. Since COUNT(*) counts every row, there is no concept of null involved (if there is a row then there is one, no matter how many of the columns on it that are null, or even might be) and therefore the results might differ. From the results we can see that there are 91 rows in the table Northwind.dbo.Customers, of which 60 (91-31) do not have a value (they are null) for column Region and 22 (91-69) do not have a value for column Fax.

You are also correct in that whether a column allows nulls or not might affect which execution plan is chosen. If a column that does not allow nulls is used as the expression, such as COUNT(CustomerId), this has the same meaning as COUNT(*) since all rows should be counted. Therefore the same plans will be used. If it does allow null then there might be rows with null and therefore the expression must be evaluated for each row (here evaluated means that the value of the column must be checked). If there is an index on the column then a scan of that index will be used (as in the COUNT(Region) example query), and if there is not an index then a table scan/clustered index scan will be used (as with the COUNT(Fax) example).

However, to finally get back to why I asked you to elaborate, I think you have misunderstood COUNT(1). The 1 does not indicate the ordinal number of a column, it is simply an expression that will always be true. So COUNT(*) and COUNT(1) will never return different results and will always use the same plan, since they are treated as identical queries by the optimizer.




--
Chris Hedgate http://www.hedgate.net/
Contributor to the Best of SQL Server Central volumes
Articles: http://www.sqlservercentral.com/columnists/chedgate/
Post #181591
Posted Wednesday, May 11, 2005 1:27 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 26, 2014 1:05 PM
Points: 126, Visits: 67

i started using count(1) when count(*) appeared not to return results in a timely manner (on very large table, lots of rows/columns).  if truly implemented the same way by sql server, it must be serendipity i experienced a difference in performance. 

 

that could be due to trying first with count(*), getting the table loaded in memory in the process, then moving on to using count(1).  i think of '1' taking up less memory internally to process, but there's no real foundation for that.

 

Post #181607
Posted Wednesday, May 11, 2005 2:22 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Saturday, February 28, 2009 6:51 AM
Points: 1,489, Visits: 7
Well, it might of course turn out that I went too far when I said that "COUNT(*) and COUNT(1) will never return different results and will always use the same plan, since they are treated as identical queries by the optimizer", since I have no way of knowing that for 100% certainty. (Oh, what I wouldn't do for the SQL Server source code)

That said, I would like more information from your example to be convinced that they were not treated the same in your specific situation. It is very probable that the reason is that the pages were fetched from disk in the first case and directly from cache in the second, just as you speculate.

>i think of '1' taking up less memory internally to process, but there's no real foundation for that.

In what way would it take up less memory than '*'? They are both nothing more than a character in a sql statement that is parsed by the optimizer into an execution plan. The execution plans for COUNT(*) and COUNT(1) have always been equal in the situations that I have encountered, but if they were not in your example (or any other situation) then of course there could be a difference in execution time.




--
Chris Hedgate http://www.hedgate.net/
Contributor to the Best of SQL Server Central volumes
Articles: http://www.sqlservercentral.com/columnists/chedgate/
Post #181622
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse