官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 龙陵县| 阿鲁科尔沁旗| 松江区| 上蔡县| 南乐县| 襄汾县| 崇阳县| 南皮县| 理塘县| 武山县| 牡丹江市| 双峰县| 威远县| 永平县| 常熟市| 肥城市| 南雄市| 重庆市| 雷波县| 喜德县| 长葛市| 靖江市| 南京市| 共和县| 泸州市| 成武县| 呼伦贝尔市| 栾城县| 宝山区| 西充县| 定州市| 望城县| 大埔区| 清丰县| 二手房| 广元市| 慈溪市| 桃江县| 东港市| 龙州县| 四平市|