Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 4ADA39FA43D for ; Mon, 23 Jul 2007 01:21:55 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 17708-07 for ; Mon, 23 Jul 2007 01:21:51 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from oxford.xeocode.com (unknown [87.127.95.196]) by postgresql.org (Postfix) with ESMTP id B6C889FA433 for ; Mon, 23 Jul 2007 01:21:51 -0300 (ADT) Received: from localhost.enta.net ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.67) (envelope-from ) id 1ICpQa-00025g-Rz; Mon, 23 Jul 2007 05:21:48 +0100 From: Gregory Stark To: Nis =?iso-8859-1?Q?J=F8rgensen?= Cc: Subject: Re: The nested view from hell - Restricting a subquerry In-Reply-To: (Nis =?iso-8859-1?Q?J=F8rgensen'?= =?iso-8859-1?Q?s?= message of "Mon, 23 Jul 2007 02:02:07 +0200") Organization: EnterpriseDB References: <46A2CABF.3080108@obviously.com> <2385.1185127507@sss.pgh.pa.us> X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.sql" 628) Date: Mon, 23 Jul 2007 05:21:48 +0100 Message-ID: <87y7h74un7.fsf@oxford.xeocode.com> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/129 X-Sequence-Number: 28864 Nis J=F8rgensen writes: > Well, the query can be satisfied by looking only at the rows with an > order_id matching the invoice_id given. The condition that this is the > largest invoice in the group then needs to be checked afterwards. > > I certainly did not expect the query planner to be able to deduce this, > though. No, that's not true. If you had two records in eg_order with the same order= _id but different invoice_ids then the query would need both records to satisfy the query. The query planner can't deduce that this can't happen because it simply does not have that information. The more I look at this view the more I think it's just seriously broken. Why is it grouping by order_id at all if, I suspect, there will only be one record per order_id in eg_orders?? --=20 Gregory Stark EnterpriseDB http://www.enterprisedb.com