by Ted » Mon, 19 Nov 2007 07:49:59
n Nov 17, 5:03 pm, "Paul Lautman" < XXXX@XXXXX.COM >
wrote:
Thanks Paul, I leanred soemthing today, and almost doubled the speed
of my query. The result is now:
SELECT A1.stock_id,
A1.price_date,
A1.adjusted,
A2.price_date AS pd22,
100.0 * (A1.adjusted - A2.adjusted)/A2.adjusted AS gl22pc,
A3.price_date AS pd66,
100.0 * (A1.adjusted - A3.adjusted)/A3.adjusted AS gl66pc,
A4.price_date AS pd132,
100.0 * (A1.adjusted - A4.adjusted)/A4.adjusted AS gl132pc,
A5.price_date AS pd264,
100.0 * (A1.adjusted - A5.adjusted)/A5.adjusted AS gl264pc
FROM
(SELECT * FROM stockprices WHERE stock_id = 1 ORDER BY price_date DESC
LIMIT 1) AS A1
LEFT JOIN
(SELECT * FROM stockprices WHERE stock_id = 1 ORDER BY price_date DESC
LIMIT 1 OFFSET 22) AS A2
ON A1.stock_id = A2.stock_id
LEFT JOIN
(SELECT * FROM stockprices WHERE stock_id = 1 ORDER BY price_date DESC
LIMIT 1 OFFSET 66) AS A3
ON A1.stock_id = A3.stock_id
LEFT JOIN
(SELECT * FROM stockprices WHERE stock_id = 1 ORDER BY price_date DESC
LIMIT 1 OFFSET 132) AS A4
ON A1.stock_id = A4.stock_id
LEFT JOIN
(SELECT * FROM stockprices WHERE stock_id = 1 ORDER BY price_date DESC
LIMIT 1 OFFSET 264) AS A5
ON A1.stock_id = A5.stock_id;
So I no longer have subselects within the select statements used to
produce derived tables, and each derived table has only one record, so
the left joins are faster.
But I am still not seeing how to apply the logic to each stock
individually from a small selection of stock ids from a very large
pool of stocks that could be chosen. Obviously, if I remove or alter
the WHERE clauses, the logic of the statement is immediately broken.
Where I am making a selection of stock IDs, the table used has simple
records, only two clumns: etf_id and stock_id. An etf is an
investment vehicle where some fund manager has selected what he
considers an optimal suite of stocks for the purpose of the fund).
The primary key uses both columns, and there is an extra index on
stock ID. If I make a mistake, and I get a part of teh planner's plan
resulting in tens of thousands of stocks involved in a left join of a
table with many millions of price records, the cost will be huge and
performance so degraded even my grand nephew will have died of old age
by the time it would be done (or at least he'll have more gray hair
than do I).
I suppose I could construct yet another derived table, giving the
whole statement an alias (by enclosing the who thing in parentheses
and adding "AS OPT"), and then using a join with another select
statement that returns only the required suite of stock IDs, But I
worry about the potential that has for making something that is now
very fast into something that is slow, and I am not sure how to ensure
that the above logic will be applied to each stock individually, with
the output placed in a resultset that has a record for each stock.
I am thinking maybe making my statement above a correlated subquery
(which ought to preserve my logic above, if I properly understand how
subqueries work), but I am at a loss as to how to put the data
returned by such a subquery into the recordset returned by the
composite statement. It would seem my understanding of correlated
subqueries is incomplete. :-(
Can you give me another hint or two? Perhaps with a word of
explanation of correlated subqueries where my understandin