Count(*) vs Count(Id) in sql server 2005

前端 未结 2 475
心在旅途
心在旅途 2021-02-07 04:25

I use SQL COUNT function to get the total number or rows from a table. Is there any difference between the two following statements?

SELECT COUNT(*)         


        
2条回答
  •  广开言路
    2021-02-07 05:08

    Thilo nailed the difference precisely... COUNT( column_name ) can return a lower number than COUNT( * ) if column_name can be NULL.

    However, if I can take a slightly different angle at answering your question, since you seem to be focusing on performance.

    First, note that issuing SELECT COUNT(*) FROM table; will potentially block writers, and it will also be blocked by other readers/writers unless you have altered the isolation level (knee-jerk tends to be WITH (NOLOCK) but I'm seeing a promising number of people finally starting to believe in RCSI). Which means that while you're reading the data to get your "accurate" count, all these DML requests are piling up, and when you've finally released all of your locks, the floodgates open, a bunch of insert/update/delete activity happens, and there goes your "accurate" count.

    If you need an absolutely transactionally consistent and accurate row count (even if it is only valid for the number of milliseconds it takes to return the number to you), then SELECT COUNT( * ) is your only choice.

    On the other hand, if you are trying to get a 99.9% accurate ballpark, you are much better off with a query like this:

    SELECT row_count = SUM(row_count)
      FROM sys.dm_db_partition_stats
      WHERE [object_id] = OBJECT_ID('dbo.Table')
      AND index_id IN (0,1);
    

    (The SUM is there to account for partitioned tables - if you are not using table partitioning, you can leave it out.)

    This DMV maintains accurate row counts for tables with the exception of rows that are currently participating in transactions - and those very transactions are the ones that will make your SELECT COUNT query wait (and ultimately make it inaccurate before you have time to read it). But otherwise this will lead to a much quicker answer than the query you propose, and no less accurate than using WITH (NOLOCK).

提交回复
热议问题