Re: Poor performance on 9.4.4 when matching multiple columns from left side of LATERAL join - Mailing list pgsql-general

From David G. Johnston
Subject Re: Poor performance on 9.4.4 when matching multiple columns from left side of LATERAL join
Date
Msg-id CAKFQuwZPj_GnDgToMQthZ9JLOJehuTFKLknFO6AVkSAxxFwfAw@mail.gmail.com
Whole thread Raw
In response to Poor performance on 9.4.4 when matching multiple columns from left side of LATERAL join  (Steven Grimm <sgrimm@thesegovia.com>)
Responses Re: Poor performance on 9.4.4 when matching multiple columns from left side of LATERAL join  (Steven Grimm <sgrimm@thesegovia.com>)
List pgsql-general
On Fri, Nov 13, 2015 at 11:25 PM, Steven Grimm <sgrimm@thesegovia.com> wrote:
We want to find all the rows from multi_id where any of the IDs (including its primary key) have a certain setting with a certain value.

LATERAL seemed like the tool for the job, so we tried the following:

---------------------------------------------
SELECT mid.id1
FROM multi_id AS mid,
LATERAL (
    SELECT 1
    FROM settings
    WHERE setting_id = 1
    AND setting_value = 'common_1'
    AND owner_id IN (mid.id1, mid.id2, mid.id3)
) AS setting_matcher;
---------------------------------------------
 
​IN semantics w.r.t NULL can result in atrocious performance in some instances.  I cannot speak to this one in particular but I'm curious if
[...]
WHERE setting_id = 1
AND setting_value = 'common_1'
AND (
owner_id = mid.id1
OR
owner_id = mid.id2
OR
owner_id = mid.id3
)​
 

placed into an EXISTS would work any better.  It seems pointless to include a LATERAL if you are not going to output any of the fields from the laterally joined relation.  If you want a join I'm not sure that INNER wouldn't be just as good, with an ON clause of (owner_id = mid.id1 OR owner_id = mid.id2 OR owner_id = mid.id3)

David J.

pgsql-general by date:

Previous
From: Steven Grimm
Date:
Subject: Poor performance on 9.4.4 when matching multiple columns from left side of LATERAL join
Next
From: Steven Grimm
Date:
Subject: Re: Poor performance on 9.4.4 when matching multiple columns from left side of LATERAL join