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 F411FD6FC7 for ; Fri, 26 Aug 2005 00:17:53 -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 17829-03 for ; Fri, 26 Aug 2005 03:17:52 +0000 (GMT) Received: from linda-3.paradise.net.nz (bm-3a.paradise.net.nz [202.0.58.22]) by svr1.postgresql.org (Postfix) with ESMTP id 08BFAD6FDD for ; Fri, 26 Aug 2005 00:17:51 -0300 (ADT) Received: from smtp-1.paradise.net.nz (smtp-1a.paradise.net.nz [202.0.32.194]) by linda-3.paradise.net.nz (Paradise.net.nz) with ESMTP id <0ILT00KK77TO3C@linda-3.paradise.net.nz> for pgsql-performance@postgresql.org; Fri, 26 Aug 2005 15:17:50 +1200 (NZST) Received: from [192.168.1.11] (218-101-14-16.paradise.net.nz [218.101.14.16]) by smtp-1.paradise.net.nz (Postfix) with ESMTP id 9C26E831B2; Fri, 26 Aug 2005 15:01:03 +1200 (NZST) Date: Fri, 26 Aug 2005 15:01:01 +1200 From: Mark Kirkwood Subject: Re: Limit + group + join In-reply-to: <20050826002709.GK10328@tobias.lan> To: Tobias Brox Cc: pgsql-performance@postgresql.org Message-id: <430E85ED.60109@paradise.net.nz> MIME-version: 1.0 Content-type: text/plain; format=flowed; charset=ISO-8859-1 Content-transfer-encoding: 7bit X-Accept-Language: en-us, en User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050726) References: <20050826002709.GK10328@tobias.lan> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.653 required=5 tests=[AWL=-0.373, RCVD_IN_NJABL_PROXY=1.026] X-Spam-Level: X-Archive-Number: 200508/431 X-Sequence-Number: 14178 Tobias, Interesting example: The 'desc' seems to be the guy triggering the sort, e.g: explain select c.id from c join b on c_id=c.id group by c.id order by c.id limit 5; QUERY PLAN ----------------------------------------------------------------------------------------- Limit (cost=0.00..0.28 rows=5 width=4) -> Group (cost=0.00..4476.00 rows=80000 width=4) -> Merge Join (cost=0.00..4276.00 rows=80000 width=4) Merge Cond: ("outer".id = "inner".c_id) -> Index Scan using c_pkey on c (cost=0.00..1518.00 rows=80000 width=4) -> Index Scan using b_on_c on b (cost=0.00..1558.00 rows=80000 width=4) (6 rows) Whereas with it back in again: explain select c.id from c join b on c_id=c.id group by c.id order by c.id desc limit 5; QUERY PLAN -------------------------------------------------------------------------------------- Limit (cost=10741.08..10741.11 rows=5 width=4) -> Group (cost=10741.08..11141.08 rows=80000 width=4) -> Sort (cost=10741.08..10941.08 rows=80000 width=4) Sort Key: c.id -> Hash Join (cost=1393.00..4226.00 rows=80000 width=4) Hash Cond: ("outer".c_id = "inner".id) -> Seq Scan on b (cost=0.00..1233.00 rows=80000 width=4) -> Hash (cost=1193.00..1193.00 rows=80000 width=4) -> Seq Scan on c (cost=0.00..1193.00 rows=80000 width=4) (9 rows) However being a bit brutal: set enable_mergejoin=false; set enable_hashjoin=false; explain select c.id from c join b on c_id=c.id group by c.id order by c.id desc limit 5; QUERY PLAN -------------------------------------------------------------------------------------------------- Limit (cost=0.00..15.24 rows=5 width=4) -> Group (cost=0.00..243798.00 rows=80000 width=4) -> Nested Loop (cost=0.00..243598.00 rows=80000 width=4) -> Index Scan Backward using c_pkey on c (cost=0.00..1518.00 rows=80000 width=4) -> Index Scan using b_on_c on b (cost=0.00..3.01 rows=1 width=4) Index Cond: (b.c_id = "outer".id) (6 rows) What is interesting is why this plan is being rejected... Cheers Mark 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; >