X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C6F2A3B7050 for ; Thu, 16 Dec 2004 11:30:48 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 18381-09 for ; Thu, 16 Dec 2004 11:30:38 +0000 (GMT) Received: from druid.net (druid.net [216.126.72.98]) by svr1.postgresql.org (Postfix) with ESMTP id A962A3B7037 for ; Thu, 16 Dec 2004 11:30:40 +0000 (GMT) Received: from imp.druid.net (imp [216.126.72.111]) by druid.net (Postfix) with SMTP id 0B0C91A52; Thu, 16 Dec 2004 06:30:41 -0500 (EST) Date: Thu, 16 Dec 2004 06:30:14 -0500 From: "D'Arcy J.M. Cain" To: Jerome Alet Cc: pgsql-sql@postgresql.org Subject: Re: question about index Message-Id: <20041216063014.0dac1660.darcy@druid.net> In-Reply-To: <20041216104154.GA30578@mail.librelogiciel.com> References: <20041216104154.GA30578@mail.librelogiciel.com> X-Mailer: Sylpheed version 1.0.0beta3 (GTK+ 1.2.10; i386--netbsdelf) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.557 tagged_above=0 required=5 tests=AWL, FULL_REFUND X-Spam-Level: X-Archive-Number: 200412/104 X-Sequence-Number: 19724 On Thu, 16 Dec 2004 11:41:54 +0100 Jerome Alet wrote: > For a future databas, I plan to have got a table with a text field > which can contain only three different values, say "VALUE1", > "VALUE2", and "VALUE3" Can it increase? That is, can a "VALUE4" be added half way through the year? > this table may have, over the course of one year, several million > rows for a size around 2 Gb or more. Doing a COUNT(*) on that many rows could get expensive but... > I'd be interested in having : > > SELECT count(*) AS nbvalue1 FROM mytable WHERE > myfield='VALUE1'; SELECT count(*) AS nbvalue2 FROM mytable > WHERE myfield='VALUE2'; SELECT count(*) AS nbvalue3 FROM > mytable WHERE myfield='VALUE3'; First of all, consider doing it in one statement so that you at least are not running through it multiple times. SELECT myfield, COUNT(*) FROM mytable GROUP BY myfield; Second, consider rules or triggers to keep a separate table up to date at all times so that you simply have to dump a three row table instead of going through a huge table counting as you go. We did that in a similar situation and it made a huge difference. We were doing SUM() instead of COUNT() and we were calculating an extremely small percentage of the table - average probably 3 or 4 rows out of 20 million on average - but it still was worth our while to calculate the sum (balance) on every transaction rather than calculate it every time. You need to analyze your own data and usage but this may be a better solution for you. As usual, if not completely satisfied you get a full refund. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.