Re: Eliminating unnecessary left joins

From: Ottó Havasvölgyi <havasvolgyi(dot)otto(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Eliminating unnecessary left joins
Date: 2007-04-07 16:45:42
Message-ID: 34608c0c0704070945i2a846e2cp7b76ce07f7a39b18@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavan Deolasee 2007-04-07 16:58:23 Re: [HACKERS] Optimized pgbench for 8.3
Previous Message Bruce Momjian 2007-04-07 16:11:51 Re: Last chance to object to MVCC-safe CLUSTER