forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commitd3d55ce
committed
Remove useless self-joins
The Self Join Elimination (SJE) feature removes an inner join of a plain tableto itself in the query tree if is proved that the join can be replaced witha scan without impacting the query result. Self join and inner relation arereplaced with the outer in query, equivalence classes, and planner infostructures. Also, inner restrictlist moves to the outer one with removingduplicated clauses. Thus, this optimization reduces the length of the rangetable list (this especially makes sense for partitioned relations), reducesthe number of restriction clauses === selectivity estimations, and potentiallycan improve total planner prediction for the query.The SJE proof is based on innerrel_is_unique machinery.We can remove a self-join when for each outer row: 1. At most one inner row matches the join clause. 2. Each matched inner row must be (physically) the same row as the outer one.In this patch we use the next approach to identify a self-join: 1. Collect all merge-joinable join quals which look like a.x = b.x 2. Add to the list above the baseretrictinfo of the inner table. 3. Check innerrel_is_unique() for the qual list. If it returns false, skip this pair of joining tables. 4. Check uniqueness, proved by the baserestrictinfo clauses. To prove the possibility of self-join elimination inner and outer clauses must have an exact match.The relation replacement procedure is not trivial and it is partly combinedwith the one, used to remove useless left joins. Tests, covering this feature,were added to join.sql. Some regression tests changed due to self-join removallogic.Discussion:https://postgr.es/m/flat/64486b0b-0404-e39e-322d-0801154901f3%40postgrespro.ruAuthor: Andrey Lepikhov, Alexander KuzmenkovReviewed-by: Tom Lane, Robert Haas, Andres Freund, Simon Riggs, Jonathan S. KatzReviewed-by: David Rowley, Thomas Munro, Konstantin Knizhnik, Heikki LinnakangasReviewed-by: Hywel Carver, Laurenz Albe, Ronan Dunklau, vignesh C, Zhihong YuReviewed-by: Greg Stark, Jaime Casanova, Michał Kłeczek, Alena RybakinaReviewed-by: Alexander Korotkov1 parent8f0fd47 commitd3d55ce
File tree
14 files changed
+2457
-68
lines changed- doc/src/sgml
- src
- backend
- optimizer
- path
- plan
- utils/misc
- include/optimizer
- test/regress
- expected
- sql
- tools/pgindent
14 files changed
+2457
-68
lines changedLines changed: 16 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
5306 | 5306 |
| |
5307 | 5307 |
| |
5308 | 5308 |
| |
| 5309 | + | |
| 5310 | + | |
| 5311 | + | |
| 5312 | + | |
| 5313 | + | |
| 5314 | + | |
| 5315 | + | |
| 5316 | + | |
| 5317 | + | |
| 5318 | + | |
| 5319 | + | |
| 5320 | + | |
| 5321 | + | |
| 5322 | + | |
| 5323 | + | |
| 5324 | + | |
5309 | 5325 |
| |
5310 | 5326 |
| |
5311 | 5327 |
| |
|
Lines changed: 39 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3494 | 3494 |
| |
3495 | 3495 |
| |
3496 | 3496 |
| |
| 3497 | + | |
| 3498 | + | |
| 3499 | + | |
| 3500 | + | |
| 3501 | + | |
| 3502 | + | |
| 3503 | + | |
| 3504 | + | |
| 3505 | + | |
| 3506 | + | |
| 3507 | + | |
| 3508 | + | |
| 3509 | + | |
| 3510 | + | |
| 3511 | + | |
| 3512 | + | |
3497 | 3513 |
| |
3498 | 3514 |
| |
3499 | 3515 |
| |
| |||
3549 | 3565 |
| |
3550 | 3566 |
| |
3551 | 3567 |
| |
| 3568 | + | |
3552 | 3569 |
| |
3553 | 3570 |
| |
3554 | 3571 |
| |
| |||
3600 | 3617 |
| |
3601 | 3618 |
| |
3602 | 3619 |
| |
| 3620 | + | |
| 3621 | + | |
| 3622 | + | |
| 3623 | + | |
| 3624 | + | |
| 3625 | + | |
| 3626 | + | |
| 3627 | + | |
| 3628 | + | |
| 3629 | + | |
| 3630 | + | |
| 3631 | + | |
| 3632 | + | |
| 3633 | + | |
| 3634 | + | |
| 3635 | + | |
| 3636 | + | |
| 3637 | + | |
3603 | 3638 |
| |
3604 | 3639 |
| |
3605 | 3640 |
| |
| |||
3642 | 3677 |
| |
3643 | 3678 |
| |
3644 | 3679 |
| |
| 3680 | + | |
| 3681 | + | |
| 3682 | + | |
3645 | 3683 |
| |
| 3684 | + | |
3646 | 3685 |
| |
3647 | 3686 |
| |
3648 | 3687 |
| |
|
0 commit comments
Comments
(0)