Re: Optimizing select count query which often takes over 10 seconds

From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Optimizing select count query which often takes over 10 seconds
Date: 2013-01-25 18:42:38
Message-ID: CAMkU=1y8_Fkepww6tPVs4gs1EtcCUoWuA14rxkbCz2O4Q1DWTA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jan 24, 2013 at 1:57 AM, Alexander Farber
<alexander(dot)farber(at)gmail(dot)com> wrote:
> Hello,
>
> for a PostgreSQL 8.4.13 database + pgbouncer

Using 8.4 is really going to limit your options.

...

>
> LOG: duration: 12590.394 ms statement:
> select count(id) from (
> select id,
> row_number() over(partition by yw order by money
> desc) as ranking
> from pref_money
> ) x
> where x.ranking = 1 and id='OK471018960997'

Since you only care about ranking=1, it might be better to rewrite
that using something like:

where money = (select max(money....)

But, I doubt it. I don't think even the 9.2 planner has the smarts to
do what you want efficiently. It might be possible to make it do it
efficiently using a recursive query, once you have the index on
(yw,money).

> This command comes from a PHP-script
> of mine which displays "medals" on
> a player profile page - meaning how many
> times she won a weekly tournament:
...
>
> Does anybody please have an idea
> how could I optimize it or should
> I introduce a hourly job and a "medals"
> column (that would make my players
> stats less "live")?

This sounds like a good idea. But if the tournament is weekly why
would the job have to be hourly? Why do the results of a weekly
tournament need to be 'live'?

Cheers,

Jeff

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2013-01-25 20:35:41 Re: main.log file not being updated
Previous Message Paul Jones 2013-01-25 18:24:45 Can LC_TIME affect timestamp input?