X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1B68D52808 for ; Wed, 18 May 2005 12:14:50 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 56176-04 for ; Wed, 18 May 2005 15:14:46 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id F195352802 for ; Wed, 18 May 2005 12:14:45 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j4IFEeuD021958; Wed, 18 May 2005 11:14:40 -0400 (EDT) To: =?ISO-8859-2?Q?Sz=FBcs_G=E1bor?= Cc: "pgsql-performance@postgresql.org" Subject: Re: where+orderby+limit not (always) using appropriate index? In-reply-to: <428B5179.6060202@gmail.com> References: <428B5179.6060202@gmail.com> Comments: In-reply-to =?ISO-8859-2?Q?Sz=FBcs_G=E1bor?= message dated "Wed, 18 May 2005 16:30:17 +0200" Date: Wed, 18 May 2005 11:14:39 -0400 Message-ID: <21957.1116429279@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/314 X-Sequence-Number: 12478 =?ISO-8859-2?Q?Sz=FBcs_G=E1bor?= writes: > Create a table with (at least) two fields, say i and o. > Create three indexes on (i), (o), (i,o) > Insert enough rows to test. > Try to replace min/max aggregates with indexable queries such as: > SELECT o FROM t WHERE i = 1 ORDER BY o LIMIT 1; > Problem #1: This tends to use one of the single-column indexes (depending on > the frequency of the indexed element), not the two-column index. Also, I'm > not perfectly sure but maybe the planner is right. Why? To get the planner to use the double-column index, you have to use an ORDER BY that matches the index, eg SELECT o FROM t WHERE i = 1 ORDER BY i,o LIMIT 1; regards, tom lane