X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id DE0A09FB1FE for ; Wed, 9 Aug 2006 11:39:22 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 12553-08 for ; Wed, 9 Aug 2006 11:39:13 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 42FEF9FB2EA for ; Wed, 9 Aug 2006 11:39:13 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k79Ed6c0019963; Wed, 9 Aug 2006 10:39:06 -0400 (EDT) To: Patrice Beliveau cc: pgsql-performance@postgresql.org Subject: Re: Optimizing queries 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> Comments: In-reply-to Patrice Beliveau message dated "Wed, 09 Aug 2006 08:05:02 -0400" Date: Wed, 09 Aug 2006 10:39:06 -0400 Message-ID: <19962.1155134346@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.137 tagged_above=0 required=5 tests=AWL, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200608/148 X-Sequence-Number: 20333 Patrice Beliveau 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