forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit3d00162
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, skipthis pair of joining tables. 4. Check uniqueness, proved by the baserestrictinfo clauses. To provethe possibility of self-join elimination inner and outer clauses must havean 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 parent99b9928 commit3d00162
File tree
15 files changed
+2951
-82
lines changed- doc/src/sgml
- src
- backend
- optimizer
- path
- plan
- prep
- utils/misc
- include
- nodes
- optimizer
- test/regress
- expected
- sql
- tools/pgindent
15 files changed
+2951
-82
lines changedLines changed: 16 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
5593 | 5593 |
| |
5594 | 5594 |
| |
5595 | 5595 |
| |
| 5596 | + | |
| 5597 | + | |
| 5598 | + | |
| 5599 | + | |
| 5600 | + | |
| 5601 | + | |
| 5602 | + | |
| 5603 | + | |
| 5604 | + | |
| 5605 | + | |
| 5606 | + | |
| 5607 | + | |
| 5608 | + | |
| 5609 | + | |
| 5610 | + | |
| 5611 | + | |
5596 | 5612 |
| |
5597 | 5613 |
| |
5598 | 5614 |
| |
|
Lines changed: 39 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3440 | 3440 |
| |
3441 | 3441 |
| |
3442 | 3442 |
| |
| 3443 | + | |
| 3444 | + | |
| 3445 | + | |
| 3446 | + | |
| 3447 | + | |
| 3448 | + | |
| 3449 | + | |
| 3450 | + | |
| 3451 | + | |
| 3452 | + | |
| 3453 | + | |
| 3454 | + | |
| 3455 | + | |
| 3456 | + | |
| 3457 | + | |
| 3458 | + | |
3443 | 3459 |
| |
3444 | 3460 |
| |
3445 | 3461 |
| |
| |||
3495 | 3511 |
| |
3496 | 3512 |
| |
3497 | 3513 |
| |
| 3514 | + | |
3498 | 3515 |
| |
3499 | 3516 |
| |
3500 | 3517 |
| |
| |||
3546 | 3563 |
| |
3547 | 3564 |
| |
3548 | 3565 |
| |
| 3566 | + | |
| 3567 | + | |
| 3568 | + | |
| 3569 | + | |
| 3570 | + | |
| 3571 | + | |
| 3572 | + | |
| 3573 | + | |
| 3574 | + | |
| 3575 | + | |
| 3576 | + | |
| 3577 | + | |
| 3578 | + | |
| 3579 | + | |
| 3580 | + | |
| 3581 | + | |
| 3582 | + | |
| 3583 | + | |
3549 | 3584 |
| |
3550 | 3585 |
| |
3551 | 3586 |
| |
| |||
3588 | 3623 |
| |
3589 | 3624 |
| |
3590 | 3625 |
| |
| 3626 | + | |
| 3627 | + | |
| 3628 | + | |
3591 | 3629 |
| |
| 3630 | + | |
3592 | 3631 |
| |
3593 | 3632 |
| |
3594 | 3633 |
| |
|
0 commit comments
Comments
(0)