Re: [NOVICE] Queries with Joins before filtering taking too muchtime! Filter (where clause) *first* -- suggestions ? - Mailing list pgsql-novice

From David G. Johnston
Subject Re: [NOVICE] Queries with Joins before filtering taking too muchtime! Filter (where clause) *first* -- suggestions ?
Date
Msg-id CAKFQuwYg5YgmKwVf1OGeEktO99yGPq_ed7_uSArciiBdR6tU6w@mail.gmail.com
Whole thread Raw
In response to [NOVICE] Re: Queries with Joins before filtering taking too much time! Filter(where clause) *first* -- suggestions ?  (Hursh Jain <hurshj@gmail.com>)
Responses Re: [NOVICE] Queries with Joins before filtering taking too muchtime! Filter (where clause) *first* -- suggestions ?  (Hursh Jain <hurshj@gmail.com>)
List pgsql-novice
On Sunday, January 15, 2017, Hursh Jain <hurshj@gmail.com> wrote:

SELECT * from property
WHERE pid in (
  SELECT pid FROM reward WHERE reward_type = 'DAILYPROMO_WIN')
;

And this also runs (again taking a long time) but without any errors. How can this be ? (there is no pid in the reward table, so why does the
subquery not throw an error)?


The "pid" column is the one from the outer query property table - this is a correlated subquery that you've written. 

As long as one record with that reward_type exists your query devolves to "where true" thus making indexes useless and returning every row in property.

David J.

pgsql-novice by date:

Previous
From: Hursh Jain
Date:
Subject: [NOVICE] Re: Queries with Joins before filtering taking too much time! Filter(where clause) *first* -- suggestions ?
Next
From: Piyush Katariya
Date:
Subject: [NOVICE] (Stateless and Distributed) Transaction in Event Driven Architecture