Re: Query with order by and limit is very slow - wrong index used

From: Marcin Mańk <marcin(dot)mank(at)gmail(dot)com>
To: Nowak Michał <michal(dot)nowak(at)me(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Query with order by and limit is very slow - wrong index used
Date: 2011-10-03 22:45:47
Message-ID: CAK61fk5=PKcymqBTQ6Cg4M5Yakr2xvmEsN=Uqq-UGDwYEzGVCQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

2011/10/3 Nowak Michał <michal(dot)nowak(at)me(dot)com>:

> Some info about data distrubution:
>
> a9-dev=> select min(id) from records;
>  min
> --------
>  190830
> (1 row)
>
> a9-dev=> select min(id), max(id) from records where source_id='http://ebuw.uw.edu.pl/dlibra/oai-pmh-repository.xml';
>   min   |   max
> ---------+---------
>  1105217 | 3811326
> (1 row)
> a9-dev=> select min(id), max(id) from records where source_id='http://www.wbc.poznan.pl/dlibra/oai-pmh-repository.xml';
>   min   |   max
> ---------+---------
>  1544991 | 3811413
> (1 row)

PG assumes that the "wbc.poznan.pl" rows are all over the range of
ids, which seems not to be the case. There is no sense of cross-column
correlation in the planner currently.

You are going to have to resort to some more or less cute hacks, like
making an index on (source_id, id - 1) and doing "... order by
source_id, id - 1" .

Greetings
Marcin Mańk

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Nowak Michał 2011-10-04 07:10:27 Re: Query with order by and limit is very slow - wrong index used
Previous Message Soporte @ TEKSOL S.A. 2011-10-03 17:48:10 pkey is not used on productive database