Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id D4A9C1337BE0 for ; Tue, 12 Oct 2010 05:15:20 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 06634-01-9 for ; Tue, 12 Oct 2010 08:15:13 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id 7ED2C1337C01 for ; Tue, 12 Oct 2010 05:15:08 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 13663AEF97; Tue, 12 Oct 2010 16:15:05 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (mail.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id RJ70fuN7xSEd; Tue, 12 Oct 2010 16:14:59 +0800 (WST) Received: from [10.1.1.6] (124-148-209-200.dyn.iinet.net.au [124.148.209.200]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id B9245AEF8C; Tue, 12 Oct 2010 16:14:58 +0800 (WST) Message-ID: <4CB41902.1020503@postnewspapers.com.au> Date: Tue, 12 Oct 2010 16:14:58 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.9) Gecko/20100921 Fedora/3.1.4-1.fc14 Thunderbird/3.1.4 MIME-Version: 1.0 To: Vitalii Tymchyshyn 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> In-Reply-To: <4CB4149D.8030803@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201010/135 X-Sequence-Number: 40554 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. 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. Even if you kept a count of tuples in each page along with the mvcc transaction ID information required to determine for which transactions that count is valid, it'd only be useful if you didn't have to do any condition checks, and it'd be yet another thing to update with every insert/delete/update. Perhaps for some users that'd be worth having, but it seems to me like it'd have pretty narrow utility. I'm not sure that's the answer. -- Craig Ringer