Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 3D590B5DBCA for ; Mon, 26 Sep 2011 03:53:23 -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 99217-05 for ; Mon, 26 Sep 2011 06:53:15 +0000 (UTC) X-Greylist: delayed 00:24:53.630202 by SQLgrey-1.8.0-rc2 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 1DE96B5DBC7 for ; Mon, 26 Sep 2011 03:53:15 -0300 (ADT) Received: from symstream.com (unknown [192.168.0.254]) by mail2.symstream.com (Postfix) with SMTP id DD2306AD86 for ; Mon, 26 Sep 2011 16:28:15 +1000 (EST) Received: (qmail 22102 invoked by uid 453); 26 Sep 2011 06:28:15 -0000 X-Virus-Checked: Checked by ClamAV on symstream.com Received: from ajs-desktop.symstream.com (HELO ajs-desktop.symstream.com) (192.168.0.22) by symstream.com (qpsmtpd/0.83) with ESMTP; Mon, 26 Sep 2011 16:28:15 +1000 From: anthony.shipman@symstream.com To: pgsql-performance@postgresql.org Subject: overzealous sorting? Date: Mon, 26 Sep 2011 16:28:15 +1000 User-Agent: KMail/1.9.4 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201109261628.15363.anthony.shipman@symstream.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.17 tagged_above=-10 required=5 tests=BAYES_00=-1.9, KHOP_DYNAMIC=0.73 X-Spam-Level: X-Archive-Number: 201109/361 X-Sequence-Number: 45137 In Mammoth Replicator (PG 8.3) I have a table described as Table "public.tevent_cdr" Column | Type | Modifiers ----------------+--------------------------+------------------------------------------------------------ event_id | integer | not null default nextval(('event_id_seq'::text)::regclass) timestamp | timestamp with time zone | not null classification | character varying | not null area | character varying | not null kind | character varying | device_id | integer | device_name | character varying | fleet_id | integer | fleet_name | character varying | customer_id | integer | customer_name | character varying | event | text | Indexes: "tevent_cdr_event_id" UNIQUE, btree (event_id) "tevent_cdr_timestamp" btree ("timestamp") Check constraints: "tevent_cdr_classification_check" CHECK (classification::text = 'cdr'::text) Inherits: tevent This simple query puzzles me. Why does it need to sort the records? Don't they come from the index in order? "explain analyze select * from tevent_cdr where timestamp >= '2011-09-09 12:00:00.000000+0' and timestamp < '2011-09-09 13:00:00.000000+0' and classification = 'cdr' order by timestamp;" QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Sort (cost=9270.93..9277.12 rows=2477 width=588) (actual time=9.219..11.489 rows=2480 loops=1) Sort Key: "timestamp" Sort Method: quicksort Memory: 2564kB -> Bitmap Heap Scan on tevent_cdr (cost=57.93..9131.30 rows=2477 width=588) (actual time=0.440..3.923 rows=2480 loops=1) Recheck Cond: (("timestamp" >= '2011-09-09 22:00:00+10'::timestamp with time zone) AND ("timestamp" < '2011-09-09 23:00:00+10'::timestamp with time zone)) Filter: ((classification)::text = 'cdr'::text) -> Bitmap Index Scan on tevent_cdr_timestamp (cost=0.00..57.31 rows=2477 width=0) (actual time=0.404..0.404 rows=2480 loops=1) Index Cond: (("timestamp" >= '2011-09-09 22:00:00+10'::timestamp with time zone) AND ("timestamp" < '2011-09-09 23:00:00+10'::timestamp with time zone)) Total runtime: 13.847 ms (9 rows) -- Anthony Shipman | flailover systems: When one goes down it Anthony.Shipman@symstream.com | flails about until the other goes down too.