【问题标题】:Same query with the same data, different performance相同的查询,相同的数据,不同的性能
【发布时间】:2015-04-02 01:20:59
【问题描述】:

我在两台不同的服务器上使用相同的数据集运行 SQL 查询。

服务器 1:mysql Ver 14.14 Distrib 5.1.73,用于使用 readline 5.1 的 unknown-linux-gnu (x86_64)

服务器 2:mysql Ver 15.1 Distrib 10.0.12-MariaDB,用于 osx10.10 (x86_64),使用 readline 5.1

查询是:

SELECT
 *
FROM
 `user`
WHERE
 user_id IN (SELECT user_id FROM reader_detail)
AND `user`.type <> 4
AND `user`.type <> 11
AND user_id NOT IN (
 SELECT
  `user_id`
 FROM
  reader_log
 WHERE
  reader_log.`date` > DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
 GROUP BY
  user_id
 ORDER BY
  user_id ASC
)
AND username IS NOT NULL;

在服务器 1 上解释选择:

+----+--------------------+---------------+-----------------+---------------+---------+---------+------+-------+--------------------------+
| id | select_type        | table         | type            | possible_keys | key     | key_len | ref  | rows  | Extra                    |
+----+--------------------+---------------+-----------------+---------------+---------+---------+------+-------+--------------------------+
|  1 | PRIMARY            | user          | ALL             | NULL          | NULL    | NULL    | NULL | 29865 | Using where              |
|  3 | DEPENDENT SUBQUERY | reader_log    | index           | NULL          | PRIMARY | 7       | NULL |    17 | Using where; Using index |
|  2 | DEPENDENT SUBQUERY | reader_detail | unique_subquery | PRIMARY       | PRIMARY | 4       | func |     1 | Using index              |
+----+--------------------+---------------+-----------------+---------------+---------+---------+------+-------+--------------------------+

在服务器 2 上解释选择:

+------+--------------------+---------------+----------------+----------------------+---------+---------+----------------------------------------+-------+--------------------------+
| id   | select_type        | table         | type           | possible_keys        | key     | key_len | ref                                    | rows  | Extra                    |
+------+--------------------+---------------+----------------+----------------------+---------+---------+----------------------------------------+-------+--------------------------+
|    1 | PRIMARY            | reader_detail | index          | PRIMARY              | PRIMARY | 4       | NULL                                   | 17682 | Using where; Using index |
|    1 | PRIMARY            | user          | eq_ref         | PRIMARY              | PRIMARY | 4       | rklocaldbmigrate.reader_detail.user_id |     1 | Using where              |
|    3 | DEPENDENT SUBQUERY | reader_log    | index_subquery | PRIMARY,user_id_date | PRIMARY | 4       | func                                   |    16 | Using index; Using where |
+------+--------------------+---------------+----------------+----------------------+---------+---------+----------------------------------------+-------+--------------------------+

查询在服务器 2 上运行不到 1 秒:集合中有 5894 行(0.09 秒)但是,在查询运行超过 1 分钟后,我不得不在服务器 1 上中止。

这里发生了什么?是因为服务器 2 使用的是 MariaDB,而服务器 1 上的是 MySQL?我在这里一无所知。同样,它是同一组数据(我从服务器 1 到服务器 2 运行了一个 mysqldump)。

【问题讨论】:

    标签: mysql


    【解决方案1】:

    解决方案是在服务器 1 上用 MariaDB 替换 MySQL。

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多