Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id B9FE39FB381 for ; Tue, 30 Jan 2007 05:33:24 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 90701-02 for ; Tue, 30 Jan 2007 05:33:17 -0400 (AST) X-Greylist: delayed 00:59:35.931702 by SQLgrey-1.7.4 Received: from mail3.plesk.ru (gw-swsoft2.ll-nsk.zsttk.ru [82.200.65.190]) by postgresql.org (Postfix) with ESMTP id 1B1BB9FA5D8 for ; Tue, 30 Jan 2007 05:33:20 -0400 (AST) Received: by mail3.plesk.ru (Postfix, from userid 426) id B22C51A9BFC; Tue, 30 Jan 2007 15:33:34 +0600 (NOVT) Received: from [127.0.0.1] (Lobanov.plesk.ru [192.168.62.80]) by mail3.plesk.ru (Postfix) with ESMTP id 6437C1A9BE3; Tue, 30 Jan 2007 15:33:34 +0600 (NOVT) Message-ID: <45BF10D7.3040203@swsoft.com> Date: Tue, 30 Jan 2007 15:33:11 +0600 From: Igor Lobanov User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 To: Richard Huxton , pgsql-performance@postgresql.org Subject: Re: Querying distinct values from a large table References: <45BF02DE.7080605@swsoft.com> <45BF0C13.3070003@archonet.com> In-Reply-To: <45BF0C13.3070003@archonet.com> Content-Type: text/plain; charset=KOI8-R; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/415 X-Sequence-Number: 23016 Richard Huxton wrote: >> I have rather large table with about 5 millions of rows and a dozen of >> columns. Let's suppose that columns are named 'a', 'b', 'c' etc. I >> need to query distinct pairs of ('a';'b') from this table. > > What version of PostgreSQL is it? 8.1.4 > How many distinct values are you getting back from your 5 million rows? > If there are too many, an index isn't going to help. No more than 10,000. > Can you share the EXPLAIN ANALYSE output? You might want to try > increasing work_mem for this one query to speed any sorting. Real table and colum names are obfuscated because of NDA, sorry. explain analyze select distinct a, b from tbl EXPLAIN ANALYZE output is: Unique (cost=500327.32..525646.88 rows=1848 width=6) (actual time=52719.868..56126.356 rows=5390 loops=1) -> Sort (cost=500327.32..508767.17 rows=3375941 width=6) (actual time=52719.865..54919.989 rows=3378864 loops=1) Sort Key: a, b -> Seq Scan on tbl (cost=0.00..101216.41 rows=3375941 width=6) (actual time=16.643..20652.610 rows=3378864 loops=1) Total runtime: 57307.394 ms > How often is the table updated? Clustering might buy you some > improvements (but not a huge amount I suspect). It is updated once per 3-5 seconds. And one more thing. I don't know if it helps, but column 'a' can have value from a limited set: 0, 1 or 2. Column 'b' is also an integer (foreign key, actually). -- Igor Lobanov Internal Development Engineer SWsoft, Inc.