- Mastering PostgreSQL 9.6
- Hans Jurgen Schonig
- 177字
- 2021-07-09 19:57:24
Investigating performance
Grouping sets are a powerful feature; they help to reduce the number of expensive queries. Internally, PostgreSQL will basically turn to traditional GroupAggregates to make things work. A GroupAggregate node requires sorted data, so be prepared that PostgreSQL might do a lot of temporary sorting:
test=# explain SELECT region, country, avg(production) FROM t_oil WHERE country IN ('USA', 'Canada', 'Iran', 'Oman') GROUP BY GROUPING SETS ( (), region, country);
QUERY PLAN
---------------------------------------------------------------
GroupAggregate (cost=22.58..32.69 rows=34 width=52)
Group Key: region
Group Key: ()
Sort Key: country
Group Key: country
-> Sort (cost=22.58..23.04 rows=184 width=24)
Sort Key: region
-> Seq Scan on t_oil
(cost=0.00..15.66 rows=184 width=24)
Filter: (country = ANY
('{USA,Canada,Iran,Oman}'::text[]))
(9 rows)
Hash aggregates are only supported for normal GROUP BY clauses involving no grouping sets. According to the developer of grouping sets (Atri Shama), whom I talked to shortly before writing this chapter, adding support for hashes is not worth the effort; so it seems PostgreSQL already has an efficient implementation even if the optimizer has fewer choices than it has with normal GROUP BY statements.
推薦閱讀
- Java編程全能詞典
- 樂高機器人:WeDo編程與搭建指南
- 大學計算機基礎:基礎理論篇
- 網(wǎng)上沖浪
- 來吧!帶你玩轉(zhuǎn)Excel VBA
- 21天學通C++
- 21天學通ASP.NET
- Linux:Powerful Server Administration
- PVCBOT機器人控制技術(shù)入門
- 網(wǎng)中之我:何明升網(wǎng)絡社會論稿
- 空間機械臂建模、規(guī)劃與控制
- 水晶石影視動畫精粹:After Effects & Nuke 影視后期合成
- Linux系統(tǒng)下C程序開發(fā)詳解
- 樂高創(chuàng)意機器人教程(中級 上冊 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實踐指導叢書)
- 渲染王3ds Max三維特效動畫技術(shù)