我正在尝试查找一系列特定日期的最近进入时间。当我运行时
select max(ts) as maxts from factorprice where ts <= '2011-1-5'
它返回得很快。
EXPLAIN 给出 select_type SIMPLE 和“选择优化掉的表”。
但是当我运行时
select (select max(ts) from factorprice where ts <= dates.dt) as maxts, dates.dt
from
trends.dates where dates.dt in ('2011-1-6');
返回需要很长时间(约 10 秒)。
解释一下:
- select_type=PRIMARY table=dates rows=506 Extra=使用位置
- select_type=DEPENDENT SUBQUERY 表=factorprice 类型=索引 possible_keys=主键=主键len=8行=26599224额外=使用 在哪里;使用索引
此查询也需要很长时间(10秒)
select dt, max(ts) as maxts from factorprice as f inner join trends.dates as d
where ts <= dt and dt in ('2011-1-6')
group by dt;
解释一下:
- select_type=SIMPLE table=d type=ALL rows =509 Extra=使用位置
- select_type=SIMPLE 表=f 类型=范围 possible_keys=主键=主 keylen=8 行=26599224 额外=使用 在哪里;使用索引
我想在许多不同的日期执行相同的操作。有没有办法可以有效地做到这一点?
最佳答案
关于mysql - 单独执行时查询速度快,嵌套时查询速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7518738/