select count(*) from tbname 

select count(1) from tbname 快

解决方案 »

  1.   

    select count(*) 应该是最慢的吧?
      

  2.   

    select count(非空的字段) from tbname;
      

  3.   

    select count(*) from tbname 

    select count(1) from tbname 快
    --从测试计划上来看
    没有索引时.
    SQL> select count(*) from testa;
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   SORT (AGGREGATE)
       2    1     TABLE ACCESS (FULL) OF 'TESTA'
    Statistics
    ----------------------------------------------------------
              0  recursive calls
              4  db block gets
              1  consistent gets
              2  physical reads
              0  redo size
            152  bytes sent via SQL*Net to client
            308  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processedSQL> select count(id) from testa;
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   SORT (AGGREGATE)
       2    1     TABLE ACCESS (FULL) OF 'TESTA'
    Statistics
    ----------------------------------------------------------
              0  recursive calls
              4  db block gets
              1  consistent gets
              2  physical reads
              0  redo size
            170  bytes sent via SQL*Net to client
            308  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processedSQL>
      

  4.   

    http://blog.itpub.net/post/330/1914
      

  5.   

    --有索引时,不管count()里面是用*还是字段,都将使用第一个索引.SQL> select count(*) from fnd_user;
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1)
       1    0   SORT (AGGREGATE)
       2    1     INDEX (FULL SCAN) OF 'FND_USER_U1' (UNIQUE) (Cost=1 Card
              =510)Statistics
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
              1  consistent gets
              0  physical reads
              0  redo size
            170  bytes sent via SQL*Net to client
            308  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processedSQL> select count(user_name) from fnd_user;
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1)
       1    0   SORT (AGGREGATE)
       2    1     INDEX (FULL SCAN) OF 'FND_USER_U1' (UNIQUE) (Cost=1 Card
              =510)Statistics
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
              1  consistent gets
              0  physical reads
              0  redo size
            178  bytes sent via SQL*Net to client
            308  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
              1  rows processedSQL> select count(user_id) from fnd_user;
    Execution Plan
    ----------------------------------------------------------
       0      SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1)
       1    0   SORT (AGGREGATE)
       2    1     INDEX (FULL SCAN) OF 'FND_USER_U1' (UNIQUE) (Cost=1 Card
              =510)Statistics
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
              1  consistent gets
              0  physical reads
              0  redo size
            176  bytes sent via SQL*Net to client
            308  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              1  sorts (memory)
              0  sorts (disk)
              1  rows processedSQL>
      

  6.   

    select count(*) from tbname 

    select count(1) from tbname从执行计划上看,没有什么不同