Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id E3C532E008C for ; Mon, 2 Jun 2008 16:44:27 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 58294-07 for ; Mon, 2 Jun 2008 16:44:20 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outmail137071.authsmtp.com (outmail137071.authsmtp.com [62.13.137.71]) by developer.postgresql.org (Postfix) with ESMTP id 8D9BC2E0085 for ; Mon, 2 Jun 2008 16:44:22 -0300 (ADT) Received: from mail-c188.authsmtp.com (mail-c188.authsmtp.com [62.13.128.25]) by punt4.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id m52JiJaa038456; Mon, 2 Jun 2008 20:44:19 +0100 (BST) Received: from [192.168.0.3] (85-211-65-92.dyn.gotadsl.co.uk [85.211.65.92]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id m52JiIPb081373; Mon, 2 Jun 2008 20:44:18 +0100 (BST) Subject: Re: Outer joins and equivalence From: Simon Riggs To: Tom Lane Cc: pgsql-performance In-Reply-To: <1212426644.4120.378.camel@ebony.site> References: <1211918358.4489.257.camel@ebony.site> <14085.1211924592@sss.pgh.pa.us> <1212426644.4120.378.camel@ebony.site> Content-Type: multipart/mixed; boundary="=-u+iuECFIz26IPeT1jaPb" Date: Mon, 02 Jun 2008 20:47:11 +0100 Message-Id: <1212436031.4120.400.camel@ebony.site> Mime-Version: 1.0 X-Mailer: Evolution 2.12.0 X-Server-Quench: 4a5940a6-30dc-11dd-aecc-001871e930f4 X-AuthRoute: OCdxZQATClZOTQEd DAteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksadBtFaAdbZ0xf HGQLW1xEUl17XWd/ agIfZQBDYEtPQQNv TkFLXVBXFgB3AVJc AxxkC2ASJQVOeX13 ZghhXXleEkArcE95 Qh9SCG4GMTR9aWFL BF0JIlAHbQNKfxdC awV2V3NbZStlM3Bw LDceBR4WBxBqYANI XgwXZVAVQEAQVjc6 S1gPBCguA0QBSiM4 InQn X-Authentic-SMTP: 61633235383639.cat.dmpriest.net.uk:1045/Kp X-Report-SPAM: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-Virus-Status: No virus detected - but ensure you scan with your own anti-virus system! X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/3 X-Sequence-Number: 30353 --=-u+iuECFIz26IPeT1jaPb Content-Type: text/plain Content-Transfer-Encoding: 7bit On Mon, 2008-06-02 at 18:10 +0100, Simon Riggs wrote: > On Tue, 2008-05-27 at 17:43 -0400, Tom Lane wrote: > > Simon Riggs writes: > > > I have a complex query where making a small change to the SQL increases > > > run-time by > 1000 times. > > > > > The first SQL statement is of the form > > > > > A JOIN B ON (a.id = b.id) LEFT JOIN C ON (a.id = c.id) > > > > > and the second is like this > > > > > A JOIN B ON (a.id = b.id) LEFT JOIN C ON (b.id = c.id) > > > > > the only difference is the substitution of a -> b > > > > Please provide an actual test case. > > Getting closer, but still not able to produce a moveable test case. I've got a test case which shows something related and weird, though not the exact case. The queries shown here have significantly different costs, depending upon whether we use tables a or b in the query. Since a and b are equivalent this result isn't expected at all. I suspect the plan variation in the original post is somehow cost related and we are unlikely to discover the exact plan. -- Simon Riggs www.2ndQuadrant.com PostgreSQL Training, Services and Support --=-u+iuECFIz26IPeT1jaPb Content-Disposition: attachment; filename=q.sql Content-Type: text/x-sql; name=q.sql; charset=utf-8 Content-Transfer-Encoding: 7bit drop table if exists a cascade; create table a (id integer not null primary key ,partition integer ,filler text ); create table a_p1 as select generate_series(1, 10000000)::integer as id, 1::integer as partition, repeat('a',100) as filler; alter table a_p1 add primary key (id); alter table a_p1 inherit a; alter table a_p1 add check ( partition = 1); create table a_p2 as select generate_series(1000000000,1001000000)::integer as id, 2::integer as partition, repeat('a',100) as filler; alter table a_p2 add primary key (id); alter table a_p2 inherit a; alter table a_p2 add check ( partition = 2); drop table if exists b cascade; create table b (id integer not null primary key ,filler text ); drop table if exists c cascade; create table c (id integer not null primary key ,othercol integer ); create index c_idx on c (othercol); insert into c select generate_series(1,24667), 0; insert into c select generate_series(25000, 27488), 1; explain select count(*) from a join b on (a.id = b.id) left join c on (a.id = c.id and c.othercol = 1); explain select count(*) from a join b on (a.id = b.id) left join c on (b.id = c.id and c.othercol = 1); set enable_seqscan = off; explain select count(*) from a join b on (a.id = b.id) left join c on (a.id = c.id and c.othercol = 1); explain select count(*) from a join b on (a.id = b.id) left join c on (b.id = c.id and c.othercol = 1); --=-u+iuECFIz26IPeT1jaPb--