【问题标题】:Percona 5.6 InnoDB issue not using indexes correctlyPercona 5.6 InnoDB 问题未正确使用索引
【发布时间】:2013-11-26 11:32:54
【问题描述】:

我刚刚在我的新 CentOS 6.4 服务器上安装了 Percona 5.6。这是一台 32 核氙气、72GB 内存、8x SAS RAID 10 设置的快速机器。到目前为止一切顺利

我的旧服务器功能稍逊一筹,并且仍在运行 MySQL 5.1。所以这是一个相当大的升级。但是我在使用 InnoDB 时遇到了一些问题,它似乎没有在某些表上正确使用索引。在我的旧机器上,相同的查询运行良好。

两台服务器具有相同的数据库。我在旧机器上做了一个 mysqldump 并将它导入到新的 Percona 5.6 服务器上。索引保持不变。两台服务器使用相同的 my.cnf 配置设置。

表项的索引位于:item_id, item_format, item_private,并包含大约 4000 万行。表格格式的索引为:format_id,包含大约 250 行。

SELECT 
i.item_name, i.item_key, i.item_date, f.format_long
FROM
items i, formats f
WHERE 
i.item_format = f.format_id
AND
i.item_private = 0 
ORDER BY 
i.item_id DESC LIMIT 8

在我的旧服务器上,这个查询大约需要 0.0003 seconds。在新服务器上,它接管了100 seconds

在旧服务器上使用 EXPLAIN 查询。

+----+-------------+-------+--------+---------------+---------+---------+----------------------+------+-------------+
| id | select_type | table |  type  | possible_keys |   key   | key_len |         ref          | rows |    Extra    |
+----+-------------+-------+--------+---------------+---------+---------+----------------------+------+-------------+
|  1 | SIMPLE      | i     | index  | item_format   | PRIMARY |       4 | NULL                 |    8 | Using where |
|  1 | SIMPLE      | f     | eq_ref | PRIMARY       | PRIMARY |       4 | dbname.i.item_format |    1 |             |
+----+-------------+-------+--------+---------------+---------+---------+----------------------+------+-------------+

在新的 [问题] 服务器上使用 EXPLAIN 进行查询。

+----+-------------+-------+------+---------------+-------------+---------+--------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys |     key     | key_len |        ref         | rows |              Extra              |
+----+-------------+-------+------+---------------+-------------+---------+--------------------+------+---------------------------------+
|  1 | SIMPLE      | f     | ALL  | PRIMARY       | NULL        | NULL    | NULL               |  219 | Using temporary; Using filesort |
|  1 | SIMPLE      | i     | ref  | item_format   | item_format | 4       | dbname.f.format_id | 3026 | Using where                     |
+----+-------------+-------+------+---------------+-------------+---------+--------------------+------+---------------------------------+

您可以看到它正在使用临时和文件排序。这似乎是缓慢的原因。

知道如何解决这个问题吗?

【问题讨论】:

    标签: mysql innodb percona


    【解决方案1】:

    这听起来像:Bug #70617 Default persistent stats can cause unexpected long query times

    不管怎样,这不是 Percona 错误,它也存在于 MySQL 5.6 社区版中。

    有三种可能的解决方法:

    1. 使用 STRAIGHT_JOIN 提示优化器不要重新排序表引用。

      SELECT STRAIGHT_JOIN
        i.item_name, i.item_key, i.item_date, f.format_long
      FROM items i
      INNER JOIN formats f
        ON i.item_format = f.format_id
      WHERE i.item_private = 0 
      ORDER BY i.item_id DESC LIMIT 8
      

      我已经重写了您的 JOIN 以使用我推荐的 SQL-92 语法。

    2. 禁用新的InnoDB persistent stats 功能,恢复到 5.6 之前的行为。

      在您的 my.cnf 文件中:

      innodb_stats_persistent=0
      
    3. 在对数据进行重大更改后(例如,在加载 mysqldump 后)手动刷新 InnoDB 优化器统计信息:

      ANALYZE TABLE items;
      ANALYZE TABLE formats;
      

    PS:我在 Percona 工作,这个 bug 是我的同事 Justin Swanhart 发现的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多