select *, (T3.price_usd - T1.price_usd)/T3.price_usd as b
from ticker T1
INNER JOIN mycoins T2
ON T1.symbol = T2.symbol
INNER JOIN ticker T3
ON T1.symbol = T3.symbol
WHERE T1.timer = (Select max(timer) from ticker)
AND T3.timer = (Select min(timer) from ticker where timer
> (Select max(timer) from ticker - 60*60*24*7*2))
上述代码存在(Select max(timer) from ticker)
在查询中多次执行的问题。这是因为多次使用最大计时器值。有没有办法改进这个代码,所以我们可以确定一次最大计时器,然后重新使用该值?
答案 0 :(得分:2)
使用以下方法优化您的方案的查询,SQL Server !!
-- Declare Varaibles
declare @MaxTime int
declare @MinTime int
-- Set values ,Need not to use inside query as recurrncy reduce processing
Select @MaxTime=max(timer) from ticker
Select @MinTime=min(timer) from ticker where timer > (Select max(timer) from ticker - 60*60*24*7*2))
-- Create complex query
select *, (T3.price_usd - T1.price_usd)/T3.price_usd as b
from ticker T1
INNER JOIN mycoins T2
ON T1.symbol = T2.symbol
INNER JOIN ticker T3
ON T1.symbol = T3.symbol
WHERE T1.timer = @MaxTime AND
T3.timer = @MinTime
答案 1 :(得分:1)
我认为您应该使用存储过程,使用它很容易调整并且对性能有益。
以下是MS SQL Server的示例
CREATE PROCEDURE test
AS
BEGIN
Declare @max int
Set @max = (Select max(timer) from ticker);
select *, (T3.price_usd - T1.price_usd)/T3.price_usd as b
from ticker T1
INNER JOIN mycoins T2
ON T1.symbol = T2.symbol
INNER JOIN ticker T3
ON T1.symbol = T3.symbol
WHERE T1.timer = @max AND
T3.timer = (Select min(timer) from ticker where timer > (@max - 60*60*24*7*2))
END
GO
答案 2 :(得分:0)
您可以使用:
declare @MaxTime bigint
declare @MinTime bigint
Select @MaxTime = max(timer) from ticker
Select @MinTime = min(timer) from ticker where timer > (@MaxTime - 60*60*24*7*2)
select *, (T3.price_usd - T1.price_usd)/T3.price_usd as b
from ticker T1
INNER JOIN mycoins T2
ON T1.symbol = T2.symbol
INNER JOIN ticker T3
ON T1.symbol = T3.symbol
WHERE T1.timer = @MaxTime
AND T3.timer = @MinTime