Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 61D67B5DBC0 for ; Tue, 27 Sep 2011 05:54:46 -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 00223-09 for ; Tue, 27 Sep 2011 08:54:39 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-bw0-f46.google.com (mail-bw0-f46.google.com [209.85.214.46]) by mail.postgresql.org (Postfix) with ESMTP id 1D6A6B5DBBD for ; Tue, 27 Sep 2011 05:54:38 -0300 (ADT) Received: by bkbzt4 with SMTP id zt4so6670128bkb.19 for ; Tue, 27 Sep 2011 01:54:38 -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=tE3nuhIre7JPbTbiiL82UZ4tAwpkAco8CRYBDfOFOyU=; b=LynRJfvlz9lbRzrmCUMzYK3t+w7ePWW8ZDi8UidB4YwUAnjtIZy+fuVkbF/O8zg6IB kvtbAOeO3eIgCJrrd6hACFIAnxkHs/L9Oi+NCmTRvVxvdzOjx4/9ey50DgHXQEV4JAOn 8fgZvSsARitR6t+oXN9O8aY3q5Y0ybMD3kUF4= Received: by 10.216.181.194 with SMTP id l44mr8966720wem.87.1317113678023; Tue, 27 Sep 2011 01:54:38 -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 k26sm31941466wbo.16.2011.09.27.01.54.36 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 27 Sep 2011 01:54:37 -0700 (PDT) Date: Tue, 27 Sep 2011 10:54:35 +0200 From: Marc Cousin To: pgsql-performance@postgresql.org Subject: Re: overzealous sorting? Message-ID: <20110927105435.26d4e9c6@marco-dalibo> In-Reply-To: <201109271245.00979.anthony.shipman@symstream.com> References: <201109261628.15363.anthony.shipman@symstream.com> <20110926113901.38fdf892@marco-dalibo> <201109271245.00979.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/385 X-Sequence-Number: 45161 Le Tue, 27 Sep 2011 12:45:00 +1000, anthony.shipman@symstream.com a =C3=A9crit : > On Monday 26 September 2011 19:39, Marc Cousin wrote: > > 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. >=20 > Where does this bitmap index scan come from? It seems to negate the > advantages of b-tree indexes described in the section "Indexes and > ORDER BY" of the manual. If I do "set enable_bitmapscan =3D off;" the > query runs a bit faster although with a larger time range it reverts > to a sequential scan. >=20 Bitmap Index Scan is just another way to use a btree index. It is often used when a bigger part of a table is required, as it costs more than plain index scan to retrieve a few records, but less when a lot of records are needed. Your tests show that index scans are a bit faster on this query. But it is probably true only when most needed data is cached, which is probably your case, as you are doing tests using the same query all the time. The bitmap index scan is probably cheaper when data isn't in cache. You could also see the bitmap index scan as safer, as it won't perform as bad when data is not cached (less random IO) :) The thing is, the optimizer doesn't know if your data will be in cache when you will run your query=E2=80=A6 if you are sure most of your data is = in the cache most of the time, you could try to tune random_page_cost (lower it) to reflect that data is cached. But if the win is small on this query, it may not be worth it.