Count(*) vs Count(1) - SQL Server

后端 未结 13 2077
醉梦人生
醉梦人生 2020-11-21 05:21

Just wondering if any of you people use Count(1) over Count(*) and if there is a noticeable difference in performance or if this is just a legacy h

13条回答
  •  囚心锁ツ
    2020-11-21 06:17

    SET STATISTICS TIME ON
    
    select count(1) from MyTable (nolock) -- table containing 1 million records. 
    

    SQL Server Execution Times:
    CPU time = 31 ms, elapsed time = 36 ms.

    select count(*) from MyTable (nolock) -- table containing 1 million records. 
    

    SQL Server Execution Times:
    CPU time = 46 ms, elapsed time = 37 ms.

    I've ran this hundreds of times, clearing cache every time.. The results vary from time to time as server load varies, but almost always count(*) has higher cpu time.

提交回复
热议问题