mysql - 单独执行时查询速度快,嵌套时查询速度慢

标签 mysql performance

我正在尝试查找一系列特定日期的最近进入时间。当我运行时

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 额外=使用 在哪里;使用索引

我想在许多不同的日期执行相同的操作。有没有办法可以有效地做到这一点?

最佳答案

看起来像这个错误:

http://bugs.mysql.com/bug.php?id=32665

也许如果您在dates.dt上创建索引,它就会消失。

关于mysql - 单独执行时查询速度快,嵌套时查询速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7518738/

相关文章:

c++ - _mm_prefetch 是异步的吗?分析显示有很多周期

mysql - Access(SQL)将一个数据中的两个字段拆分为两个数据

mysql - SQL循环得到一个空值

php - 组合相似条目的 sum(),并包含 WHERE 子句 SQL

java - SQL 断言 - 在单元测试中比较两个 SQL 查询

windows - 在 Windows 中通过常规文件传输 1-2 兆字节的数据 - 它比通过 RAM 慢吗?

php - 在 PHP 中启动新进程/线程的最简单方法

带有连接的 MySQL 计数函数?

performance - 快速移动物体2D游戏(Unity3d)

linux - Linux 中的时间命令显示的用户和系统时间过高