Pushing limit into subqueries of a union

Поиск
Список
Период
Сортировка
От Phil Endecott
Тема Pushing limit into subqueries of a union
Дата
Msg-id 42A8BCD9.1090307@chezphil.org
обсуждение исходный текст
Ответы Re: Pushing limit into subqueries of a union  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
Dear Experts,

Here is another "how can I rewrite this to go faster" idea.

I have two tables T1 and T2 and a view V that is the UNION ALL of T1 and
T2.  The tables have an editdate field, and I want to get the n most
recently changed rows:

   select * from V order by editdate desc limit 40;

This seems to unconditionally read the whole of T1 and T2, so it is slow.

T1 and T2 both have indexes on the editdate attribute, so if I write

(select * from T1 order by editdate desc limit 40)
union all (select * from T2 order by editdate desc limit 40)
order by editdate desc limit 40;

I get the same results, about 1000 times faster.

I presume that PostgreSQL doesn't try to push the limit clause into the
subqueries of a UNION ALL in this way.  I believe it is safe, isn't it?


Cheers,  Phil.




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

Предыдущее
От: Russ Brown
Дата:
Сообщение: Re: Version Control?
Следующее
От: David Siebert
Дата:
Сообщение: Now() function