Re: CURSOR slowes down a WHERE clause 100 times?

Поиск
Список
Период
Сортировка
От Richard Huxton
Тема Re: CURSOR slowes down a WHERE clause 100 times?
Дата
Msg-id 42CCF28A.3010403@archonet.com
обсуждение исходный текст
Ответ на Re: CURSOR slowes down a WHERE clause 100 times?  (Niccolo Rigacci <niccolo@rigacci.org>)
Ответы Re: CURSOR slowes down a WHERE clause 100 times?
Список pgsql-performance
Niccolo Rigacci wrote:
>>
>>I get the results in about 108 seconds (8060 rows).
>>
>>If I issue the SELECT alone (without the CURSOR) I get the
>>same results in less than 1 second.
>
>
> By trial and error I discovered that adding an "ORDER BY
> toponimo" clause to the SELECT, boosts the CURSOR performances
> so that they are now equiparable to the SELECT alone.
>
> Is there some documentation on how an ORDER can affect the
> CURSOR in a different way than the SELECT?

I think you're misunderstanding exactly what's happening here. If you
ask for a cursor, PG assumes you aren't going to want all the results
(or at least not straight away). After all, most people use them to work
through results in comparatively small chunks, perhaps only ever
fetching 1% of the total results.

So - if you ask for a cursor, PG weights things to give you the first
few rows as soon as possible, at the expense of fetching *all* rows
quickly. If you're only going to fetch e.g. the first 20 rows this is
exactly what you want. In your case, since you're immediately issuing
FETCH ALL, you're not really using the cursor at all, but PG doesn't
know that.

So - the ORDER BY means PG has to sort all the results before returning
the first row anyway. That probably means the plans with/without cursor
are identical.

Of course, all this assumes that your configuration settings are good
and statistics adequate. To test that, try fetching just the first row
from your cursor with/without the ORDER BY. Without should be quicker.

--
   Richard Huxton
   Archonet Ltd

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

Предыдущее
От: Niccolo Rigacci
Дата:
Сообщение: Re: CURSOR slowes down a WHERE clause 100 times?
Следующее
От: Niccolo Rigacci
Дата:
Сообщение: Re: CURSOR slowes down a WHERE clause 100 times?