X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 591D053CCE for ; Fri, 8 Apr 2005 20:39:55 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 23018-05 for ; Fri, 8 Apr 2005 19:39:52 +0000 (GMT) Received: from priv-edtnes51.telusplanet.net (outbound04.telus.net [199.185.220.223]) by svr1.postgresql.org (Postfix) with ESMTP id F01A153BF4 for ; Fri, 8 Apr 2005 20:39:49 +0100 (BST) Received: from localhost ([199.185.220.240]) by priv-edtnes51.telusplanet.net (InterMail vM.6.01.04.00 201-2131-118-20041027) with ESMTP id <20050408193949.WNOI4275.priv-edtnes51.telusplanet.net@localhost>; Fri, 8 Apr 2005 13:39:49 -0600 Received: from 209.17.183.249 ( [209.17.183.249]) as user a3a18850@192.168.200.1 by webmail.telus.net with HTTP; Fri, 8 Apr 2005 12:39:49 -0700 Message-ID: <1112989189.4256de0583b33@webmail.telus.net> Date: Fri, 8 Apr 2005 12:39:49 -0700 From: Mischa Sandberg To: Scott Marlowe Cc: pgsql-sql@postgresql.org Subject: Re: getting count for a specific querry References: <000001c53c5d$9f07f0d0$797ba8c0@jfradkin> <1112983840.20921.77.camel@state.g2switchworks.com> In-Reply-To: <1112983840.20921.77.camel@state.g2switchworks.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.1-cvs X-Originating-IP: 209.17.183.249 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.082 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_POST, FORGED_RCVD_HELO X-Spam-Level: * X-Archive-Number: 200504/116 X-Sequence-Number: 21009 Quoting Scott Marlowe : > On Fri, 2005-04-08 at 12:08, Joel Fradkin wrote: > > I might have to add a button to do the count on command so they don't get > > the hit. > > I would want it to return the count of the condition, not the currently > > displayed number of rows. > > Judging postgresql on one single data point (count(*) performance) is > quite unfair. Unless your system only operates on static data and is > used to mostly do things like counting, in which case, why are you using > a database? For the general discussion of slowness of count(*), and given no entry on the subject in http://www.postgresql.org/docs/faqs.FAQ.html ... I guess everyone has to be pointed at: http://archives.postgresql.org/pgsql-hackers/2005-01/msg00247.php However, the gist of this person's problem is that an adhoc query, NOT just a 'select count(*) from table', can take remarkably long. Again, the problem is that PG can't just scan an index. ---------------------- One workaround for this is to use EXPLAIN. THIS APPLIES TO CASES WHERE THE USER HAS WIDE LIBERTY IN QUERIES. It's pointless overhead, otherwise. default_statistics_target is cranked up to 200 on all such tables, and pg_autovacuum is running. (If there were anything to improve, it would be refining the thresholds on this). If the "(cost...rows=nnnn" string returns a number higher than the QUERY row limit, the user is derailed ("That's not specific enough to answer immediately; do you want an emailed report?"). Otherwise, it runs EXPLAIN ANALYZE, which is still faster than the query itself. If the "(actual...rows=nnnn...)" is higher than the RESULT row limit (PAGE limit). It then runs the query, with the PAGE rows offset and limit --- and happily, practically everything that query needs is now in shared_buffers. The count from the EXPLAIN analyze is displayed in the web page. -- "Dreams come true, not free." -- S.Sondheim, ITW