pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: wouter-postgresql@publica.duodecim.org
To: pgsql-general@lists.postgresql.org
Subject: Conditional JOINs to optimise expensive views
Date: Sun, 05 Aug 2018 20:05:31 +0300
Message-ID: <1533488731.48072.1464077088.225906DE@webmail.messagingengine.com> (raw)

Hello all,

I'm trying to write queries with conditional JOINs. The goal is to guard expensive views by checking some conditions first and not execute the view at all if any of the conditions fail. Think along the lines of REST, like Not Authorized or Not Modified.

My problem is that the Postgresql (9.5 and 9.6) query planner executes LEFT and LATERAL JOINed queries even when the JOIN conditions are false.

I don't understand why.

I could work around some of this by adding a (dirty-ish?) CASE statement or a slower and more complex CTE construct or even create functions for each of these queries, but I'd really like to know why the most simple solution doesn't work. I tried many variations, but in my use cases, Postgresql always executes JOINs if the conditions are not constants.

What I'm really looking for is a general pattern with good performance to guard expensive views.

Here is an example query (is_owner is false):

SELECT is_owner, is_newer, json 
FROM (
     SELECT id, owner = '053bffbc-c41e-dad4-853b-ea91fc42ea17' "is_owner"
          , modified >= created "is_newer" 
     FROM datasets 
     WHERE id = '056e4eed-ee63-2add-e981-0c86b8b6a66f'
) cond
LEFT JOIN view_dataset view
    ON view.id = cond.id 
   AND cond.is_owner 
   AND cond.is_newer;

Which returns (correctly, but executing the slow JOINed view):
is_owner: false
is_newer: true
json: (null)


I don't want that query to execute the JOINed view, that is the whole point of those conditions, but it does.


Here is a working sql fiddle:

http://sqlfiddle.com/#!17/6882c6/5

And here is the stackoverflow thread:

https://dba.stackexchange.com/questions/211642/optimising-expensive-join-subquery-by-filtering-with-...


(Note that in the fiddle example there is a  0.5 second delay in the view to simulate it being expensive, so it should be pretty clear if the query planner executes the view or not.)

Thanks for any insight!




Message-ID: <1533488731.48072.1464077088.225906DE@webmail.messagingengine.com>
Permalink:  ../1533488731.48072.1464077088.225906DE@webmail.messagingengine.com/
Also on:    postgresql.org/message-id/1533488731.48072.1464077088.225906DE@webmail.messagingengine.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: wouter-postgresql@publica.duodecim.org
  Subject: Re: Conditional JOINs to optimise expensive views
  In-Reply-To: <1533488731.48072.1464077088.225906DE@webmail.messagingengine.com>
  References: <1533488731.48072.1464077088.225906DE@webmail.messagingengine.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox