我感兴趣的是Postgres如何实现多维数据集、汇总集和分组集操作符?
发布于 2015-11-11 08:11:59
该实现基于处理排序数据。您可以看到解释语句的结果:
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)对数据进行排序,然后继续进行聚合。
https://stackoverflow.com/questions/33638443
复制相似问题