Re: Eliminating unnecessary left joins - Mailing list pgsql-hackers

From Ottó Havasvölgyi
Subject Re: Eliminating unnecessary left joins
Date
Msg-id 34608c0c0704070945i2a846e2cp7b76ce07f7a39b18@mail.gmail.com
Whole thread Raw
In response to Re: Eliminating unnecessary left joins  ("Simon Riggs" <simon@2ndquadrant.com>)
Responses Re: Eliminating unnecessary left joins  ("Nicolas Barbier" <nicolas.barbier@gmail.com>)
Re: Eliminating unnecessary left joins  (Jim Nasby <decibel@decibel.org>)
List pgsql-hackers
Sorry, I have left out the PK requirement.
What Nicolas wrote is right, I also use an O/R mapper and inheritance is solved with vertical partitioning. The tables are connected to each other with the PK. And the mapper defines views for each class with left joins. The mapper generates queries based on these views. A high fraction of the joins would be eliminated almost in every query.
 
My simple example:
 
Class hierarchy and fields:
Shape (ID, X, Y)
|
+-Circle (ID, Radius)
|
+-Rectangle (ID, Width, Height)
 
The mapper creates 3 tables with the columns next to the class name.
And it creates 3 views. One of them: 
 
RectangleView:  SELECT r."ID" as "ID", s."X" as "X", s."Y" as "Y", r."Width" as "Width", r."Height" as "Height" FROM "Rectangle" r LEFT JOIN "Shape" s ON ( r.ID=s.ID)
 
Now if I query Rectangle object IDs, whose Width is greater than 5, it will generate this:
 
SELECT "ID" FROM RectangleView WHERE "Width">5
 
In this case I don't need to left join the Shape table, because X and Y columns are not used.
 
 
The other typical situation is when I execute more complex, not-O/Rmapper-generated SQL commands based on these views for reporting. For example the average width of rectangles whose height is greater than 10.
----------------------------------------------------
 
This optimization should be also applied to subqueries.
 
 
 
Is this optimization relatively easy to introduce?
 
I would gladly work on this, but unfortunately I don't know the codebase at all.
I would really appreciate if someone competent implemented this feature in 8.4.
 
Thank you in advance,
Otto
 

pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Last chance to object to MVCC-safe CLUSTER
Next
From: "Pavan Deolasee"
Date:
Subject: Re: [PATCHES] Optimized pgbench for 8.3