Re: Is it possible to speed this query up? - Mailing list pgsql-performance

From Tom Lane
Subject Re: Is it possible to speed this query up?
Date
Msg-id 17741.1153945487@sss.pgh.pa.us
Whole thread Raw
In response to Is it possible to speed this query up?  (Arnau <arnaulist@andromeiberica.com>)
List pgsql-performance
Arnau <arnaulist@andromeiberica.com> writes:
> the explain analyze shouts the following:

The expensive part appears to be this indexscan:

>                       ->  Index Scan using pk_agndusrgrp_usergroup on
> agenda_users_groups ug  (cost=0.00..123740.26 rows=2936058 width=30)
> (actual time=0.101..61921.260 rows=2836638 loops=1)

Since there's no index condition, the planner is evidently using this
scan just to obtain sort order.  I think ordinarily it would use a
seqscan and then sort the final result, which'd be a lot faster if the
whole result were being selected.  But you have a LIMIT and it's
mistakenly guessing that only a small part of the table will need to be
scanned before the LIMIT is satisfied.

Bottom line: try dropping the LIMIT.  If you really need the limit to be
enforced on the SQL side, you could try declaring the query as a cursor
and only fetching 150 rows from it.

            regards, tom lane

pgsql-performance by date:

Previous
From: Arnau
Date:
Subject: Is it possible to speed this query up?
Next
From: Eliott
Date:
Subject: performance issue with a specific query