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

Investigating performance

Grouping sets is 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)

In PostgreSQL, hash aggregates are only supported for normal GROUP BY clauses involving no grouping sets. In PostgreSQL 10.0, the planner already has more options than in PostgreSQL 9.6. Expect grouping sets to be faster in the new version.

主站蜘蛛池模板: 保山市| 沾化县| 新巴尔虎左旗| 通渭县| 白玉县| 中卫市| 潮安县| 福建省| 通榆县| 昂仁县| 沽源县| 山丹县| 宜丰县| 曲水县| 卢湾区| 平利县| 南昌县| 青川县| 武川县| 姜堰市| 百色市| 陇南市| 邯郸市| 邢台市| 鄂伦春自治旗| 湘西| 漠河县| 进贤县| 舟山市| 永济市| 汾阳市| 沙雅县| 察隅县| 信宜市| 常德市| 八宿县| 永定县| 通化县| 河南省| 门头沟区| 兴国县|