What is the difference between count(0), count(1).. and count(*) in mySQL/SQL?

后端 未结 9 1859

I was recently asked this question in an interview. I tried this in mySQL, and got the same results(final results). All gave the number of rows in that particular table. Can

相关标签:
9条回答
  • 2020-12-24 02:21

    Now - they should all perform identically.

    In days gone by, though, COUNT(1) (or whatever constant you chose) was sometimes recommended over COUNT(*) because poor query optimisation code would make the database retrieve all of the field data prior to running the count. COUNT(1) was therefore faster, but it shouldn't matter now.

    0 讨论(0)
  • 2020-12-24 02:24

    Since the expression 1 is a constant expression, they should always produce the same result, but the implementations might differ as some RDBMS might check whether 1 IS NULL for every single row in the group. This is still being done by PostgreSQL 11.3 as I have shown in this article.

    I've benchmarked queries on 1M rows doing the two types of count:

    -- Faster
    SELECT count(*) FROM t;
    
    -- 10% slower on PostgreSQL 11.3
    SELECT count(1) FROM t;
    

    One reason why people might use the less intuitive COUNT(1) could be that historically, it was the other way round.

    0 讨论(0)
  • 2020-12-24 02:26

    COUNT(*) will count the number of rows, while COUNT(expression) will count non-null values in expression and COUNT(column) will count all non-null values in column.

    Since both 0 and 1 are non-null values, COUNT(0)=COUNT(1) and they both will be equivalent to the number of rows COUNT(*). It's a different concept, but the result will be the same.

    0 讨论(0)
  • 2020-12-24 02:30

    Let's say we have table with columns

    Table 
    -------
    col_A  col_B
    

    System returns all column (null and non-null) values when we query

    select col_A from Table
    

    System returns column values which are non-null when we query

    select count(col_A) from Table
    

    System returns total rows when we query

    select count(*) from Table
    
    0 讨论(0)
  • 2020-12-24 02:32

    1) count(any integer value) is faster than count(*) ---> gives all counts including null values

    2) count(column_name) omits null

    Ex-->

    column name=> id

    values => 1 1 null null 2 2

    ==> count(0), count(1), count(*) -----> result is 6 only

    ==> count(id) ----> result is 4

    0 讨论(0)
  • 2020-12-24 02:38

    This is an example where you can see the difference on count(<int>,<col>, or *):

    create table test (title char(1),number int)
    
    insert into test values ('A',0)
    insert into test values ('B',1)
    insert into test values ('C',2)
    insert into test values (null,null)
    insert into test values (null,3)
    insert into test values ('D',null)
    
    select count (0) from test       --> 6
    select count (1) from test       --> 6
    select count (title) from test   --> 4 (execludes nulls)
    select count (number) from test  --> 4 (execludes nulls)
    select count (*) from test       --> 6
    

    Performance and memory wise, there is no difference between count(1) and count(*) both count the number of records in the group.

    Check COUNT (Transact-SQL) - Remarks

    0 讨论(0)
提交回复
热议问题