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

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.

主站蜘蛛池模板: 怀化市| 乐清市| 区。| 洪泽县| 德令哈市| 湘乡市| 南开区| 思茅市| 广德县| 榆林市| 广昌县| 石嘴山市| 五寨县| 兴宁市| 无锡市| 全州县| 广河县| 江永县| 华坪县| 厦门市| 五峰| 长兴县| 张家界市| 南和县| 江都市| 卫辉市| 菏泽市| 濮阳县| 亳州市| 郎溪县| 东乡族自治县| 高邑县| 吉木乃县| 北安市| 收藏| 大冶市| 青岛市| 三都| 嘉鱼县| 平潭县| 洮南市|