Re: Approach to extract top records from table based upon aggregate

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: droberts <david(dot)roberts(at)riverbed(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Approach to extract top records from table based upon aggregate
Date: 2015-11-02 22:22:31
Message-ID: CAKFQuwa4Dofum1iWrHJjhb-THmugn+xy0fVPLGcnfsTZ5RbEFw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Nov 2, 2015 at 3:14 PM, droberts <david(dot)roberts(at)riverbed(dot)com> wrote:

> Hi, I have a table that contains call records. I'm looking to get only
> records for users who made the most calls over a particular time duration
> in
> an efficient way.
>
> calls()
>
> time, duration, caller_number, dialed_number
>
>
>
> -- query to get top 10 callers
> select caller_number, count(1) from calls group by caller_number order by
> calls desc limit 10
>
> --my current query to get those callers
>
> select * from call where caller_number in (above query)
>
>
> It works but I was hoping for something a little more efficient if anyone
> has an idea.
>

​I don't think there is anything that is "a little more efficient"
(implying, only a bit harder to implement).

You can probably get significantly faster by combining various forms of
pre-computation and caching. It is likewise significantly more complex to
implement.

​David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Janes 2015-11-02 22:28:25 Re: Is there bigintarray?
Previous Message droberts 2015-11-02 22:14:09 Approach to extract top records from table based upon aggregate