X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A5519D7908 for ; Thu, 25 Aug 2005 22:55:44 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 81172-04 for ; Fri, 26 Aug 2005 01:55:43 +0000 (GMT) Received: from mail27.sea5.speakeasy.net (mail27.sea5.speakeasy.net [69.17.117.29]) by svr1.postgresql.org (Postfix) with ESMTP id EF9C9D7911 for ; Thu, 25 Aug 2005 22:55:42 -0300 (ADT) Received: (qmail 25321 invoked from network); 26 Aug 2005 01:55:46 -0000 Received: from dsl081-060-184.sfo1.dsl.speakeasy.net (HELO noodles) ([64.81.60.184]) (envelope-sender ) by mail27.sea5.speakeasy.net (qmail-ldap-1.03) with RC4-MD5 encrypted SMTP for ; 26 Aug 2005 01:55:46 -0000 Subject: Re: Limit + group + join From: "Jeffrey W. Baker" To: Tobias Brox Cc: pgsql-performance@postgresql.org In-Reply-To: <20050826002709.GK10328@tobias.lan> References: <20050826002709.GK10328@tobias.lan> Content-Type: text/plain Date: Thu, 25 Aug 2005 18:56:59 -0700 Message-Id: <1125021419.16451.0.camel@noodles> Mime-Version: 1.0 X-Mailer: Evolution 2.2.1.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.043 required=5 tests=[AWL=0.043] X-Spam-Level: X-Archive-Number: 200508/428 X-Sequence-Number: 14175 On Fri, 2005-08-26 at 02:27 +0200, Tobias Brox wrote: > Consider this setup - which is a gross simplification of parts of our > production system ;-) > > create table c (id integer primary key); > create table b (id integer primary key, c_id integer); > create index b_on_c on b(c_id) > > insert into c (select ... lots of IDs ...); > insert into b (select id, id from c); /* keep it simple :-) */ > > Now, I'm just interessted in some few rows. > > All those gives good plans: > > explain select c.id from c order by c.id limit 1; > explain select c.id from c group by c.id order by c.id limit 1; > explain select c.id from c join b on c_id=c.id order by c.id limit 1; > > ... BUT ... combining join, group and limit makes havoc: > > explain select c.id from c join b on c_id=c.id group by c.id order by c.id > desc limit 5; Where's b in this join clause? It looks like a cartesian product to me. -jwb