Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query?

Поиск
Список
Период
Сортировка
От David Rowley
Тема Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query?
Дата
Msg-id CAApHDvrmpeqM0k2teGsy2ep24EqEsFzbGQsTgy44dN3qwnHRtw@mail.gmail.com
обсуждение исходный текст
Ответ на Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query?  ("Walter Dörwald" <walter@livinglogic.de>)
Список pgsql-general
On Tue, 16 Aug 2022 at 21:15, Walter Dörwald <walter@livinglogic.de> wrote:
> select count(*) over (), e.* from email.email e;

Depending on the complexity of the query, putting the count(*) as a
subquery in the SELECT clause might execute more quickly. i.e. select
(select count(*) from email.email) c, * from email.emails; A
non-correlated subquery will become an "initplan", which will execute
only once.

The reason the window aggregate might be slower is due to the fact
that internally PostgreSQL will store tuples in the window frame in a
tuplestore.  In this example, all rows will be put in that tuple store
at once. These tuple stores will spill to disk when they exceed
work_mem.  On the other hand, the window aggregate version could be
faster if fetching each tuple from the outer query was sufficiently
more expensive than storing it in a tuple store. That could easily
happen if the query contained expensive joins or many rows were
filtered out. That could be more expensive as the subquery version
would have to do that twice whereas the window agg version would only
have to do that once.

David



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

Предыдущее
От: "Walter Dörwald"
Дата:
Сообщение: Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query?
Следующее
От: hubert depesz lubaczewski
Дата:
Сообщение: Would it be possible to add functions to tab-completion in psql?