Re: count is ten times faster

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Sabin Coanda" <coanda(at)hotpop(dot)com>, <pgsql-performance(at)postgresql(dot)org>
Subject: Re: count is ten times faster
Date: 2010-04-14 14:16:16
Message-ID: 4BC587E00200002500030859@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

"Sabin Coanda" <coanda(at)hotpop(dot)com> wrote:

> How do you explain the cost is about ten times lower in the 2nd
> query than the first ?

To elaborate on Pierre's answer:

In the first query, you scan the entire table and execute the "f1"
function on each row. In the second query you pass the entire table
just counting visible tuples and then run the "f1" function once,
and use the resulting value to scan an index on which it expects to
find one row.

It estimates the cost of running the "f1" function 7.7 million times
as being roughly ten times the cost of scanning the table. Now,
this is all just estimates; if they don't really reflect the
relative cost of *running* the two queries, you might want to adjust
costs factors -- perhaps the estimated cost of the "f1" function.

-Kevin

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Kevin Grittner 2010-04-14 14:31:33 Re: significant slow down with various LIMIT
Previous Message Kevin Grittner 2010-04-14 13:50:10 Re: PostgreSQL with Zabbix - problem of newbe