Re: Is it possible to speed this query up?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: arnaulist(at)andromeiberica(dot)com
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Is it possible to speed this query up?
Date: 2006-07-26 20:24:47
Message-ID: 17741.1153945487@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Arnau <arnaulist(at)andromeiberica(dot)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

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Eliott 2006-07-27 14:23:28 performance issue with a specific query
Previous Message Arnau 2006-07-26 17:01:33 Is it possible to speed this query up?