pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: kouber@saparev.com
Cc: pgsql-performance@postgresql.org
Subject: Re: LIMIT confuses the planner
Date: Mon, 23 Feb 2009 13:27:44 +0000
Message-ID: <49A2A450.20002@archonet.com> (raw)
In-Reply-To: <49A295F0.5010405@saparev.com>
References: <49A295F0.5010405@saparev.com>

Kouber Saparev wrote:

> db=# EXPLAIN ANALYZE
> SELECT
>   *
> FROM
>   login_attempt
> WHERE
>   username='kouber'
> ORDER BY
>   login_attempt_sid DESC;
> 
> QUERY PLAN
> ------------------------------------------------------------------------------------------------------------------------------------------------------
> 
>  Sort  (cost=1415.15..1434.93 rows=7914 width=38) (actual
> time=0.103..0.104 rows=2 loops=1)
>    Sort Key: login_attempt_sid
>    Sort Method:  quicksort  Memory: 25kB
>    ->  Index Scan using login_attempt_username_idx on login_attempt
> (cost=0.00..902.71 rows=7914 width=38) (actual time=0.090..0.091 rows=2
> loops=1)
>          Index Cond: ((username)::text = 'kouber'::text)
>  Total runtime: 0.140 ms

It's expecting 7914 rows returned and is getting only 2. That is
probably the root of the problem.

> However when I add a LIMIT clause to the same query the planner no
> longer uses the right index, hence the query becomes very slow:
> 
> 
> db=# EXPLAIN ANALYZE
> SELECT
>   *
> FROM
>   login_attempt
> WHERE
>   username='kouber'
> ORDER BY
>   login_attempt_sid DESC LIMIT 20;

Since it's expecting 7914 rows for "kouber" it thinks it will find the
20 rows you want fairly quickly by just looking backward through the
login_attempt_pkey index.

Try increasing the stats on the username column.

ALTER TABLE login_attempt ALTER COLUMN username SET STATISTICS 100;
ANALYZE login_attempt;

You can try different values of statistics up to 1000, but there's no
point in setting it too high.

-- 
  Richard Huxton
  Archonet Ltd



view thread (14+ messages)  latest in thread

Message-ID: <49A2A450.20002@archonet.com>
Permalink:  ../49A2A450.20002@archonet.com/
Also on:    postgresql.org/message-id/49A2A450.20002@archonet.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: dev@archonet.com, kouber@saparev.com
  Subject: Re: LIMIT confuses the planner
  In-Reply-To: <49A2A450.20002@archonet.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