Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 88FCEB5DC29 for ; Tue, 7 Jun 2011 13:40:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 33830-09 for ; Tue, 7 Jun 2011 16:40:15 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from elizabeth.gransy.com (elizabeth.gransy.com [89.187.132.199]) by mail.postgresql.org (Postfix) with ESMTP id 59DB8B5DC30 for ; Tue, 7 Jun 2011 13:40:15 -0300 (ADT) Received: from sq.gransy.com (localhost [127.0.0.1]) by elizabeth.gransy.com (Postfix) with ESMTP id 889C715E40BD; Tue, 7 Jun 2011 18:40:13 +0200 (CEST) Received: from 85.162.99.149 (SquirrelMail authenticated user tv@fuzzy.cz) by sq.gransy.com with HTTP; Tue, 7 Jun 2011 18:40:13 +0200 Message-ID: <30c6caeda38eb66a15665d147a5db451.squirrel@sq.gransy.com> In-Reply-To: <201106071802.08272.anthony.shipman@symstream.com> References: <201106071802.08272.anthony.shipman@symstream.com> Date: Tue, 7 Jun 2011 18:40:13 +0200 Subject: Re: strange query plan with LIMIT From: tv@fuzzy.cz To: anthony.shipman@symstream.com Cc: pgsql-performance@postgresql.org User-Agent: SquirrelMail/1.4.21 MIME-Version: 1.0 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal 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/49 X-Sequence-Number: 43982 > Version: PostgreSQL 8.3.5 (mammoth replicator) > > Schema: > > CREATE TABLE tdiag ( > diag_id integer DEFAULT nextval('diag_id_seq'::text), > create_time timestamp with time zone default now(), /* time this > record > was created */ > diag_time timestamp with time zone not null, > device_id integer, /* optional */ > fleet_id integer, /* optional */ > customer_id integer, /* optional */ > module character varying, > node_kind smallint, > diag_level smallint, > tag character varying not null default '', > message character varying not null default '', > options text, > > PRIMARY KEY (diag_id) > ); > > create index tdiag_create_time ON tdiag(create_time); > > The number of rows is over 33 million with time stamps over the past two > weeks. > > The create_time order is almost identical to the id order. What I want > to find is the first or last entry by id in a given time range. The > query I am having a problem with is: Hi, why are you reposting this? Pavel Stehule already recommended you to run ANALYZE on the tdiag table - have you done that? What was the effect? The stats are off - e.g. the bitmap scan says -> Bitmap Heap Scan on tdiag (cost=25763.48..638085.13 rows=1141019 width=114) (actual time=43.232..322.441 rows=86530 loops=1) so it expects to get 1141019 rows but it gets 86530, i.e. about 7% of the expected number. That might be enough to cause bad plan choice and thus performance issues. And yet another recommendation - the sort is performed on disk, so give it more work_mem and it should be much faster (should change from "merge sort" to "quick sort"). Try something like work_mem=20MB and see if it does the trick. regards Tomas