count(*)與count(1)的區別有多大?

來源:互聯網
上載者:User

資料庫表的記錄數為:

SQL> select count(*) from table_name t;

COUNT(*)
----------
      6873

1、使用count(*)的統計結果:

SQL> alter session set nls_language = "American";

Session altered.

SQL> set timing on;
SQL> set autotrace on;
SQL> select a.document_id,count(*) from table_name a group by a.document_id having count(a.document_id) >1;

DOCUMENT_ID   COUNT(*)
----------- ----------
          0         71

Elapsed: 00:00:05.20

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=90 Card=339 Bytes=16
          95)

   1    0   FILTER
   2    1     SORT (GROUP BY) (Cost=90 Card=339 Bytes=1695)
   3    2       TABLE ACCESS (FULL) OF 'table_name' (Cost=78 Card=6828
           Bytes=34140)

 

Statistics
----------------------------------------------------------
          0 recursive calls
          0 db block gets
        837 consistent gets
          0 physical reads
          0 redo size
        230 bytes sent via SQL*Net to client
        242 bytes received via SQL*Net from client
          2 SQL*Net roundtrips to/from client
          1 sorts (memory)
          0 sorts (disk)
          1 rows processed

SQL>
2、使用count(1)的統計結果:

SQL> alter session set nls_language = "American";

Session altered.

SQL> set timing on;
SQL> set autotrace on;
SQL> select a.document_id,count(1) from table_name a group by a.document_id having count(a.document_id) >1;

DOCUMENT_ID   COUNT(1)
----------- ----------
          0         71

Elapsed: 00:00:05.57

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=90 Card=339 Bytes=16
          95)

   1    0   FILTER
   2    1     SORT (GROUP BY) (Cost=90 Card=339 Bytes=1695)
   3    2       TABLE ACCESS (FULL) OF 'table_name' (Cost=78 Card=6828
           Bytes=34140)

 

Statistics
----------------------------------------------------------
          0 recursive calls
          0 db block gets
        837 consistent gets
          0 physical reads
          0 redo size
        230 bytes sent via SQL*Net to client
        242 bytes received via SQL*Net from client
          2 SQL*Net roundtrips to/from client
          1 sorts (memory)
          0 sorts (disk)
          1 rows processed

SQL>

其實:兩個並沒有多大差別!使用count(1)要比count(*)的用時多些!

對資料庫表作分析之後的比較:

3、使用count(*)的結果:

SQL> analyze table table_name compute statistics;

Table analyzed.

Elapsed: 00:00:02.92
SQL> select a.document_id,count(*) from table_name a group by a.document_id having count(a.document_i

DOCUMENT_ID   COUNT(*)
----------- ----------
          0         71

Elapsed: 00:00:05.43

Execution Plan
----------------------------------------------------------
   0      SELECT STATEMENT Optimizer=CHOOSE (Cost=93 Card=341 Bytes=13
          64)

   1    0   FILTER
   2    1     SORT (GROUP BY) (Cost=93 Card=341 Bytes=1364)
   3    2       TABLE ACCESS (FULL) OF 'table_name' (Cost=82 Card=6873
           Bytes=27492)

 

Statistics
----------------------------------------------------------
          0 recursive calls
          0 db block gets
        837 consistent gets
          0 physical reads
          0 redo size
        231 bytes sent via SQL*Net to client
        241 bytes received via SQL*Net from client
          2 SQL*Net roundtrips to/from client
          1 sorts (memory)
          0 sorts (disk)
          1 rows processed

SQL>

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.