Re: Query planner not using indexes with JOIN query and OR clause

From: Ben Hoyt <benhoyt(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: postgres performance list <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Query planner not using indexes with JOIN query and OR clause
Date: 2015-07-14 20:17:52
Message-ID: CAL9jXCFm=XJ4MehSSX1C6wQeBxiZKP8X=QfKdVdRJUTtxQv=XA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

>
> Try refactoring to:
>
> select ai.position, i.filename as image_filename, p.filename as
> panorama_filename
> from album_items ai
> left join image2 i on i.imageid = ai.image_id
> left join panoramas p on p.id = ai.panorama_id
> where i.filename in ('pano360--v471.jpg', 'pano360-2--v474.jpg')
> union all select ai.position, i.filename as image_filename, p.filename
> as panorama_filename
> from album_items ai
> left join image2 i on i.imageid = ai.image_id
> left join panoramas p on p.id = ai.panorama_id
> where p.filename in ('pano360--v471', 'pano360-2--v474')
>
> ...and see if that helps. Dealing with 'or' conditions is a general
> weakness of the planner that has gotten better over time but in some
> cases you have to boil it to 'union all'.
>

Yes, this definitely helps and the query performance goes back to normal,
thanks. It makes the code a bit more complicated, so not ideal, but
definitely works!

Thanks for the help. I don't how much you know about PostgreSQL internals
(I don't!), but what optimization would need to be in place for PostgreSQL
to be smarter about this query?

-Ben

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Ryan King - NOAA Affiliate 2015-07-15 14:13:53 Re: could not create shared memory segment: Invalid argument
Previous Message Andy Colson 2015-07-14 13:59:16 Re: could not create shared memory segment: Invalid argument