Oracle 中count(1) 、count(*) 和count(列名) 函數的區別

來源:互聯網
上載者:User

標籤:示範   nbsp   str   number   主鍵   資料   ref   去重   tail   

1)count(1)與count(*)比較:

1、如果你的資料表沒有主鍵,那麼count(1)比count(*)快
2、如果有主鍵的話,那主鍵(聯合主鍵)作為count的條件也比count(*)要快
3、如果你的表只有一個欄位的話那count(*)就是最快的啦
4、count(*) count(1) 兩者比較。主要還是要count(1)所相對應的資料欄位。
5、如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。  因為count(*),自動會最佳化指定到那一個欄位。所以沒必要去count(?),用count(*),sql會幫你完成最佳化的

2)count詳解:

1、count(*)將返回表格中所有存在的行的總數包括值為null的行,然而count(列名)將返回表格中除去null以外的所有行的總數(有預設值的列也會被計入).
2、distinct 列名,得到的結果將是除去值為null和重複資料後的結果


3)舉例示範如下:

 

 1 SQL> create table test 2 ( 3 ename varchar2(10), 4 sal number(4) 5  ); 6  7 表已建立。 8  9  10 SQL> insert into test values(‘fxe1‘,90);11 已建立 1 行。12  13 SQL> insert into test(ename) values(‘fxe2‘);14 已建立 1 行。15 16 SQL> insert into test(ename) values(‘fxe3‘);17 已建立 1 行。18 19 SQL> insert into test(ename) values(‘fxe4‘);20 已建立 1 行。21 22 SQL> insert into test values(‘fxe5‘,80);23 已建立 1 行。24 25 SQL> insert into test values(‘fxe6‘,80);26 已建立 1 行。27 28 29 SQL> select * from test;30 ENAME      SAL31 ---------- ----------32 fxe1        9033 fxe234 fxe335 fxe436 fxe5        8037 fxe6        8038 39 40 SQL> select count(*) from test;  -- count(*):包含NULL,一共6條記錄41 COUNT(*)42 ----------43 6        44 45 SQL> select count(1) from test;  -- count(1):包含NULL,一共6條記錄,和count(*)的結果一樣46 COUNT(1)47 ----------48 6       49 50 SQL> select count(sal) from test;  -- count(列名):不包含NULL,但包含重複值項,一共3條記錄51 COUNT(SAL)52 ----------53 3              54  55 SQL> select count(distinct sal) from test;  -- count(列名):不包含NULL,去重“count(distinct sal)”,一共2條記錄56 COUNT(DISTINCTSAL)57 ------------------58 2              59 60 SQL> select distinct sal from test;61 SAL62 ----------63 8064 90

 

 

轉至:http://blog.csdn.net/szstephenzhou/article/details/8446481

 

Oracle 中count(1) 、count(*) 和count(列名) 函數的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.