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

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.

主站蜘蛛池模板: 千阳县| 庄河市| 禄劝| 广西| 宣武区| 兰西县| 巴中市| 原平市| 五华县| 黔南| 静海县| 舟山市| 防城港市| 河东区| 本溪市| 明星| 山西省| 新泰市| 庆云县| 雷州市| 崇义县| 镇赉县| 丰镇市| 吐鲁番市| 南投县| 尼玛县| 通海县| 岳阳县| 枞阳县| 临夏县| 鹤岗市| 昭通市| 靖安县| 镇远县| 姜堰市| 虞城县| 滦平县| 吉安市| 宝兴县| 新宾| 鞍山市|