Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 4CF9A9FB4D7 for ; Mon, 19 Mar 2007 05:22:53 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 94255-04 for ; Mon, 19 Mar 2007 05:22:49 -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 879D99FB3EE for ; Mon, 19 Mar 2007 05:22:49 -0300 (ADT) Received: from dial-dynamic-62-69-36-219.surfdial.murphx.net ([62.69.36.219] helo=bolek.coachhouse) by zebedee.cranntara.net with esmtpsa (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.62) (envelope-from ) id 1HTD8g-0002YY-8c for pgsql-sql@postgresql.org; Mon, 19 Mar 2007 08:22:47 +0000 Received: from [192.168.0.32] (helo=[192.168.0.32]) by bolek.coachhouse with esmtp (Exim 4.50) id 1HTD8T-00038P-2M for pgsql-sql@postgresql.org; Mon, 19 Mar 2007 08:22:33 +0000 Message-ID: <45FE4848.5040407@numerixtechnology.de> Date: Mon, 19 Mar 2007 08:22:32 +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 CC: pgsql-sql@postgresql.org Subject: Re: triple self-join crawling References: <45FD9840.4040305@numerixtechnology.de> In-Reply-To: <45FD9840.4040305@numerixtechnology.de> 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/246 X-Sequence-Number: 27884 T E Schmitz wrote: > The following self join of a table containing 5800 records is crawling: > > 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 Things improved hugely when I changed the JOIN clauses: LEFT OUTER JOIN history AS past_month ON (past_month.stock = history.stock AND past_month.day >= (history.day - 30) AND past_month.day < history.day) LEFT OUTER JOIN history AS past_week ON (past_week.stock = past_month.stock AND past_week.day =past_month.day AND past_week.day >= (history.day - 7)) -- Regards, Tarlika Elisabeth Schmitz