我不是很积极,但我相信子选择不是最优的(速度方面?)。
有没有办法从我的查询中删除这个子选择(我认为查询是不言自明的)。
select *
from tickers
where id = (select max(id) from tickers where annual_score is not null);
我不是很积极,但我相信子选择不是最优的(速度方面?)。
有没有办法从我的查询中删除这个子选择(我认为查询是不言自明的)。
select *
from tickers
where id = (select max(id) from tickers where annual_score is not null);
会像:
SELECT *
FROM ticker
WHERE annual_score IS NOT NULL
ORDER BY id desc
LIMIT 1
工作?
那个特定的子选择根本不应该是低效的。它应该在主查询开始之前运行一次。
有一类子查询效率低下(那些在主查询和子查询之间连接列的子查询),因为它们最终对主查询的每一行都运行子查询。
但这不应该是其中之一,除非 MySQL 严重脑损伤,我对此表示怀疑。
但是,如果您仍然热衷于摆脱子查询,您可以按 id (降序)对行进行排序,并且只获取第一个,例如:
select * from tickers
where annual_score is not null
order by id desc
limit 0, 1
不太熟悉 MySQL,但如果你想消除子查询,那么你可以尝试这样的事情:
select *
from tickers
where annual_score is not null
order by id desc
limit 1
我不知道这是否或多或少的性能,因为 MySQL 不是我的背景。