X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id F3D37DAB20 for ; Tue, 25 Oct 2005 07:10:23 -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 63263-08 for ; Tue, 25 Oct 2005 10:10:20 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 92C03DA9E5 for ; Tue, 25 Oct 2005 07:10:21 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id AE51B415C17; Tue, 25 Oct 2005 11:10:15 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id EC23315EDA; Tue, 25 Oct 2005 11:07:32 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 05323-01; Tue, 25 Oct 2005 11:07:28 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id A0FB015ED5; Tue, 25 Oct 2005 11:07:28 +0100 (BST) Message-ID: <435E03E0.2040107@archonet.com> Date: Tue, 25 Oct 2005 11:07:28 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051013) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Markus Wollny Cc: pgsql-performance@postgresql.org Subject: Re: Strange planner decision on quite simple select References: <2266D0630E43BB4290742247C891057508383E8E@dozer.computec.de> In-Reply-To: <2266D0630E43BB4290742247C891057508383E8E@dozer.computec.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.059 required=5 tests=[AWL=0.009, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200510/354 X-Sequence-Number: 15122 Markus Wollny wrote: > Hello! > > I've got a table BOARD_MESSAGE (message_id int8, thread_id int8, ...) > with pk on message_id and and a non_unique not_null index on thread_id. > A count(*) on BOARD_MESSAGE currently yields a total of 1231171 rows, > the planner estimated a total of 1232530 rows in this table. I've got > pg_autovacuum running on the database and run an additional nightly > VACUUM ANALYZE over it every night. > > I've got a few queries of the following type: > > select * > from PUBLIC.BOARD_MESSAGE > where THREAD_ID = 3354253 > order by MESSAGE_ID asc > limit 20 > offset 0; > > > There are currently roughly 4500 rows with this thread_id in > BOARD_MESSAGE. Explain-output is like so: > > QUERY PLAN > > ------------------------------------------------------------------------ > ---------------------------------------------- > Limit (cost=0.00..3927.22 rows=20 width=1148) > -> Index Scan using pk_board_message on board_message > (cost=0.00..1100800.55 rows=5606 width=1148) > Filter: (thread_id = 3354253) > (3 rows) > > I didn't have the patience to actually complete an explain analyze on > that one - I cancelled the query on several attempts after more than 40 > minutes runtime. Now I fiddled a little with this statement and tried > nudging the planner in the right direction like so: Hmm - it shouldn't take that long. If I'm reading this right, it's expecting to have to fetch 5606 rows to match thread_id=3354253 the 20 times you've asked for. Now, what it probably doesn't know is that thread_id is correlated with message_id quite highly (actually, I don't know that, I'm guessing). So - it starts at message_id=1 and works along, but I'm figuring that it needs to reach message_id's in the 3-4 million range to see any of the required thread. Suggestions: 1. Try "ORDER BY thread_id,message_id" and see if that nudges things your way. 2. Keep #1 and try replacing the index on (thread_id) with (thread_id,message_id) -- Richard Huxton Archonet Ltd