by截取字段 group_深入理解 group by【思考点滴】
網(wǎng)上搜索 group by,結(jié)果一大堆,千篇一律
……
現(xiàn)在從淺入深的講一下 group by的應(yīng)用
1、group by的作用
group by 簡(jiǎn)單來(lái)講,就是分組,配合計(jì)算使用,計(jì)算方法如SUM、count、avg、max、min等…
應(yīng)用舉例 : 從交易表中查詢出今天有交易的商戶賬戶。
2、使用 group by 完成一個(gè)簡(jiǎn)單的應(yīng)用
查詢order_amount表中,是否有指定的賬戶 ?: 使用group by 就能做到 : select account_id from order_amount
group by account_id;
同樣distinct 也能做到 : 使用 distinct 也能做到 : select distinct account_id from
order_amount ;
如上使用 group by 和 distinct 的效率是一樣的,如果是查找是否存在,建議使用 order by + limit 1,掃描到第一條數(shù)據(jù)就完成,效率最高
select
account_id from
order_amount where
account_id =
xxx limit 1,
group by 和 distinct 使用上的差異
舉例1:select count(distinct account_id)
from order_amount ;
舉例2:select count(account_id) from order_amount
group by account_id;
感興趣的話,可以自己運(yùn)行一下。
差異:
group by : 先分組,再計(jì)算
distinct :先去重,再計(jì)算,distinct 只是去重,不太適合條件和計(jì)算類查找
復(fù)雜應(yīng)用,通過(guò)兩個(gè)條件鎖定一條記錄
舉例,獲取一批商戶,符合指定條件的最后一個(gè)訂單詳情(如:12點(diǎn)到15點(diǎn)之間,參與秒殺商戶的最后一個(gè)訂單)
方法1:
1、select * from (select * from order_amount
where xxx yyy zzz) as sel group
by xy
方法2:
2、select * from? order_amount
group by xy
having id = (select max(id) from order_amount
where xxx yyy zzz)
注 distinct 無(wú)法完成本類需求,只能靜靜的看著 group
by 表演了。
方法1分析:涉及子查詢,子查詢空間和效率的問(wèn)題都暴露出來(lái)了。
select * from (select * from order_amount
where xxx yyy zzz) as sel group
by xy
優(yōu)點(diǎn):
可以實(shí)現(xiàn)功能
缺點(diǎn):
1、group by 中的,除了算法使用的字段和group
by 以外的字段,其它字段的值是隨機(jī)的,默認(rèn)獲取的是選擇查詢索引(where或者group
by)的第一條符合分組的記錄填充的。
2、當(dāng)子查詢的結(jié)果非常大的時(shí)候,數(shù)據(jù)庫(kù)服務(wù)器的臨時(shí)表空間會(huì)用完,因此多余的查詢數(shù)據(jù)會(huì)丟失
3、子查詢生成的臨時(shí)表,沒(méi)有索引可用,如果臨時(shí)表數(shù)據(jù)很大,則主select語(yǔ)句的效率也很低
4、子查詢結(jié)果很大的時(shí)候,生成臨時(shí)表的時(shí)間也很長(zhǎng)
如果子查詢的數(shù)據(jù)超過(guò)1G【1G一般是mysql中默認(rèn)的,子查詢配置的表大小,數(shù)量差不多是500萬(wàn)條以上數(shù)據(jù)】,則后面的查詢結(jié)構(gòu)就丟失了。造成隨機(jī)性數(shù)據(jù)丟失的問(wèn)題。
所以一般數(shù)據(jù)量都不會(huì)踩到這個(gè)坑,踩到這個(gè)坑的都不是一般的數(shù)據(jù)量!
方法2分析:
select * from? order_amount
group by xy
having id = (select max(id) from order_amount
where xxx yyy zzz)
優(yōu)點(diǎn):
可以實(shí)現(xiàn)功能,合理的利用了having
語(yǔ)句,查詢結(jié)果集很小,無(wú)臨時(shí)表空間占滿的問(wèn)題
缺點(diǎn):
1、效率偏低。
方法3:
select * from order_amount
where id in (select max(id) from order_amount
where
xxx yyy
zzz
group
by
xy)
優(yōu)點(diǎn):
可以實(shí)現(xiàn)功能,查詢結(jié)果集很小,無(wú)臨時(shí)表空間占滿的問(wèn)題,效率應(yīng)該比網(wǎng)友指路要好很多
缺點(diǎn):
不能說(shuō)沒(méi)有缺點(diǎn),暫時(shí)是最好的選擇。
更復(fù)雜的需求
step 1:先定位出唯一記錄的ID或者索引信息
需求1:12點(diǎn)到15點(diǎn)之間,最后完成,且最后創(chuàng)建的訂單select max(concat(complete_time,create_time)) from order_amount where xxx yyy zzz group by xy
需求2:12點(diǎn)到15點(diǎn)之間,最后完成,且最先創(chuàng)建的訂單:select max(concat(complete_time,2000000000-create_time)) from order_amount where xxx yyy zzz group by xy
需求3:12點(diǎn)到15點(diǎn)之間,最先完成,且最后創(chuàng)建的訂單:select min(concat(complete_time,2000000000-create_time)) from order_amount where xxx yyy zzz group by xy
需求4:12點(diǎn)到15點(diǎn)之間,最先完成,且最先創(chuàng)建的訂單:select min(concat(complete_time,create_time)) from order_amount where xxx yyy zzz group by xy
step 2:通過(guò)如上唯一信息,查詢唯一數(shù)據(jù)
如上只是個(gè)舉例,總的來(lái)講,還是通過(guò) max/min(concat(xxx,yyy,bbb,...)) 等方式完成按需查找,找到符合條件的唯一記錄, 其中?xxx,yyy,bbb 可以是字段,也可以是一種運(yùn)算,如2000000000-create_time,總的原則來(lái)講,就是想通過(guò)max或者min搜索出想要的唯一信息。????????如果覺(jué)得數(shù)據(jù)量不是很大,則可以使用 select * from (select * from order_amount order
by complete_time desc,
create_time asc where
xxx yyy zzz) as sel
group by xy,zz,dd,通過(guò)內(nèi)查詢按要求排序,通過(guò)group by篩選出第一條記錄。
group by 總結(jié)
1、group by 非計(jì)算列,非group by列,如何自行控制?
解決方法:子查詢,子查詢按需進(jìn)行排序
select * from (select * from order_amount order
by complete_time desc,
create_time asc where
xxx yyy zzz) as sel
group by xy
2、group by 也是優(yōu)先使用索引。
3、group by 一次可以完成多個(gè)函數(shù),可以通過(guò)多個(gè)字段進(jìn)行分組
select count(amount) as cnt, SUM(amount)
as total_amount, avg(amount)
as avg_amount, max(id)
as max_id, min(id)
as min_id, xy, za, hs from
order_amount
group by xy,
za,hs
4、同時(shí)可以使用 with rollup再獲取上級(jí)匯總
select count(amount) as cnt,
SUM(amount) as total_amount, avg(amount)
as avg_amount,
max(id) as max_id,
min(id) as min_id, xy, za, hs from
order_amount
group by xy, za,hs with
rollup
5、group by 之后的結(jié)果也可以排序,并非select的條件,且不影響select的結(jié)果。
select count(amount) as cnt,
SUM(amount) as total_amount, avg(amount)
as avg_amount,
max(id) as max_id,
min(id) as min_id, xy, za, hs from
order_amount
group by xy asc, za desc,hs asc
6、使用 group by 的時(shí)候,難免會(huì)用到子查詢,一定要嚴(yán)格審視子查詢結(jié)果的大小和性能
總結(jié)
以上是生活随笔為你收集整理的by截取字段 group_深入理解 group by【思考点滴】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。