pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Patrice Beliveau <pbeliveau@avior.ca>
Cc: pgsql-performance@postgresql.org
Subject: Re: Optimizing queries
Date: Wed, 09 Aug 2006 10:39:06 -0400
Message-ID: <19962.1155134346@sss.pgh.pa.us> (raw)
In-Reply-To: <44D9CF6E.9030900@avior.ca>
References: <44D8CE92.1080407@avior.ca>
	<1155062399.20252.50.camel@state.g2switchworks.com>
	<44D8F0B8.2030804@avior.ca>
	<13030.1155069749@sss.pgh.pa.us>
	<44D9CF6E.9030900@avior.ca>

Patrice Beliveau <pbeliveau@avior.ca> writes:
> Tom Lane wrote:
>> PG 8.1 will not reorder WHERE clauses for a single table unless it has
>> some specific reason to do so (and AFAICT no version back to 7.0 or so
>> has done so either...)  So there's something you are not telling us that
>> is relevant.

> here is my query, and the query plan that result

> explain select * from (
>    select * from sales_order_delivery
>        where sales_order_id in (
>                select sales_order_id from sales_order
>                where closed=false
>        )
>  ) as a where outstandingorder(sales_order_id, sales_order_item, 
> date_due) > 0;

So this isn't a simple query, but a join.  PG will generally push
single-table restrictions down to the individual tables in order to
reduce the number of rows that have to be processed at the join.
In this case that's not a win, but the planner doesn't know enough
about the outstandingorder() function to realize that.

I think what you need is an "optimization fence" to prevent the subquery
from being flattened:

explain select * from (
   select * from sales_order_delivery
       where sales_order_id in (
               select sales_order_id from sales_order
               where closed=false
       )
   OFFSET 0
 ) as a where outstandingorder(sales_order_id, sales_order_item, 
date_due) > 0;

Any LIMIT or OFFSET in a subquery prevents WHERE conditions from being
pushed down past it (since that might change the results).  OFFSET 0 is
otherwise a no-op, so that's what people usually use.

			regards, tom lane



view thread (9+ messages)  latest in thread

Message-ID: <19962.1155134346@sss.pgh.pa.us>
Permalink:  ../19962.1155134346@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/19962.1155134346@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pbeliveau@avior.ca
  Subject: Re: Optimizing queries
  In-Reply-To: <19962.1155134346@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox