Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 7924F9FB31B for ; Mon, 19 Mar 2007 15:24:43 -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 62682-03 for ; Mon, 19 Mar 2007 15:24:25 -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 244E29FB7DD for ; Mon, 19 Mar 2007 15:19:27 -0300 (ADT) Received: from [62.69.36.78] (helo=bolek.coachhouse) by zebedee.cranntara.net with esmtpsa (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.62) (envelope-from ) id 1HTMS3-0002u7-Pg; Mon, 19 Mar 2007 18:19:25 +0000 Received: from [192.168.0.32] (helo=[192.168.0.32]) by bolek.coachhouse with esmtp (Exim 4.50) id 1HTKQa-0003ak-Ad; Mon, 19 Mar 2007 16:09:44 +0000 Message-ID: <45FEB5C7.5010406@numerixtechnology.de> Date: Mon, 19 Mar 2007 16:09:43 +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: Andrew Sullivan CC: pgsql-sql@postgresql.org Subject: Re: triple self-join crawling References: <45FD9840.4040305@numerixtechnology.de> <20070319081228.GA21894@phlogiston.dyndns.org> In-Reply-To: <20070319081228.GA21894@phlogiston.dyndns.org> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/252 X-Sequence-Number: 27890 Andrew Sullivan wrote: > Define "crawling". Also, please post EXPLAIN and, if feasible, Total runtime: 191430.537 ms > EXPLAIN ANALYSE output for your case. > > On Sun, Mar 18, 2007 at 07:51:28PM +0000, 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 QUERY PLAN GroupAggregate (cost=1372725715.71..1414901439.85 rows=5801 width=56) (actual time=185248.481..191393.056 rows=5800 loops=1) -> Sort (cost=1372725715.71..1378750806.73 rows=2410036407 width=56) (actual time=185247.212..187763.008 rows=584203 loops=1) Sort Key: history.stock, history."day", history.high, history.low -> Nested Loop (cost=0.00..132245248.41 rows=2410036407 width=56) (actual time=0.124..148136.464 rows=584203 loops=1) Join Filter: (("inner".stock)::text = ("outer".stock)::text) -> Nested Loop (cost=0.00..555931.84 rows=3739067 width=53) (actual time=0.087..137531.941 rows=28147 loops=1) Join Filter: (("outer"."day" >= ("inner"."day" - 7)) AND (("outer".stock)::text = ("inner".stock)::text)) -> Index Scan using history_pkey on history past_week (cost=0.00..266.62 rows=5801 width=23) (actual time=0.034..43.147 rows=5801 loops=1) -> Index Scan using idx_history_day on history (cost=0.00..57.11 rows=1934 width=34) (actual time=0.021..12.709 rows=2900 loops=5801) Index Cond: ("outer"."day" < history."day") -> Index Scan using idx_history_day on history past_month (cost=0.00..22.32 rows=645 width=23) (actual time=0.019..0.170 rows=21 loops=28147) Index Cond: ((past_month."day" < "outer"."day") AND (past_month."day" >= ("outer"."day" - 30))) Total runtime: 191430.537 ms -- Regards, Tarlika Elisabeth Schmitz