Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8zXX-0008IU-2u for pgsql-performance@arkaria.postgresql.org; Fri, 22 Feb 2013 20:48:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U8zXV-0007if-Ma for pgsql-performance@arkaria.postgresql.org; Fri, 22 Feb 2013 20:48:17 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8zXE-0007UC-K7 for pgsql-performance@postgresql.org; Fri, 22 Feb 2013 20:48:00 +0000 Received: from nm31.bullet.mail.bf1.yahoo.com ([72.30.239.198]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8zXC-0007bW-GO for pgsql-performance@postgresql.org; Fri, 22 Feb 2013 20:47:59 +0000 Received: from [98.139.212.147] by nm31.bullet.mail.bf1.yahoo.com with NNFMP; 22 Feb 2013 20:47:56 -0000 Received: from [98.139.212.222] by tm4.bullet.mail.bf1.yahoo.com with NNFMP; 22 Feb 2013 20:47:56 -0000 Received: from [127.0.0.1] by omp1031.mail.bf1.yahoo.com with NNFMP; 22 Feb 2013 20:47:56 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 852375.50515.bm@omp1031.mail.bf1.yahoo.com Received: (qmail 19209 invoked by uid 60001); 22 Feb 2013 20:47:56 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s1024; t=1361566076; bh=qoIZjlxJz4f5jvv1E6Kz3SzHKI2OCh1lUgHwDF6rhBg=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=rF7/DQFIOo18auZ9yPa+IXkeUfWKu0w7XA0N7By+a3DI8UAzokL1RSjAKaIVRs9oJTAk3VJrbQEEg771JV5umnwR9OLyboaIQrLl5CYzNprF9S8cKVhv2RQHYXFssQ+3PzrvVev64b9mEDsMFfe0ACdfyDX+OfHteCCsjKjgFpo= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=ymail.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=Mcd68a/6ksVNHWALbCBwMpHM/tb2bqDFxPZcdHYpEBM+Yp412DEN4SuJSU9DB+S9f8QS4ZHhlDxtoiNvY22XU1nlS2p3kmk4Y2xElp4KAFGYsYsGJSpjaWxIy+WD5wQ4Q3bUXRlH2vgSMLMtzYL4CGnxgMLETjmDFz6sg4eA5HI=; X-YMail-OSG: e3uxzG4VM1mcxfforgnnTLRj7HczmVOs5R9iUxiIOOkMefC 9h6nCBgJhH8zT_9mhxuYlfPhQxmi1FgtgFuEpypnp5GqJy_TpHQ2DMZte.qU pAqICCZmVowFzqVTu8k8VK9snLAieysYATLIz0f8SsnGvcSWJmFaQBVdzVZA boPBmoGeK_jMiaI68s_FmekOnlFoNh3zQSq4Y0s3Tib0Ps9hipyPymvHl_fP JbrO_FAr3b9Rxupx6rB2EWhse6x8rwpUdc0h0V39VkpFFrS90CSAaJeMiZhv fj3ky8ZAFQc55te3B0ttXQJDh1vliXZU5gy41BKirk3m0yPp2lfLnTpQCNM6 OCUvoMhihH9MEyJM6lQeyPMu4vI8jIulKBMbeWAZL1AAdvN.RMLzZp1WUvkP 8Dsnpyy0I9hm8sr_WV.khj8Oe6vaoVIfhJIohM1GhKNd8OerKqKRuQNGNH2p X58xBBXDvK6zVOSSUko2ywK4MSs9FwlDT7IBVilF3IOe9LAlp_GD8MY0P7i7 vnws7P9R0XOgK5JBy_NYqmqkkoox3J1vyMdd2J3vrsjWwsaXWXGpS2w0xc4Z qDhpq8RA8qeNReYmvv2Y0 Received: from [76.255.18.237] by web162905.mail.bf1.yahoo.com via HTTP; Fri, 22 Feb 2013 12:47:56 PST X-Rocket-MIMEInfo: 001.001, QWxleGFuZGVyIFN0YXVibyA8YWxleEBiZW5nbGVyLm5vPiB3cm90ZToKCj4gVGhpcyBpcyBteSBzY2hlbWE6Cj4KPsKgwqAgY3JlYXRlIHRhYmxlIGNvbW1lbnRzICgKPsKgwqDCoMKgIGlkIHNlcmlhbCBwcmltYXJ5IGtleSwKPsKgwqDCoMKgIGNvbnZlcnNhdGlvbl9pZCBpbnRlZ2VyLAo.wqDCoMKgwqAgY3JlYXRlZF9hdCB0aW1lc3RhbXAKPsKgwqAgKTsKPsKgwqAgY3JlYXRlIGluZGV4IGNvbW1lbnRzX2NvbnZlcnNhdGlvbl9pZF9pbmRleCBvbiBjb21tZW50cyAoY29udmVyc2F0aW9uX2lkKTsKPsKgwqABMAEBAQE- X-Mailer: YahooMailWebService/0.8.134.513 References: <19B2B4ECC4724A438200C54257BA8B20@gmail.com> Message-ID: <1361566076.10158.YahooMailNeo@web162905.mail.bf1.yahoo.com> Date: Fri, 22 Feb 2013 12:47:56 -0800 (PST) From: Kevin Grittner Reply-To: Kevin Grittner Subject: Re: Bad query plan with high-cardinality column To: Alexander Staubo , "pgsql-performance@postgresql.org" In-Reply-To: <19B2B4ECC4724A438200C54257BA8B20@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -1.9 (-) 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 Alexander Staubo wrote: > This is my schema: > >=A0=A0 create table comments ( >=A0=A0=A0=A0 id serial primary key, >=A0=A0=A0=A0 conversation_id integer, >=A0=A0=A0=A0 created_at timestamp >=A0=A0 ); >=A0=A0 create index comments_conversation_id_index on comments (conversati= on_id); >=A0=A0 create index comments_created_at_index on comments (created_at); I suspect you would be better off without those two indexes, and instead having an index on (conversation_id, created_at).=A0 Not just for the query you show, but in general. >=A0=A0 select comments.id from comments where >=A0=A0=A0=A0 conversation_id =3D 3975979 order by created_at limit 13 > > This filters about 5000 rows and returns the oldest 13 rows. But > the query is consistently planned wrong: > [planner thinks it will be cheaper to read index in ORDER BY > sequence and filter rows until it has 13 than to read 5471 rows > and sort them to pick the top 13 after the sort.] In my experience these problems come largely from the planner not knowing the cost of dealing with each tuple.=A0 I see a lot less of this if I raise cpu_tuple_cost to something in the 0.03 to 0.05 range. --=20 Kevin Grittner EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance