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?

  • From books online on msdn :

    count(*)

    Specifies that all rows should be counted to return the total number of rows in a table. COUNT(*) takes no parameters and cannot be used with DISTINCT. COUNT(*) does not require an expression parameter because, by definition, it does not use information about any particular column. COUNT(*) returns the number of rows in a specified table without eliminating duplicates. It counts each row separately, including rows that contain null values.

    And from a post on Oracle (ok it's not sql server but the same logic applies).

    Count(*) returns the total row count

    count(1) returns all rows where 1 is not null >> simplified version it returns all rows.

    So Oracle is optimized to use count(*) instead of count(number) because count(1) wouldn't offer any performance gain. And I can only assume that sql server will use the same logic. I've also tried many different variation of count() but unless the specified column allows nulls the plan is always the same : index scan. With a non-indexed nullable column : clustered index scan.

    You can run this and see for yourself :

    Select count(autoval) from dbo.SysColumns --cis

    Select count(*) from dbo.SysColumns --is

    Select count(1) from dbo.SysColumns --is

    Select count(id) from dbo.SysColumns --is