Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 15F6D9FB38B for ; Sun, 18 Mar 2007 17:37:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 12798-01 for ; Sun, 18 Mar 2007 17:37:07 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from zebedee.cranntara.net (zebedee.cranntara.net [91.186.7.79]) by postgresql.org (Postfix) with ESMTP id 3B30C9FB35C for ; Sun, 18 Mar 2007 17:37:09 -0300 (ADT) Received: from du-007-0116.freeuk.com ([212.126.143.116] helo=bolek.coachhouse) by zebedee.cranntara.net with esmtpsa (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.62) (envelope-from ) id 1HT27m-0002Bq-5J for pgsql-sql@postgresql.org; Sun, 18 Mar 2007 20:37:07 +0000 Received: from [192.168.0.32] (helo=[192.168.0.32]) by bolek.coachhouse with esmtp (Exim 4.50) id 1HT1Pc-0001v0-Eq for pgsql-sql@postgresql.org; Sun, 18 Mar 2007 19:51:28 +0000 Message-ID: <45FD9840.4040305@numerixtechnology.de> Date: Sun, 18 Mar 2007 19:51:28 +0000 From: T E Schmitz Reply-To: mailreg@numerixtechnology.de User-Agent: Debian Thunderbird 1.0.7 (X11/20051017) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: triple self-join crawling 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: 200703/239 X-Sequence-Number: 27877 The following self join of a table containing 5800 records is crawling: CREATE TABLE history ( stock VARCHAR(30) NOT NULL, day date NOT NULL, open NUMERIC (6,1) NOT NULL, high NUMERIC (6,1) NOT NULL, low NUMERIC (6,1) NOT NULL, close NUMERIC (6,1) NOT NULL, volume NUMERIC (12) NOT NULL, PRIMARY KEY (stock,day) ); SELECT history.stock, history.day, history.high, history.low, MAX(past_week.high) AS week_high, MAX(past_month.high) AS month_high FROM history INNER JOIN history AS past_month ON (past_month.stock = history.stock AND past_month.day < history.day AND past_month.day >= (history.day - 30)) INNER JOIN history AS past_week ON (past_week.stock = past_month.stock AND past_week.day < history.day AND past_week.day >= (history.day - 7)) GROUP BY history.stock, history.day, history.high, history.low ORDER BY history.stock, history.day DESC How can I speed this up? -- Regards, Tarlika Elisabeth Schmitz