Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 相同数据的相同查询,不同性能_Mysql - Fatal编程技术网

Mysql 相同数据的相同查询,不同性能

Mysql 相同数据的相同查询,不同性能,mysql,Mysql,我正在两台不同的服务器上使用相同的数据集运行SQL查询 服务器1:mysql版本14.14发行版5.1.73,用于使用readline 5.1的未知linux gnu(x86_64) 服务器2:mysql版本15.1发行版10.0.12-MariaDB,用于使用readline 5.1的osx10.10(x86_64) 查询是: SELECT * FROM `user` WHERE user_id IN (SELECT user_id FROM reader_detail) AND `us

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

服务器1:mysql版本14.14发行版5.1.73,用于使用readline 5.1的未知linux gnu(x86_64)

服务器2:mysql版本15.1发行版10.0.12-MariaDB,用于使用readline 5.1的osx10.10(x86_64)

查询是:

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;
解释在服务器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运行了一个mysqldump到服务器2)。

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

+------+--------------------+---------------+----------------+----------------------+---------+---------+----------------------------------------+-------+--------------------------+
| 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 |
+------+--------------------+---------------+----------------+----------------------+---------+---------+----------------------------------------+-------+--------------------------+