Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id B676A9FB963 for ; Tue, 24 Jul 2007 15:27:10 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 41344-10 for ; Tue, 24 Jul 2007 15:26:55 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from fafnir.androme.com (fafnir.androme.com [62.58.96.158]) by postgresql.org (Postfix) with ESMTP id 182049FB931 for ; Tue, 24 Jul 2007 15:27:06 -0300 (ADT) Received: by fafnir.androme.com (Postfix, from userid 1003) id E07A4B9A5A1; Tue, 24 Jul 2007 20:27:01 +0200 (CEST) Message-ID: <46A64475.3040201@andromeiberica.com> Date: Tue, 24 Jul 2007 20:27:01 +0200 From: Arnau Reply-To: arnaulist@andromeiberica.com User-Agent: Thunderbird 2.0.0.5 (Windows/20070716) MIME-Version: 1.0 To: PostgreSQL Performance Subject: index over timestamp not being used Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/320 X-Sequence-Number: 26004 Hi all, I've got the following two tables running on postgresql 8.1.4 transactions Column | Type | Modifiers ----------------------+-----------------------------+--------------- transaction_id | character varying(32) | not null user_id | bigint | not null timestamp_in | timestamp without time zone | default now() type_id | integer | technology_id | integer | Indexes: "pk_phusrtrans_transid" PRIMARY KEY, btree (transaction_id) "idx_phusrtrans_paytyptech" btree (type_id, technology_id) "idx_putrnsctns_tstampin" btree (timestamp_in) statistics Column | Type | Modifiers ----------------------+-----------------------------+------------------- statistic_id | bigint | not null duration | bigint | transaction_id | character varying(32) | Indexes: "pk_phstat_statid" PRIMARY KEY, btree (statistic_id) "idx_phstat_transid" btree (transaction_id) the idea is to have a summary of how many transactions, duration, and type for every date. To do so, I've done the following query: SELECT count(t.transaction_id) AS num_transactions , SUM(s.duration) AS duration , date(t.timestamp_in) as date , t.type_id FROM transactions t LEFT OUTER JOIN statistics s ON t.transaction_id = s.transaction_id WHERE t.timestamp_in >= to_timestamp('20070101', 'YYYYMMDD') GROUP BY date, t.type_id; I think this could be speed up if the index idx_putrnsctns_tstampin (index over the timestamp) could be used, but I haven't been able to do it. Any suggestion? Thanks all -- Arnau