------------------------------------------------------------------------------------------------------------------
1、我這裏有10個字段。
count(1)和count(*)都是返回一樣的結果!
2、到底誰的速度快一點。
問了很多人了,也網上搜索過一早上,還是沒有確切答案,都是各執一詞!各有各的理!
10W數據,測試是count(*)快一些,但是我以前不知道那裏看過資料,說的是count(1)快!怎麼回事情!?大家給個明確說法!
------------------------------------------------------------------------------------------------------------------
就是因爲count,sum這些聚合函數加上統計group by,遇到null自動略過,所以你用count(1),count(*)結果是很可能不一樣,sum的話
我有個一個表A,作爲測試
select * from a
/*
a_nam a_add
----------- ----------
1 aa
1 bb
NULL kk
2 kk
NULL uu
4 dd
6 yy
6 yy
(8 row(s) affected)
*/
select a_add,count(a_nam) from a
group by a_add
/*
a_add
---------- -----------
aa 1
bb 1
dd 1
kk 1
uu 0
yy 2
(6 row(s) affected)
Warning: Null value is eliminated by an aggregate or other SET operation.
*/
select a_add,count(1) from a
group by a_add
/*
a_add
---------- -----------
aa 1
bb 1
dd 1
kk 2 --有2筆,null認爲存在
uu 1 --爲null,但是也認爲有一筆
yy 2
(6 row(s) affected)
*/
下面2個看看,很有意思,一個是空,一個是沒有資料
select sum(a_nam) from a
where a_add = 'xx'
group by a_add
/*
-----------
(0 row(s) affected)
*/
select sum(a_nam) from a
where a_add = 'xx'
/*
-----------
NULL
(1 row(s) affected)
*/
上面2個是因爲沒有用group by造成的差異,有些程序認爲null是有資料,那麼就要注意啦
所以用聚合函數,一般要加上group by,有出現null的資料,請加上isnull()轉換
FYI