Skip site navigation (1) Skip section navigation (2)

Re: how to get the total number of records in report

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: AI Rumman <rummandba(at)gmail(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: how to get the total number of records in report
Date: 2010-10-28 17:40:02
Message-ID: AANLkTinBDTWRycvoAWTn0xpmx5ePrVNctWTVcp072Fia@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-performance
On Thu, Oct 28, 2010 at 1:05 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> On Tue, Oct 19, 2010 at 7:56 PM, Merlin Moncure <mmoncure(at)gmail(dot)com> wrote:
>> On Mon, Oct 18, 2010 at 1:16 AM, AI Rumman <rummandba(at)gmail(dot)com> wrote:
>>> At present for reporting I use following types of query:
>>> select crm.*, crm_cnt.cnt
>>> from crm,
>>> (select count(*) as cnt from crm) crm_cnt;
>>> Here count query is used to find the total number of records.
>>> Same FROM clause is copied in both the part of the query.
>>> Is there any other good alternative way to get this similar value?
>>
>> Probably the best way to do this type of thing is handle it on the
>> client.  However, if you want to do it this way and your from clause
>> is more complex than 'from table', you can possibly improve on this
>> with a CTE:
>>
>> with q as (select * from <something expensive>)
>> select q.* q_cnt.cnt from q, (select count(*) as cnt from q) q_cnt;
>>
>> The advantage here is that the CTE is materialized without having to
>> do the whole query again.  This can be win or loss depending on the
>> query.
>
> What about
>
> select crm.*, sum(1) over () as crm_count from crm limit 10;

Hm, after a few quick tests it seems your approach is better in just
about every respect :-).

merlin

In response to

pgsql-performance by date

Next:From: AI RummanDate: 2010-10-28 17:49:25
Subject: Re: how to get the total number of records in report
Previous:From: Joshua D. DrakeDate: 2010-10-28 17:31:32
Subject: Re: partitioning question 1

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group