文章出處
文章列表
count(1)與count(*)比較: 如果你的數據表沒有主鍵,那么count(1)比count(*)快 如果有主鍵的話,那主鍵(聯合主鍵)作為count的條件也比count(*)要快 如果你的表只有一個字段的話那count(*)就是最快的啦 count(*) count(1) 兩者比較。主要還是要count(1)所相對應的數據字段。 如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。 因為count(*),自動會優化指定到那一個字段。所以沒必要去count(?),用count(*),sql會幫你完成優化的 count詳解: count(*)將返回表格中所有存在的行的總數包括值為null的行,然而count(列名)將返回表格中除去null以外的所有行的總數(有默認值的列也會被計入). distinct 列名,得到的結果將是除去值為null和重復數據后的結果 ---------------------------------------------------------------------------------------------------------------- 舉例演示如下: SQL> create table test 2 ( 3 ename varchar2(10), 4 sal number(4) 5 ); 表已創建。 SQL> insert into test values('fxe1',90); 已創建 1 行。 SQL> insert into test(ename) values('fxe2'); 已創建 1 行。 SQL> insert into test(ename) values('fxe3'); 已創建 1 行。 SQL> insert into test(ename) values('fxe4'); 已創建 1 行。 SQL> insert into test values('fxe5',80); 已創建 1 行。 SQL> insert into test values('fxe6',80); 已創建 1 行。 SQL> select * from test; ENAME SAL ---------- ---------- fxe1 90 fxe2 fxe3 fxe4 fxe5 80 fxe6 80 SQL> select count(*) from test; COUNT(*) ---------- 6 SQL> select count(sal) from test; COUNT(SAL) ---------- 3 SQL> select count(distinct sal) from test; COUNT(DISTINCTSAL) ------------------ 2 SQL> select distinct sal from test; SAL ---------- 80 90
- count(1)與count(*)比較:
- 如果你的數據表沒有主鍵,那么count(1)比count(*)快
- 如果有主鍵的話,那主鍵(聯合主鍵)作為count的條件也比count(*)要快
- 如果你的表只有一個字段的話那count(*)就是最快的啦
- count(*) count(1) 兩者比較。主要還是要count(1)所相對應的數據字段。
- 如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。
- 因為count(*),自動會優化指定到那一個字段。所以沒必要去count(?),用count(*),sql會幫你完成優化的
- count詳解:
- count(*)將返回表格中所有存在的行的總數包括值為null的行,然而count(列名)將返回表格中除去null以外的所有行的總數(有默認值的列也會被計入).
- distinct 列名,得到的結果將是除去值為null和重復數據后的結果
- ----------------------------------------------------------------------------------------------------------------
- 舉例演示如下:
- SQL> create table test
- 2 (
- 3 ename varchar2(10),
- 4 sal number(4)
- 5 );
- 表已創建。
- SQL> insert into test values('fxe1',90);
- 已創建 1 行。
- SQL> insert into test(ename) values('fxe2');
- 已創建 1 行。
- SQL> insert into test(ename) values('fxe3');
- 已創建 1 行。
- SQL> insert into test(ename) values('fxe4');
- 已創建 1 行。
- SQL> insert into test values('fxe5',80);
- 已創建 1 行。
- SQL> insert into test values('fxe6',80);
- 已創建 1 行。
- SQL> select * from test;
- ENAME SAL
- ---------- ----------
- fxe1 90
- fxe2
- fxe3
- fxe4
- fxe5 80
- fxe6 80
- SQL> select count(*) from test;
- COUNT(*)
- ----------
- 6
- SQL> select count(sal) from test;
- COUNT(SAL)
- ----------
- 3
- SQL> select count(distinct sal) from test;
- COUNT(DISTINCTSAL)
- ------------------
- 2
- SQL> select distinct sal from test;
- SAL
- ----------
- 80
- 90
-
文章列表
全站熱搜