Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id E783713364DE for ; Tue, 12 Oct 2010 05:34:29 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 52911-02 for ; Tue, 12 Oct 2010 08:34:20 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f46.google.com (mail-bw0-f46.google.com [209.85.214.46]) by mail.postgresql.org (Postfix) with ESMTP id 4C1D91336473 for ; Tue, 12 Oct 2010 05:34:17 -0300 (ADT) Received: by bwz15 with SMTP id 15so2238018bwz.19 for ; Tue, 12 Oct 2010 01:34:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=Fxv6NSh/628HwuHV7CIb37Stp72i/jjYIePWrKrqiTg=; b=S5zXRTZM5dkHFqldaqg60gzO7Kb1xwFusNyN2KjPXVspPluluw2t6ZmWivBacbXL+2 7np2gACK+6SyAG+7WGaq9jDasme8bgkbdEgli6tQFarqLr6ruFVsgUHsLX/FdY1Ag7fc /o071rrvf9UYzQcjzxvPbiZbefw+Wq4Vk843k= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:cc:subject:references :in-reply-to:content-type:content-transfer-encoding; b=PQAioCqsr/wuk9zHqB83kTs3eHj+Oo0kPYrVz62JAAQsXi5y4J3Qll+6fR2MWECLhv Cb5AeX0154WP9PHKdkfHlZarHfGFYDfuWMzvfqpMmj9Ffd3P9BNBP90M0hwhjJO0kjLj dcS2Rkjdx7+Lj4xYfN4cNekRmtMmp540ge/FE= Received: by 10.204.76.3 with SMTP id a3mr6044453bkk.190.1286872456885; Tue, 12 Oct 2010 01:34:16 -0700 (PDT) Received: from [10.1.1.57] (skuns.zoral.com.ua [91.193.166.194]) by mx.google.com with ESMTPS id d27sm6306519bku.10.2010.10.12.01.34.14 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 12 Oct 2010 01:34:14 -0700 (PDT) Message-ID: <4CB41D8E.2010302@gmail.com> Date: Tue, 12 Oct 2010 11:34:22 +0300 From: Vitalii Tymchyshyn User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.12) Gecko/20100915 Thunderbird/3.0.8 MIME-Version: 1.0 CC: pgsql-performance@postgresql.org Subject: Re: Slow count(*) again... References: <201010091626.18636.neil.whelchel@gmail.com> <4CB34D69.6060108@emolecules.com> <4CB4149D.8030803@gmail.com> <4CB41902.1020503@postnewspapers.com.au> In-Reply-To: <4CB41902.1020503@postnewspapers.com.au> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.879 tagged_above=-10 required=5 tests=BAYES_00=-1.9, MISSING_HEADERS=1.021, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201010/137 X-Sequence-Number: 40556 12.10.10 11:14, Craig Ringer написав(ла): > On 10/12/2010 03:56 PM, Vitalii Tymchyshyn wrote: > >> BTW: There is a lot of talk about MVCC, but is next solution possible: >> 1) Create a page information map that for each page in the table will >> tell you how may rows are within and if any write (either successful or >> not) were done to this page. This even can be two maps to make second >> one really small (a bit per page) - so that it could be most time >> in-memory. >> 2) When you need to to count(*) or index check - first check if there >> were no writes to the page. If not - you can use count information from >> page info/index data without going to the page itself >> 3) Let vacuum clear the bit after frozing all the tuples in the page (am >> I using terminology correctly?). > > Part of this already exists. It's called the visibility map, and is > present in 8.4 and above. It's not currently used for queries, but can > potentially be used to aid some kinds of query. > > http://www.postgresql.org/docs/8.4/static/storage-vm.html > >> In this case all read-only (archive) data will be this bit off and >> index/count(*) will be really fast. > > A count with any joins or filter criteria would still have to scan all > pages with visible tuples in them. If one don't use parittioning. With proper partitioning, filter can simply select a partitions. Also filtering can be mapped on the index lookup. And if one could join index hash and visibility map, much like two indexes can be bit joined now, count can be really fast for all but non-frozen tuples. > So the visibility map helps speed up scanning of bloated tables, but > doesn't provide a magical "fast count" except in the utterly trivial > "select count(*) from tablename;" case, and can probably only be used > for accurate results when there are no read/write transactions > currently open. Why so? You simply has to recount the pages that are marked dirty in usual way. But count problem usually occurs when there are a lot of archive data (you need to count over 100K records) that is not modified. Best regards, Vitalii Tymchyshyn