X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 1AC3BD1B51E for ; Sat, 4 Oct 2003 17:49:38 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 46762-01 for ; Sat, 4 Oct 2003 14:48:53 -0300 (ADT) Received: from candle.pha.pa.us (momjian.navpoint.com [207.106.42.251]) by svr1.postgresql.org (Postfix) with ESMTP id 64391D1B54C for ; Sat, 4 Oct 2003 14:48:51 -0300 (ADT) Received: (from pgman@localhost) by candle.pha.pa.us (8.11.6/8.11.6) id h94Hmlc04602; Sat, 4 Oct 2003 13:48:47 -0400 (EDT) From: Bruce Momjian Message-Id: <200310041748.h94Hmlc04602@candle.pha.pa.us> Subject: Re: count(*) slow on large tables In-Reply-To: <6743.1065286173@sss.pgh.pa.us> To: Tom Lane Date: Sat, 4 Oct 2003 13:48:47 -0400 (EDT) Cc: Christopher Browne , pgsql-performance@postgresql.org X-Mailer: ELM [version 2.4ME+ PL106 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/142 X-Sequence-Number: 3890 Tom Lane wrote: > Bruce Momjian writes: > > We do have a TODO item: > > * Consider using MVCC to cache count(*) queries with no WHERE clause > > > The idea is to cache a recent count of the table, then have > > insert/delete add +/- records to the count. A COUNT(*) would get the > > main cached record plus any visible +/- records. This would allow the > > count to return the proper value depending on the visibility of the > > requesting transaction, and it would require _no_ heap or index scan. > > ... and it would give the wrong answers. Unless the cache is somehow > snapshot-aware, so that it can know which other transactions should be > included in your count. The cache is an ordinary table, with xid's on every row. I meant it would require no index/heap scans of the large table --- it would still require a scan of the "count" table. -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073