Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id A5E062E0284 for ; Thu, 6 Mar 2008 02:29:00 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 08461-03 for ; Thu, 6 Mar 2008 02:28:57 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from ironport2-out.pppoe.ca (ironport2-out.pppoe.ca [206.248.154.182]) by postgresql.org (Postfix) with ESMTP id A0D0B2E027C for ; Thu, 6 Mar 2008 02:28:57 -0400 (AST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ah4FADcez0fO+Igp/2dsb2JhbACCOjSKVJ4SgQE X-IronPort-AV: E=Sophos;i="4.25,454,1199682000"; d="scan'208,217";a="15646769" Received: from smtp.pppoe.ca ([65.39.196.238]) by ironport2-out.pppoe.ca with ESMTP; 06 Mar 2008 01:28:55 -0500 Received: from [206.248.136.41] ([206.248.136.41]) by smtp.pppoe.ca (Internet Mail Server v1.0) with ESMTP id MJI55956; Thu, 06 Mar 2008 01:28:56 -0500 Message-ID: <47CF8EA6.8070503@mark.mielke.cc> Date: Thu, 06 Mar 2008 01:26:46 -0500 From: Mark Mielke User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 To: Shoaib Mir CC: "A. Kretschmer" , pgsql-performance@postgresql.org Subject: Re: count * performance issue References: <20080306060829.GA21084@a-kretschmer.de> In-Reply-To: Content-Type: multipart/alternative; boundary="------------050908040209060106060701" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/95 X-Sequence-Number: 29049 This is a multi-part message in MIME format. --------------050908040209060106060701 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit > > There aren't a general solution. If you realy need the exact count of > tuples than you can play with a TRIGGER and increase/decrease the > tuple-count for this table in an extra table. > Of course, this means accepting the cost of obtaining update locks on the count table. The original poster should understand that they can either get a fast estimated count, or they can get a slow accurate count (either slow in terms of select using count(*) or slow in terms of updates using triggers and locking). Other systems have their own issues. An index scan may be faster than a table scan for databases that can accurately determine counts using only the index, but it's still a relatively slow operation, and people don't normally need an accurate count for records in the range of 100,000+? :-) Cheers, mark -- Mark Mielke --------------050908040209060106060701 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit  
There aren't a general solution. If you realy need the exact count of
tuples than you can play with a TRIGGER and increase/decrease the
tuple-count for this table in an extra table.

Of course, this means accepting the cost of obtaining update locks on the count table.

The original poster should understand that they can either get a fast estimated count, or they can get a slow accurate count (either slow in terms of select using count(*) or slow in terms of updates using triggers and locking).

Other systems have their own issues. An index scan may be faster than a table scan for databases that can accurately determine counts using only the index, but it's still a relatively slow operation, and people don't normally need an accurate count for records in the range of 100,000+? :-)

Cheers,
mark

-- 
Mark Mielke <mark@mielke.cc>
--------------050908040209060106060701--