Re: LIMIT confuses the planner

From: Richard Huxton <dev(at)archonet(dot)com>
To: kouber(at)saparev(dot)com
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: LIMIT confuses the planner
Date: 2009-02-23 13:27:44
Message-ID: 49A2A450.20002@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

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

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Robert Haas 2009-02-23 14:53:41 Re: LIMIT confuses the planner
Previous Message Kouber Saparev 2009-02-23 12:26:24 LIMIT confuses the planner