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 22C269FA4A8 for ; Mon, 12 Jun 2006 19:53:17 -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 40490-05 for ; Mon, 12 Jun 2006 19:53:11 -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 D79869FA10A for ; Mon, 12 Jun 2006 19:53:12 -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 k5CMr8lY013393; Mon, 12 Jun 2006 18:53:08 -0400 (EDT) To: PFC cc: pgsql-performance@postgresql.org Subject: Re: Interesting slow query In-reply-to: References: <003601c687ce$7aee7b20$7f00a8c0@kicommunication.com> Comments: In-reply-to PFC message dated "Mon, 12 Jun 2006 21:42:00 +0200" Date: Mon, 12 Jun 2006 18:53:08 -0400 Message-ID: <13392.1150152788@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/142 X-Sequence-Number: 19499 PFC writes: > Here are two ways to phrase a query... the planner choses very different > plans as you will see. Everything is freshly ANALYZEd. Usually we get complaints the other way around (that the NOT EXISTS approach is a lot slower). You did not show any statistics, but I suspect the key point here is that the condition id > 1130306 excludes most or all of the A and D tables. The planner is not smart about making transitive inequality deductions, but you could help it along by adding the implied clauses yourself: EXPLAIN ANALYZE SELECT r.* FROM raw_annonces r LEFT JOIN annonces a ON (a.id=r.id AND a.id > 1130306) LEFT JOIN archive_data d ON (d.id=r.id AND d.id > 1130306) WHERE a.id IS NULL AND d.id IS NULL AND r.id > 1130306 order by id limit 1; Whether this is worth doing in your app depends on how often you do searches at the end of the ID range ... regards, tom lane