Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 4F7966323C3 for ; Wed, 2 Jun 2010 19:42:03 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 51407-05 for ; Wed, 2 Jun 2010 22:41:55 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 95AFA62F5D9 for ; Wed, 2 Jun 2010 19:41:55 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o52MfrsK013219; Wed, 2 Jun 2010 18:41:53 -0400 (EDT) To: "Kevin Grittner" cc: "Jori Jovanovich" , pgsql-performance@postgresql.org Subject: Re: SELECT ignoring index even though ORDER BY and LIMIT present In-reply-to: <4C0694060200002500031DEA@gw.wicourts.gov> References: <4C0694060200002500031DEA@gw.wicourts.gov> Comments: In-reply-to "Kevin Grittner" message dated "Wed, 02 Jun 2010 17:25:26 -0500" Date: Wed, 02 Jun 2010 18:41:52 -0400 Message-ID: <13218.1275518512@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201006/46 X-Sequence-Number: 38902 "Kevin Grittner" writes: > Jori Jovanovich wrote: >> what is the recommended way to solve this? > The recommended way is to adjust your costing configuration to > better reflect your environment. Actually, it's probably not the costs so much as the row estimates. For instance, that first query was estimated to select 20 out of a possible 24 rows. If 24 is indeed the right number of matches, then the planner is right and the OP is wrong: the indexscan is going to have to traverse almost all of the table and therefore it will be a lot slower than seqscan + sort. Now, if the real number of matches is a lot more than that, then the indexscan would make sense because it could be expected to get stopped by the LIMIT before it has to traverse too much of the table. So the true problem is to get the rowcount estimate to line up with reality. Unfortunately the estimates for ~* are typically not very good. If you could convert that to plain ~ (case sensitive) it'd probably work better. Also, if this isn't a particularly modern version of Postgres, a newer version might do a bit better with the estimate. regards, tom lane