pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <kgrittn@ymail.com>
To: Alexander Staubo <alex@bengler.no>
To: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: Bad query plan with high-cardinality column
Date: Fri, 22 Feb 2013 12:47:56 -0800 (PST)
Message-ID: <1361566076.10158.YahooMailNeo@web162905.mail.bf1.yahoo.com> (raw)
In-Reply-To: <19B2B4ECC4724A438200C54257BA8B20@gmail.com>
References: <19B2B4ECC4724A438200C54257BA8B20@gmail.com>
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>

Alexander Staubo <alex@bengler.no> wrote:

> This is my schema:
>
>   create table comments (
>     id serial primary key,
>     conversation_id integer,
>     created_at timestamp
>   );
>   create index comments_conversation_id_index on comments (conversation_id);
>   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).  Not just
for the query you show, but in general.

>   select comments.id from comments where
>     conversation_id = 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.  I see a lot less of
this if I raise cpu_tuple_cost to something in the 0.03 to 0.05
range.

-- 
Kevin Grittner
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



view thread (8+ messages)  latest in thread

Message-ID: <1361566076.10158.YahooMailNeo@web162905.mail.bf1.yahoo.com>
Permalink:  ../1361566076.10158.YahooMailNeo@web162905.mail.bf1.yahoo.com/
Also on:    postgresql.org/message-id/1361566076.10158.YahooMailNeo@web162905.mail.bf1.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: kgrittn@ymail.com, alex@bengler.no
  Subject: Re: Bad query plan with high-cardinality column
  In-Reply-To: <1361566076.10158.YahooMailNeo@web162905.mail.bf1.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox