Re: [GENERAL] Avoid using index scan backward when limit order desc

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: [GENERAL] Avoid using index scan backward when limit order desc
Дата
Msg-id 1462.1482172785@sss.pgh.pa.us
обсуждение исходный текст
Ответ на [GENERAL] Avoid using index scan backward when limit order desc  (Christophe Escobar <christophe.esco@gmail.com>)
Список pgsql-general
Christophe Escobar <christophe.esco@gmail.com> writes:
> I have some performance issues when trying to fetch rows from the table
> with a specific query, I suspect the planner to choose the wrong index
> because of the limit.
> ...
> 1) Why is the planner changing index scanning at the threshold of 45 for
> the LIMIT ?

That's just where the crossover point happens to fall for the estimated
costs of the two plans.  Of course, the costs are based on the estimate
of 2061 rows matching the WHERE conditions, and since the true figure
is 0 rows, the cost estimates are bad :-(

> 2) Is it possible for a specific query to force the planner on choosing a
> given index or preventing it from choosing one ?

In this example I'd be inclined to prevent the ORDER BY from being
considered while choosing an index, which you can do with the traditional
optimization fence of OFFSET 0:

SELECT * FROM
  (SELECT * FROM notifications
   WHERE bucket_id IN (30231,30230,30104) AND type IN ('foo', 'bar')
   OFFSET 0) ss
ORDER BY created_at DESC limit 20;

Of course, if you have cases where the WHERE conditions will select
a large number of rows, this will prevent the planner from making
a wise choice in those cases --- the use of the created_at index isn't
inherently stupid, it all depends on how many rows match the WHERE.

Depending on how wedded you are to your current data representation,
it might be better to redesign the table so that you don't need to
test two independent columns to select the rows you care about.
That would improve the odds of getting a decent rowcount estimate.

            regards, tom lane


В списке pgsql-general по дате отправления:

Предыдущее
От: Adrian Klaver
Дата:
Сообщение: Re: [GENERAL] Windows installation - could not connect to server:Connection refused (0x0000274D/10061)
Следующее
От: John R Pierce
Дата:
Сообщение: Re: [GENERAL] Re: Re: [GENERAL] PostgreSQL mirroring from RPM installto Source install