Re: converting in() clause into a with prefix?

From: Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)com>
To: Benjamin Smith <lists(at)benjamindsmith(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: converting in() clause into a with prefix?
Date: 2015-10-16 22:06:44
Message-ID: 562174F4.7020103@BlueTreble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/16/15 1:18 PM, Benjamin Smith wrote:
> I have a horribly-performing query similar to below, and I'd like to convert
> it to use a "WITH mytable as ( ... ) " without having to re-architect my code.
> For some reason, using a WITH prefix seems to generally work much faster than
> IN() sub clause even allowing identical results. (runs in 1/4th the time)

Presumably it's faster because WITH is currently an optimization fence.
Instead of WITH, you could also do

WHERE (classes.school_id, classes.building_id) IN( SELECT a, b FROM
unnest(...) u(a, b))
--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jim Nasby 2015-10-16 22:19:44 Re: postgres function
Previous Message Jim Nasby 2015-10-16 21:59:12 Re: ID column naming convention