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 54FB0D1B520 for ; Sat, 4 Oct 2003 15:57:30 +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 17917-09 for ; Sat, 4 Oct 2003 12:56:43 -0300 (ADT) Received: from candle.pha.pa.us (momjian.navpoint.com [207.106.42.251]) by svr1.postgresql.org (Postfix) with ESMTP id 97BE5D1B4EC for ; Sat, 4 Oct 2003 12:56:42 -0300 (ADT) Received: (from pgman@localhost) by candle.pha.pa.us (8.11.6/8.11.6) id h94Fuek24423; Sat, 4 Oct 2003 11:56:40 -0400 (EDT) From: Bruce Momjian Message-Id: <200310041556.h94Fuek24423@candle.pha.pa.us> Subject: Re: count(*) slow on large tables In-Reply-To: <60brszcng5.fsf@dev6.int.libertyrms.info> To: Christopher Browne Date: Sat, 4 Oct 2003 11:56:40 -0400 (EDT) Cc: 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/139 X-Sequence-Number: 3887 Christopher Browne wrote: > jllachan@nsd.ca (Jean-Luc Lachance) writes: > > That's one of the draw back of MVCC. > > I once suggested that the transaction number and other house keeping > > info be included in the index, but was told to forget it... > > It would solve once and for all the issue of seq_scan vs index_scan. > > It would simplify the aggregate problem. > > It would only simplify _one_ case, namely the case where someone cares > about the cardinality of a relation, and it would do that at > _considerable_ cost. > > A while back I outlined how this would have to be done, and for it to > be done efficiently, it would be anything BUT simple. > > It would be very hairy to implement it correctly, and all this would > cover is the single case of "SELECT COUNT(*) FROM SOME_TABLE;" 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. -- 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