pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedRe: ***SPAM*** Re: Can I get the number of results plus the results with a single query?
4+ messages / 4 participants
[nested] [flat]
* Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query?
@ 2022-08-15 20:37 Perry Smith <pedz@easesoftware.com>
0 siblings, 1 reply; 4+ messages in thread
From: Perry Smith @ 2022-08-15 20:37 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
> On Aug 15, 2022, at 08:55, David G. Johnston <david.g.johnston@gmail.com> wrote:
>
> On Monday, August 15, 2022, Perry Smith <pedz@easesoftware.com <mailto:pedz@easesoftware.com>> wrote:
> I’ve been toying with row_number() and then sort by row_number descending and pick off the first row as the total number.
>
> Use count as a window function.
I see others are commenting after David’s update so:
Thank you David.
This seems to work for me:
SELECT count(*) OVER (), id, basename, sha1 FROM dateien WHERE (lower(ext) in ( 'pxd' ) and ftype = 'file') ORDER BY sha1;
This has, e.g. 73, in the first column for all of the rows.
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query?
@ 2022-08-15 22:13 Rob Sargent <robjsargent@gmail.com>
parent: Perry Smith <pedz@easesoftware.com>
0 siblings, 1 reply; 4+ messages in thread
From: Rob Sargent @ 2022-08-15 22:13 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 8/15/22 14:37, Perry Smith wrote:
>
>
>> On Aug 15, 2022, at 08:55, David G. Johnston
>> <david.g.johnston@gmail.com> wrote:
>>
>> On Monday, August 15, 2022, Perry Smith <pedz@easesoftware.com> wrote:
>>
>> I’ve been toying with row_number() and then sort by row_number
>> descending and pick off the first row as the total number.
>>
>>
>> Use count as a window function.
>
> I see others are commenting after David’s update so:
>
> Thank you David.
>
> This seems to work for me:
>
> SELECT count(*) OVER (), id, basename, sha1 FROM dateien WHERE
> (lower(ext) in ( 'pxd' ) and ftype = 'file') ORDER BY sha1;
>
>
> This has, e.g. 73, in the first column for all of the rows.
>
Any comparative timing statistics on that? Especially on more than 73
records returned, because with that few just grab them all and get
size() or length of what ever collection mechanism you're playing with.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query?
@ 2022-08-16 09:15 Walter Dörwald <walter@livinglogic.de>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 4+ messages in thread
From: Walter Dörwald @ 2022-08-16 09:15 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
On 16 Aug 2022, at 0:13, Rob Sargent wrote:
> On 8/15/22 14:37, Perry Smith wrote:
>>
>>
>>> On Aug 15, 2022, at 08:55, David G. Johnston
>>> <david.g.johnston@gmail.com> wrote:
>>>
>>> On Monday, August 15, 2022, Perry Smith <pedz@easesoftware.com>
>>> wrote:
>>>
>>> I’ve been toying with row_number() and then sort by row_number
>>> descending and pick off the first row as the total number.
>>>
>>>
>>> Use count as a window function.
>>
>> I see others are commenting after David’s update so:
>>
>> Thank you David.
>>
>> This seems to work for me:
>>
>> SELECT count(*) OVER (), id, basename, sha1 FROM dateien WHERE
>> (lower(ext) in ( 'pxd' ) and ftype = 'file') ORDER BY sha1;
>>
>>
>> This has, e.g. 73, in the first column for all of the rows.
>>
> Any comparative timing statistics on that? Especially on more than
> 73 records returned, because with that few just grab them all and get
> size() or length of what ever collection mechanism you're playing
> with.
I tried with a larger table (739951 records):
```
select e.* from email.email e;
```
takes 50 seconds (as displayed by TablePlus).
```
select count(*) over (), e.* from email.email e;
```
takes 58 seconds.
And doing `select count(*) from email.email e;` takes 2-3 seconds.
Note that in this example the records where fetched over the internet
(i.e. not from a local Postgres installation) and there is no `where`
condition that must be evaluated repeatedly, so other variants might
give better numbers.
Servus,
Walter
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query?
@ 2022-08-16 09:58 David Rowley <dgrowleyml@gmail.com>
parent: Walter Dörwald <walter@livinglogic.de>
0 siblings, 0 replies; 4+ messages in thread
From: David Rowley @ 2022-08-16 09:58 UTC (permalink / raw)
To: Walter Dörwald <walter@livinglogic.de>; +Cc: Rob Sargent <robjsargent@gmail.com>; pgsql-general@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2022-08-16 09:58 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-08-15 20:37 Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query? Perry Smith <pedz@easesoftware.com>
2022-08-15 22:13 ` Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query? Rob Sargent <robjsargent@gmail.com>
2022-08-16 09:15 ` Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query? Walter Dörwald <walter@livinglogic.de>
2022-08-16 09:58 ` Re: ***SPAM*** Re: Can I get the number of results plus the results with a single query? David Rowley <dgrowleyml@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox