Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 160DDB5DBCA for ; Mon, 26 Sep 2011 06:44:42 -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 76922-04 for ; Mon, 26 Sep 2011 09:44:35 +0000 (UTC) X-Greylist: delayed 00:05:30.076584 by SQLgrey-1.8.0-rc2 Received: from mail-wy0-f174.google.com (mail-wy0-f174.google.com [74.125.82.174]) by mail.postgresql.org (Postfix) with ESMTP id 2452DB5DBF4 for ; Mon, 26 Sep 2011 06:44:34 -0300 (ADT) Received: by wyg34 with SMTP id 34so5735964wyg.19 for ; Mon, 26 Sep 2011 02:44:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=date:from:to:subject:message-id:in-reply-to:references:x-mailer :mime-version:content-type:content-transfer-encoding; bh=sK5SmUsJWf+Q91MVA0fB7bHxW71F5ndtnhkDjiWhDQM=; b=ST75gB354u8mmulvY8IdC/N/uYudu8mgA/tMwOTLR80qeOtmkyl3EOQ3qYKRiwrlbb dwNQVlbfOVZ6VoLYaDVdUrQQovL8vbou8Z4xNVw0E2zc1QrCyl4olG3M1rMOYcWkrw8m XsmMlp4hKEzdhvg360ATPLAIUYaT15BiBozAo= Received: by 10.216.133.5 with SMTP id p5mr3338179wei.34.1317029944551; Mon, 26 Sep 2011 02:39:04 -0700 (PDT) Received: from marco-dalibo (gev44-1-78-228-108-65.fbx.proxad.net. [78.228.108.65]) by mx.google.com with ESMTPS id i11sm30081445wbn.25.2011.09.26.02.39.02 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 26 Sep 2011 02:39:03 -0700 (PDT) Date: Mon, 26 Sep 2011 11:39:01 +0200 From: Marc Cousin To: pgsql-performance@postgresql.org Subject: Re: overzealous sorting? Message-ID: <20110926113901.38fdf892@marco-dalibo> In-Reply-To: <201109261628.15363.anthony.shipman@symstream.com> References: <201109261628.15363.anthony.shipman@symstream.com> X-Mailer: Claws Mail 3.7.10 (GTK+ 2.24.6; x86_64-unknown-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.889 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201109/363 X-Sequence-Number: 45139 Le Mon, 26 Sep 2011 16:28:15 +1000, anthony.shipman@symstream.com a =C3=A9crit : > In Mammoth Replicator (PG 8.3) I have a table described as=20 >=20 > 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=20 > =3D 'cdr'::text) > Inherits: tevent >=20 >=20 > This simple query puzzles me. Why does it need to sort the records? > Don't they come from the index in order? >=20 > "explain analyze select * from tevent_cdr where timestamp >=3D=20 > '2011-09-09 12:00:00.000000+0' and timestamp < '2011-09-09=20 > 13:00:00.000000+0' and classification =3D 'cdr' order by timestamp;" > = =20 > QUERY PLAN > -------------------------------------------------------------------------= ---------------------------------------------------------------------------= -------------------- > Sort (cost=3D9270.93..9277.12 rows=3D2477 width=3D588) (actual=20 > time=3D9.219..11.489 rows=3D2480 loops=3D1) > Sort Key: "timestamp" > Sort Method: quicksort Memory: 2564kB > -> Bitmap Heap Scan on tevent_cdr (cost=3D57.93..9131.30 > rows=3D2477 width=3D588) (actual time=3D0.440..3.923 rows=3D2480 loops=3D= 1) > Recheck Cond: (("timestamp" >=3D '2011-09-09=20 > 22:00:00+10'::timestamp with time zone) AND ("timestamp" < > '2011-09-09 23:00:00+10'::timestamp with time zone)) > Filter: ((classification)::text =3D 'cdr'::text) > -> Bitmap Index Scan on tevent_cdr_timestamp =20 > (cost=3D0.00..57.31 rows=3D2477 width=3D0) (actual time=3D0.404..0.404 > rows=3D2480 loops=3D1) > Index Cond: (("timestamp" >=3D '2011-09-09=20 > 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) Because Index Scans are sorted, not Bitmap Index Scans, which builds a list of pages to visit, to be then visited by the Bitmap Heap Scan step. Marc.