Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XqsjK-0001Ai-18 for pgsql-performance@arkaria.postgresql.org; Wed, 19 Nov 2014 00:02:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XqsjJ-0006jG-BF for pgsql-performance@arkaria.postgresql.org; Wed, 19 Nov 2014 00:02:41 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XqsjI-0006jA-80 for pgsql-performance@postgresql.org; Wed, 19 Nov 2014 00:02:40 +0000 Received: from new1-smtp.messagingengine.com ([66.111.4.221]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XqsjE-0000fX-Kq for pgsql-performance@postgresql.org; Wed, 19 Nov 2014 00:02:38 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailnew.nyi.internal (Postfix) with ESMTP id 0C24F2F4C for ; Tue, 18 Nov 2014 19:02:35 -0500 (EST) Received: from web6 ([10.202.2.216]) by compute5.internal (MEProxy); Tue, 18 Nov 2014 19:02:35 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=yuriks.net; h= message-id:x-sasl-enc:from:to:mime-version :content-transfer-encoding:content-type:in-reply-to:references :subject:date; s=mesmtp; bh=dcga8NEtC4WbSvkrzT5PAmarKdA=; b=RM64 bKKljrNYAH0fltxmS3lLOF6Z+THa/MG/rm7zyFy9/75pgVb61CJJCLpapchoCNPP pB+aSHzDu0pBpwQPD68wL1Q8PxgKsLEeMFWY/G1YFfATO3jLirjnmNzcK6BgXeTY XcShWDhydG1eYSt70eo6MFa05H54wbjfzjsMXws= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:x-sasl-enc:from:to :mime-version:content-transfer-encoding:content-type:in-reply-to :references:subject:date; s=smtpout; bh=dcga8NEtC4WbSvkrzT5PAmar KdA=; b=lreIq7a93YI4vjjhhW6cEcr7+TL4o6ToGR7SvwStWZ+euiIW9g+UBozg gR8v05zQdDpogK1nrxCIAy4sn+6QGHsUQ99S6YsT1QleJdyXq0a6HuBSX6VANU0d zzbUbEOk0mpGPH1iGTFi8bnJakE6G2jZOmEgn7lxsNqX8FK4bFc= Received: by web6.nyi.internal (Postfix, from userid 99) id AAFCF4A2BE; Tue, 18 Nov 2014 19:02:34 -0500 (EST) Message-Id: <1416355354.465985.192652933.31058871@webmail.messagingengine.com> X-Sasl-Enc: xHTL3Ty7DjGsQXKezZeOLJTb7rUufHYWU2EIymNYf401 1416355354 From: Yuri Kunde Schlesner To: pgsql-performance@postgresql.org MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain X-Mailer: MessagingEngine.com Webmail Interface - ajax-9183bd94 In-Reply-To: <2542.1416158326@sss.pgh.pa.us> References: <1416093360.655916.191452721.03160196@webmail.messagingengine.com> <2542.1416158326@sss.pgh.pa.us> Subject: Re: Plan uses wrong index, preferring to scan pkey index instead Date: Tue, 18 Nov 2014 22:02:34 -0200 X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On Sun, Nov 16, 2014, at 03:18 PM, Tom Lane wrote: > I suspect that the reason the planner likes the backlog_pkey is that it's > almost perfectly correlated with table order, which greatly reduces the > number of table fetches that need to happen over the course of a > indexscan > compared to using the less-well-correlated bufferid+messageid index. > So that way is estimated to be cheaper than using the less-correlated > index ... and that may even be true except for outlier bufferid values > with no recent messages. Indeed, and I can imagine that this is more advantageous in the general case, as I described in my last message. The problem is that the variance is too high, with a 500x slowdown between the best and the worst cases for that plan. > What I think might be a workable solution, assuming you can stand a > little > downtime to do it, is to CLUSTER the table on the bufferid+messageid > index. This would reverse the correlation advantage and thereby solve > your problem. Now, ordinarily CLUSTER is only a temporary solution > because the cluster-induced ordering degrades over time. But I think it > would likely be a very long time until you accumulate so many new > messages > that the table as a whole looks well-correlated on messageid alone. I tried this and it seems to have solved my problem! The better plan is consistently chosen now, and it's as fast as former plan on the fast cases, and much faster on the slow case. I will continue monitoring the DB to see if it eventually switches back to the former scheme, and if it does I can just include a re-cluster on my maintenance schedule. Thanks so much for the suggestion. -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance