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

后端 未结 13 2563
醉梦人生
醉梦人生 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 05:58

    In all RDBMS, the two ways of counting are equivalent in terms of what result they produce. Regarding performance, I have not observed any performance difference in SQL Server, but it may be worth pointing out that some RDBMS, e.g. PostgreSQL 11, have less optimal implementations for COUNT(1) as they check for the argument expression's nullability as can be seen in this post.

    I've found a 10% performance difference for 1M rows when running:

    -- Faster
    SELECT COUNT(*) FROM t;
    
    -- 10% slower
    SELECT COUNT(1) FROM t;
    

提交回复
热议问题