Re: Slow performance with trivial self-joins

From: Benny Kramek <benny(at)medflyt(dot)com>
To:
Cc: pgsql-performance(at)lists(dot)postgresql(dot)org
Subject: Re: Slow performance with trivial self-joins
Date: 2020-02-05 21:55:49
Message-ID: CAGPCyEYHdQGHKr_h91zp_d95LRa45t-A5N2OAdiRrK7Z7T5fYg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Thank you for your response. I have tested out the patch in the linked
thread and it works very well on a bunch of complex queries that I
have tested, improving both the planning time significantly and the
execution time drastically.

I have also read through the entire linked discussion thread as well
as a few other large threads linked from it, and found the discussion
very interesting.

I don't believe that all such queries are "poorly-written". As was
discussed in the other threads, the reason these types of self-joins
can occur is when you use SQL views. You can create a library of
reusable views that are small, easy-to-understand and readable. Then
you build them up into bigger views, and finally query from them. But
then you end up with lots of (hidden) self-joins. The alternative is
to copy&paste the shared logic from the views into all of the queries.

I understand the need to be conservative about which optimizations to
apply in order to not waste time looking for opportunities that don't
exist. One idea I had that I didn't see mentioned is the following
heuristic: Only if a query references an SQL view (or multiple views),
then try to apply the self_join_removal optimization. This should be
enough, because as you say, no human would intentionally write such a
query. Queries generated by ORMs were also discussed, so I believe it
might also be beneficial to consider queries that contain inner
SELECTs.

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Adam Brusselback 2020-02-05 22:12:21 Re: Slow performance with trivial self-joins
Previous Message Haroldo Kerry 2020-02-05 17:46:58 Re: Writing 1100 rows per second