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 C615E52924 for ; Wed, 10 Aug 2005 10:32:33 -0300 (ADT) 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 03756-06 for ; Wed, 10 Aug 2005 13:32:23 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 08C435287F for ; Wed, 10 Aug 2005 10:32:21 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j7ADVw99046528 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 10 Aug 2005 07:32:01 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j7ADVwOa046296; Wed, 10 Aug 2005 07:31:58 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j7ADVv6c046295; Wed, 10 Aug 2005 07:31:57 -0600 (MDT) (envelope-from mfuhr) Date: Wed, 10 Aug 2005 07:31:57 -0600 From: Michael Fuhr To: Tom Lane Cc: dracula007@atlas.cz, pgsql-sql@postgresql.org Subject: Re: **SPAM** Faster count(*)? Message-ID: <20050810133157.GA46247@winnie.fuhr.org> References: <000101c59d33$36a4a560$9b00015a@osl.com> <196456155.20050810012958@karneval.cz> <21626.1123642154@sss.pgh.pa.us> <20050810032913.GA40917@winnie.fuhr.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050810032913.GA40917@winnie.fuhr.org> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.109 tagged_above=0 required=5 tests=AWL, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200508/47 X-Sequence-Number: 22275 On Tue, Aug 09, 2005 at 09:29:13PM -0600, Michael Fuhr wrote: > On Tue, Aug 09, 2005 at 10:49:14PM -0400, Tom Lane wrote: > > Current best practice is to run the explain and parse out the "rows" > > figure using a perl (or axe-of-choice) regexp, though we could be > > persuaded to supply a simpler API if there's enough demand for it. > > Somebody else requested a row-count-estimate function a couple of > weeks ago: > > http://archives.postgresql.org/pgsql-admin/2005-07/msg00256.php Here's a simple example that parses EXPLAIN output. It should work in 8.0.2 and later: CREATE FUNCTION count_estimate(query text) RETURNS integer AS $$ DECLARE rec record; rows integer; BEGIN FOR rec IN EXECUTE 'EXPLAIN ' || query LOOP rows := substring(rec."QUERY PLAN" FROM ' rows=([[:digit:]]+)'); EXIT WHEN rows IS NOT NULL; END LOOP; RETURN rows; END; $$ LANGUAGE plpgsql VOLATILE STRICT; CREATE TABLE foo (r double precision); INSERT INTO foo SELECT random() FROM generate_series(1, 1000); ANALYZE foo; SELECT count_estimate('SELECT * FROM foo WHERE r < 0.1'); count_estimate ---------------- 97 (1 row) EXPLAIN SELECT * FROM foo WHERE r < 0.1; QUERY PLAN ----------------------------------------------------- Seq Scan on foo (cost=0.00..17.50 rows=97 width=8) Filter: (r < 0.1::double precision) (2 rows) -- Michael Fuhr