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

Поиск
Список
Период
Сортировка
От Peter J. Holzer
Тема Re: Can I get the number of results plus the results with a single query?
Дата
Msg-id 20220815181618.bkpg7jwf546qlvlb@hjp.at
обсуждение исходный текст
Ответ на Can I get the number of results plus the results with a single query?  (Perry Smith <pedz@easesoftware.com>)
Ответы Re: Can I get the number of results plus the results with a single query?  (Bryn Llewellyn <bryn@yugabyte.com>)
Список pgsql-general
On 2022-08-15 08:49:33 -0500, Perry Smith wrote:
> I like to have what I call “baby sitting” messages such as “Completed
> 15 out of 1023”.  To do this, I need the number of results a query
> returns but I also need the results.
>
> Currently I’m doing this with two queries such as:
>
>     SELECT COUNT(*) FROM table WHERE …. expression …
>     SELECT * FROM table WHERE …. expression …
>
> But this requires two queries.

And unless evaluating/transmitting the select expressions is very
expensive the first one is unlikely to be much faster than the second.
So you've just halved your performance.

> Is there a way to do the same thing with just one query somehow?

I don't think there can be. You don't know how many results there will
be until you have evaluated the query, but when you have done that you
already have all the results so displaying the baby sitting messages or
a progress bar becomes pointless.


> I’ve been toying with row_number() and then sort by row_number
> descending and pick off the first row as the total number.

I think that this will usually result in a long pause at the start
(while the query is evaluated) followed by a very quick count up (while
the result is transmitted to the client). Probably not what you want.

        hp


--
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Вложения

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

Предыдущее
От: "David G. Johnston"
Дата:
Сообщение: Re: lateral join with union all
Следующее
От: Bryn Llewellyn
Дата:
Сообщение: Re: Can I get the number of results plus the results with a single query?