From: Sabin Coanda <coanda@hotpop.com>
To: pgsql-performance@postgresql.org
Subject: count is ten times faster
Date: Tue, 13 Apr 2010 15:32:57 +0300
Message-ID: <hq1ocv$2mea$1@news.hub.org> (raw)
Hi there,
I have a simple aggregate query: SELECT count("PK_ID") AS "b1" FROM "tbA"
WHERE "PK_ID" > "f1"( 'c1' ), which has the following execution plan:
"Aggregate (cost=2156915.42..2156915.43 rows=1 width=4)"
" -> Seq Scan on "tbA" (cost=0.00..2137634.36 rows=7712423 width=4)"
" Filter: ("PK_ID" > "f1"('c1'::character varying))"
I tried to get the same result with the following query:
SELECT (
SELECT count("PK_ID") AS "b1" FROM "tbA" ) -
(
SELECT count("PK_ID") AS "b1"
FROM "tbA"
WHERE "PK_ID" <= "f1"( 'c1' )
)
with the execution plan:
"Result (cost=248952.95..248952.96 rows=1 width=0)"
" InitPlan"
" -> Aggregate (cost=184772.11..184772.12 rows=1 width=4)"
" -> Seq Scan on "tbA" (cost=0.00..165243.49 rows=7811449
width=4)"
" -> Aggregate (cost=64180.81..64180.82 rows=1 width=4)"
" -> Index Scan using "tbA_pkey" on "tbA" (cost=0.25..63933.24
rows=99026 width=4)"
" Index Cond: ("PK_ID" <= "f1"('c1'::character varying))"
How do you explain the cost is about ten times lower in the 2nd query than
the first ?
TIA,
Sabin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: coanda@hotpop.com
Subject: Re: count is ten times faster
In-Reply-To: <hq1ocv$2mea$1@news.hub.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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