forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commitdb0d67d
committed
Optimize order of GROUP BY keys
When evaluating a query with a multi-column GROUP BY clause using sort,the cost may be heavily dependent on the order in which the keys arecompared when building the groups. Grouping does not imply any ordering,so we're allowed to compare the keys in arbitrary order, and a Hash Aggleverages this. But for Group Agg, we simply compared keys in the orderas specified in the query. This commit explores alternative ordering ofthe keys, trying to find a cheaper one.In principle, we might generate grouping paths for all permutations ofthe keys, and leave the rest to the optimizer. But that might get veryexpensive, so we try to pick only a couple interesting orderings basedon both local and global information.When planning the grouping path, we explore statistics (number ofdistinct values, cost of the comparison function) for the keys andreorder them to minimize comparison costs. Intuitively, it may be betterto perform more expensive comparisons (for complex data types etc.)last, because maybe the cheaper comparisons will be enough. Similarly,the higher the cardinality of a key, the lower the probability we’llneed to compare more keys. The patch generates and costs variousorderings, picking the cheapest ones.The ordering of group keys may interact with other parts of the query,some of which may not be known while planning the grouping. E.g. theremay be an explicit ORDER BY clause, or some other ordering-dependentoperation, higher up in the query, and using the same ordering may allowusing either incremental sort or even eliminate the sort entirely.The patch generates orderings and picks those minimizing the comparisoncost (for various pathkeys), and then adds orderings that might beuseful for operations higher up in the plan (ORDER BY, etc.). Finally,it always keeps the ordering specified in the query, on the assumptionthe user might have additional insights.This introduces a new GUC enable_group_by_reordering, so that theoptimization may be disabled if needed.The original patch was proposed by Teodor Sigaev, and later improved andreworked by Dmitry Dolgov. Reviews by a number of people, including me,Andrey Lepikhov, Claudio Freire, Ibrar Ahmed and Zhihong Yu.Author: Dmitry Dolgov, Teodor Sigaev, Tomas VondraReviewed-by: Tomas Vondra, Andrey Lepikhov, Claudio Freire, Ibrar Ahmed, Zhihong YuDiscussion:https://postgr.es/m/7c79e6a5-8597-74e8-0671-1c39d124c9d6%40sigaev.ruDiscussion:https://postgr.es/m/CA%2Bq6zcW_4o2NC0zutLkOJPsFt80megSpX_dVRo6GK9PC-Jx_Ag%40mail.gmail.com1 parent606948b commitdb0d67d
File tree
24 files changed
+1882
-494
lines changed- contrib/postgres_fdw/expected
- doc/src/sgml
- src
- backend
- optimizer
- path
- plan
- util
- utils
- adt
- misc
- include
- nodes
- optimizer
- utils
- test/regress
- expected
- sql
24 files changed
+1882
-494
lines changedLines changed: 6 additions & 9 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2741 | 2741 |
| |
2742 | 2742 |
| |
2743 | 2743 |
| |
2744 |
| - | |
2745 |
| - | |
2746 |
| - | |
| 2744 | + | |
| 2745 | + | |
| 2746 | + | |
2747 | 2747 |
| |
2748 |
| - | |
2749 |
| - | |
2750 |
| - | |
2751 |
| - | |
2752 |
| - | |
2753 |
| - | |
| 2748 | + | |
| 2749 | + | |
| 2750 | + | |
2754 | 2751 |
| |
2755 | 2752 |
| |
2756 | 2753 |
| |
|
Lines changed: 14 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
4967 | 4967 |
| |
4968 | 4968 |
| |
4969 | 4969 |
| |
| 4970 | + | |
| 4971 | + | |
| 4972 | + | |
| 4973 | + | |
| 4974 | + | |
| 4975 | + | |
| 4976 | + | |
| 4977 | + | |
| 4978 | + | |
| 4979 | + | |
| 4980 | + | |
| 4981 | + | |
| 4982 | + | |
| 4983 | + | |
4970 | 4984 |
| |
4971 | 4985 |
| |
4972 | 4986 |
| |
|
0 commit comments
Comments
(0)