Postgres如何实现CUBE-,ROLLUP-和GROUPING SETS运算符

Mar*_*yer 2 postgresql grouping rollup set cube

我对Postgres如何实现CUBE-,ROLLUP-和GROUPING SETS操作符感兴趣?

Pav*_*ule 7

该实现基于处理排序数据.你可以看到EXPLAIN语句的结果:

 postgres=# EXPLAIN SELECT a, b, sum(c) FROM foo GROUP BY ROLLUP(a,b);
??????????????????????????????????????????????????????????????????????
?                             QUERY PLAN                             ?
??????????????????????????????????????????????????????????????????????
? GroupAggregate  (cost=142.54..166.99 rows=405 width=12)            ?
?   Group Key: a, b                                                  ?
?   Group Key: a                                                     ?
?   Group Key: ()                                                    ?
?   ->  Sort  (cost=142.54..147.64 rows=2040 width=12)               ?
?         Sort Key: a, b                                             ?
?         ->  Seq Scan on foo  (cost=0.00..30.40 rows=2040 width=12) ?
??????????????????????????????????????????????????????????????????????
(7 rows)

postgres=# EXPLAIN SELECT a, b, sum(c) FROM foo GROUP BY CUBE(a,b);
??????????????????????????????????????????????????????????????????????
?                             QUERY PLAN                             ?
??????????????????????????????????????????????????????????????????????
? GroupAggregate  (cost=142.54..302.48 rows=605 width=12)            ?
?   Group Key: a, b                                                  ?
?   Group Key: a                                                     ?
?   Group Key: ()                                                    ?
?   Sort Key: b                                                      ?
?     Group Key: b                                                   ?
?   ->  Sort  (cost=142.54..147.64 rows=2040 width=12)               ?
?         Sort Key: a, b                                             ?
?         ->  Seq Scan on foo  (cost=0.00..30.40 rows=2040 width=12) ?
??????????????????????????????????????????????????????????????????????
(9 rows)
Run Code Online (Sandbox Code Playgroud)

对数据进行排序,然后不断进行汇总.