Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 82D2AB5DBC2 for ; Fri, 10 Jun 2011 05:38:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 41620-07 for ; Fri, 10 Jun 2011 08:38:43 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail2.symstream.com (2.102.96.58.static.exetel.com.au [58.96.102.2]) by mail.postgresql.org (Postfix) with ESMTP id 27554B5DBBE for ; Fri, 10 Jun 2011 05:38:42 -0300 (ADT) Received: from symstream.com (unknown [192.168.0.254]) by mail2.symstream.com (Postfix) with SMTP id 847336ACEF for ; Fri, 10 Jun 2011 18:38:40 +1000 (EST) Received: (qmail 18744 invoked by uid 453); 10 Jun 2011 08:38:40 -0000 X-Virus-Checked: Checked by ClamAV on symstream.com Received: from Unknown (HELO ajs-desktop.symstream.com) (192.168.0.22) by symstream.com (qpsmtpd/0.40) with ESMTP; Fri, 10 Jun 2011 18:38:40 +1000 From: anthony.shipman@symstream.com To: tv@fuzzy.cz Subject: Re: strange query plan with LIMIT Date: Fri, 10 Jun 2011 18:38:39 +1000 User-Agent: KMail/1.9.4 Cc: pgsql-performance@postgresql.org, "Claudio Freire" , Pavel Stehule References: <201106071802.08272.anthony.shipman@symstream.com> <201106081834.07263.anthony.shipman@symstream.com> In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201106101838.39781.anthony.shipman@symstream.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201106/109 X-Sequence-Number: 44042 On Wednesday 08 June 2011 19:47, tv@fuzzy.cz wrote: > Have you tried to create a composite index on those two columns? Not sure > if that helps but I'd try that. > > Tomas This finally works well enough CREATE TABLE tdiag ( diag_id integer DEFAULT nextval('diag_id_seq'::text), create_time timestamp with time zone default now(), .... PRIMARY KEY (diag_id) ); -- ************ COMPOSITE INDEX create index tdiag_id_create on tdiag(diag_id, create_time); alter table tdiag alter column diag_id set statistics 1000; alter table tdiag alter column create_time set statistics 1000; and then just do the original query symstream2=> explain analyze select * from tdiag where symstream2-> (create_time >= '2011-06-07 02:00:00.000000+0' and create_time < '2011-06-10 07:58:03.000000+0') and diag_level <= 1 symstream2-> order by diag_id LIMIT 100 OFFSET 800; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=6064.19..6822.21 rows=100 width=112) (actual time=1496.644..1497.094 rows=100 loops=1) -> Index Scan using tdiag_id_create on tdiag (cost=0.00..1320219.58 rows=174166 width=112) (actual time=1409.285..1495.831 rows=900 loops=1) Index Cond: ((create_time >= '2011-06-07 12:00:00+10'::timestamp with time zone) AND (create_time < '2011-06-10 17:58:03+10'::timestamp with time zone)) Filter: (diag_level <= 1) Total runtime: 1497.297 ms If I had set the primary key to (diag_id, create_time) would simple queries on diag_id still work well i.e. select * from tdiag where diag_id = 1234; -- Anthony Shipman | -module(erlang). Anthony.Shipman@symstream.com | ''(_)->0. %-)