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

后端 未结 13 2052
醉梦人生
醉梦人生 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:08

    In SQL Server, these statements yield the same plans.

    Contrary to the popular opinion, in Oracle they do too.

    SYS_GUID() in Oracle is quite computation intensive function.

    In my test database, t_even is a table with 1,000,000 rows

    This query:

    SELECT  COUNT(SYS_GUID())
    FROM    t_even
    

    runs for 48 seconds, since the function needs to evaluate each SYS_GUID() returned to make sure it's not a NULL.

    However, this query:

    SELECT  COUNT(*)
    FROM    (
            SELECT  SYS_GUID()
            FROM    t_even
            )
    

    runs for but 2 seconds, since it doen't even try to evaluate SYS_GUID() (despite * being argument to COUNT(*))

提交回复
热议问题