Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 29686632315 for ; Tue, 13 Apr 2010 09:37:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 62111-04 for ; Tue, 13 Apr 2010 12:36:52 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 3A07563267F for ; Tue, 13 Apr 2010 09:37:00 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.3/8.14.3) with ESMTP id o3DCasDQ089313 for ; Tue, 13 Apr 2010 09:36:54 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.3/8.14.3/Submit) id o3DCYebP088525 for pgsql-performance@postgresql.org; Tue, 13 Apr 2010 09:34:40 -0300 (ADT) (envelope-from news) From: "Sabin Coanda" X-Newsgroups: pgsql.performance Subject: count is ten times faster Date: Tue, 13 Apr 2010 15:32:57 +0300 Organization: Hub.Org Networking Services Lines: 34 Message-ID: X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5843 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-RFC2646: Format=Flowed; Original To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.79 tagged_above=-10 required=5 tests=BAYES_50=0.8, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201004/163 X-Sequence-Number: 38413 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