Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 48F4213364DE for ; Wed, 16 Mar 2011 14:05:16 -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 94798-04 for ; Wed, 16 Mar 2011 17:05:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp2.peak6.com (smtp2.peak6.com [38.98.137.9]) by mail.postgresql.org (Postfix) with ESMTP id AA911133616B for ; Wed, 16 Mar 2011 14:05:08 -0300 (ADT) X-AuditID: 0a02cb1e-b7bf8ae00000096d-da-4d80edc3d275 Message-ID: <4D80EDC2.8010103@peak6.com> Date: Wed, 16 Mar 2011 12:05:06 -0500 From: Shaun Thomas Reply-To: sthomas@peak6.com Organization: Peak6 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.7) Gecko/20100720 Lightning/1.0b2 Thunderbird/3.1.1 MIME-Version: 1.0 To: Timothy Garnett CC: "pgsql-performance@postgresql.org" Subject: Re: Adding additional index causes 20,000x slowdown for certain select queries - postgres 9.0.3 References: In-Reply-To: Content-Type: text/plain; charset="ISO-8859-1"; format=flowed content-transfer-encoding: 7bit X-Brightmail-Tracker: AAAAAQAAAZE= 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: 201103/166 X-Sequence-Number: 42845 On 03/15/2011 01:23 PM, Timothy Garnett wrote: > Column | Type > --------------------------+------------------------+ > id | integer | > bl_number | character varying(16) | > month | date | > buyer_id | integer | > supplier_id | integer | Ok. In your table description, you don't really talk about the distribution of bl_number. But this part of your query: ORDER BY month DESC LIMIT 100 OFFSET 0 Is probably tricking the planner into using that index. But there's the fun thing about dates: we almost always want them in order of most recent to least recent. So you might want to try again with your index_customs_records_on_month_and_bl_number declared like this instead: CREATE INDEX index_customs_records_on_month_and_bl_number ON customs_records (month DESC, bl_number); Or, if bl_number is more selective anyway, but you need both columns for other queries and you want this one to ignore it: CREATE INDEX index_customs_records_on_month_and_bl_number ON customs_records (bl_number, month DESC); Either way, I bet you'll find that your other queries that use this index are also doing a backwards index scan, which will always be slower by about two orders of magnitude, since backwards reads act basically like random reads. The effect you're getting is clearly exaggerated, and I've run into it on occasion for effectively the entire history of PostgreSQL. Normally increasing the statistics on the affected columns and re-analyzing fixes it, but on a composite index, that won't necessarily be the case. -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 800 | Chicago IL, 60604 312-676-8870 sthomas@peak6.com ______________________________________________ See http://www.peak6.com/email_disclaimer.php for terms and conditions related to this email