What does “select count(1) from table_name” on any database tables mean?

前端 未结 9 584
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 18:38

When we execute select count(*) from table_name it returns the number of rows.

What does count(1) do? What does 1 signify here

相关标签:
9条回答
  • 2020-12-22 19:08

    Depending on who you ask, some people report that executing select count(1) from random_table; runs faster than select count(*) from random_table. Others claim they are exactly the same.

    This link claims that the speed difference between the 2 is due to a FULL TABLE SCAN vs FAST FULL SCAN.

    0 讨论(0)
  • 2020-12-22 19:09

    in oracle i believe these have exactly the same meaning

    0 讨论(0)
  • 2020-12-22 19:12

    Here is a link that will help answer your questions. In short:

    count(*) is the correct way to write it and count(1) is OPTIMIZED TO BE count(*) internally -- since

    a) count the rows where 1 is not null is less efficient than
    b) count the rows

    0 讨论(0)
  • 2020-12-22 19:13

    You can test like this:

    create table test1(
     id number,
     name varchar2(20)
    );
    
    insert into test1 values (1,'abc');
    insert into test1 values (1,'abc');
    
    select * from test1;
    select count(*) from test1;
    select count(1) from test1;
    select count(ALL 1) from test1;
    select count(DISTINCT 1) from test1;
    
    0 讨论(0)
  • 2020-12-22 19:15

    Difference between count(*) and count(1) in oracle?

    count(*) means it will count all records i.e each and every cell BUT

    count(1) means it will add one pseudo column with value 1 and returns count of all records

    0 讨论(0)
  • 2020-12-22 19:23

    There is no difference.

    COUNT(1) is basically just counting a constant value 1 column for each row. As other users here have said, it's the same as COUNT(0) or COUNT(42). Any non-NULL value will suffice.

    http://asktom.oracle.com/pls/asktom/f?p=100:11:2603224624843292::::P11_QUESTION_ID:1156151916789

    The Oracle optimizer did apparently use to have bugs in it, which caused the count to be affected by which column you picked and whether it was in an index, so the COUNT(1) convention came into being.

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