Hello, an 2015 14:13:37 +1100, James Sewell <james.sewell@xxxxxxxxxxxx> wrote in <CANkGpBs8GypQ3TQGKdjTD+n-w1rkq5uO97h3tuhg5eWaKR6RbA@xxxxxxxxxxxxxx> > Sadly not ... I still hit all the tables. | 5.9.4. Partitioning and Constraint Exclusion http://www.postgresql.org/docs/9.4/static/ddl-partitioning.html Constraint exclusion is a mechanism to omit tables that are known to have no hit by the query *beforehand* execution. So the criteria cannot rely on out of the query itself (and CHECK constraints, of course). Your query uses the result of the WITH-clause-query in the WHERE clause which is unknown to the planner so constraint exclusion does not work. JOINs don't change the situation. > On Mon, Jan 19, 2015 at 1:54 PM, John R Pierce <pierce@xxxxxxxxxxxx> wrote: > > > On 1/18/2015 5:58 PM, James Sewell wrote: > > > > WITH idlist as (SELECT id from othertable) > > SELECT id from mastertable WHERE id = idlist.id); > > > > > > > > select mt.id, ... from mastertable mt join othertable ot on mt.id= > > ot.id; > > > > might optimize better. As the result, the query inevitably scans all the tables, but not necessariry in sequqntial scans or simple index scans. The suggestion above seeems showing the notation which the planner can find the better plans on that premise. For example, if you have an index on id of one of the two tables, (and some other conditions match, of course) index only scan will be selected for it and the suggested query will give you a seemingly better plan than your query. regards, -- Kyotaro Horiguchi NTT Open Source Software Center -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general