Home Forums SQL Server 7,2000 T-SQL SELECT COUNT(*) or COUNT(1) Performance or Benefits? RE: SELECT COUNT(*) or COUNT(1) Performance or Benefits?

  • It's upto what you are going to achieve. If the first column is NOT NULL, COUNT(*) and COUNT(1) have the same result. If it's NULLable, the result may be different.

    USE northwind

    GO

    SET NOCOUNT ON

    SET ANSI_WARNINGS ON

    select COUNT(*) FROM dbo.Customers

    select COUNT(CustomerID) FROM dbo.Customers

    select COUNT(Region) FROM dbo.Customers

    select COUNT(FAX) FROM dbo.Customers

    Result:

               

    -----------

    91

               

    -----------

    91

               

    -----------

    31

    Warning: Null value is eliminated by an aggregate or other SET operation.

               

    -----------

    69

    Warning: Null value is eliminated by an aggregate or other SET operation.

    Their execution plans are slightly different. The last one uses clustered index scan and others use index scan on my machine.

    select COUNT(*) FROM dbo.Customers

    StmtText                                                                   

    ---------------------------------------------------------------------------

      |--Compute Scalar(DEFINE[Expr1002]=Convert([Expr1003])))

           |--Stream Aggregate(DEFINE[Expr1003]=Count(*)))

                |--Index Scan(OBJECT[Northwind].[dbo].[Customers].[Region]))

    select COUNT(CustomerID) FROM dbo.Customers

    StmtText                                                                   

    ---------------------------------------------------------------------------

      |--Compute Scalar(DEFINE[Expr1002]=Convert([Expr1003])))

           |--Stream Aggregate(DEFINE[Expr1003]=Count(*)))

                |--Index Scan(OBJECT[Northwind].[dbo].[Customers].[Region]))

    select COUNT(Region) FROM dbo.Customers

    StmtText                                                                        

    --------------------------------------------------------------------------------

      |--Compute Scalar(DEFINE[Expr1002]=Convert([Expr1003])))

           |--Stream Aggregate(DEFINE[Expr1003]=COUNT_BIG([Customers].[Region])))

                |--Index Scan(OBJECT[Northwind].[dbo].[Customers].[Region]))

    select COUNT(FAX) FROM dbo.Customers

    StmtText                                                                                   

    -------------------------------------------------------------------------------------------

      |--Compute Scalar(DEFINE[Expr1002]=Convert([Expr1003])))

           |--Stream Aggregate(DEFINE[Expr1003]=COUNT_BIG([Customers].[Fax])))

                |--Clustered Index Scan(OBJECT[Northwind].[dbo].[Customers].[PK_Customers]))