MySQL查询时间在两台不同的机器上差异很大

时间:2017-03-14 23:43:13

标签: mysql

这让我疯了。

我在两台不同的机器上拥有完全相同的数据库,一个Arch和一个Debian。我在下表中运行查询:

describe wellness;
+--------------+-------------+------+-----+---------------------+----------------+
| Field        | Type        | Null | Key | Default             | Extra              |    
+--------------+-------------+------+-----+---------------------+----------------+
| wellness_id  | int(11)     | NO   | PRI | NULL                | auto_increment |
| people_id    | int(11)     | NO   | MUL | NULL                |                |
| time_checked | timestamp   | NO   | MUL | CURRENT_TIMESTAMP   |                |
| check_type   | varchar(1)  | NO   | MUL | NULL                |                |
| username     | varchar(16) | NO   | MUL | NULL                |                |
| return_date  | timestamp   | NO   | MUL | 0000-00-00 00:00:00 |                |
| seen_by      | varchar(16) | YES  | MUL | NULL                |                |
+--------------+-------------+------+-----+---------------------+----------------+
7 rows in set (0.00 sec)

和查询:

mysql> explain select * from wellness         where wellness_id in (                 select max(wellness_id) from wellness group by people_id) and time_checked < (now() - interval 48 hour);
+----+--------------------+----------+-------+------------------+---------------+---------+------+-------+-------------+
| id | select_type        | table    | type  | possible_keys    | key           | key_len | ref  | rows  | Extra       |
+----+--------------------+----------+-------+------------------+---------------+---------+------+-------+-------------+
|  1 | PRIMARY            | wellness | ALL   | time_checked_key | NULL          | NULL    | NULL | 62546 | Using where |
|  2 | DEPENDENT SUBQUERY | wellness | index | NULL             | people_id_key | 4       | NULL |   231 | Using index |
+----+--------------------+----------+-------+------------------+---------------+---------+------+-------+-------------+
2 rows in set (0.00 sec)

在我的Debian服务器上,我正在迁移使用此数据库的应用程序,该查询需要7分钟才能运行。在我的Arch服务器上,它只需不到一秒钟。奇怪的是,我的Arch框中的EXPLAIN是不同的,我从一开始就抓取了SQL数据:

MariaDB [redacted]> explain select * from wellness         where wellness_id in (                 select max(wellness_id) from wellness group by people_id) and time_checked < (now() - interval 48 hour);
+------+--------------+-------------+--------+--------------------------+---------------+---------+------------------------------+------+--------------------------+
| id   | select_type  | table       | type   | possible_keys            | key           | key_len | ref                          | rows | Extra                    |
+------+--------------+-------------+--------+--------------------------+---------------+---------+------------------------------+------+--------------------------+
|    1 | PRIMARY      | <subquery2> | ALL    | distinct_key             | NULL          | NULL    | NULL                         |  221 |                          |
|    1 | PRIMARY      | wellness    | eq_ref | PRIMARY,time_checked_key | PRIMARY       | 4       | <subquery2>.max(wellness_id) |    1 | Using where              |
|    2 | MATERIALIZED | wellness    | range  | NULL                     | people_id_key | 4       | NULL                         |  221 | Using index for group-by |
+------+--------------+-------------+--------+--------------------------+---------------+---------+------------------------------+------+--------------------------+
3 rows in set (0.00 sec)

对于我需要调整以使其正常工作的任何想法?据我所知,两台服务器上的Apache和PHP设置完全相同,所以我觉得这可能是数据库问题。

1 个答案:

答案 0 :(得分:1)

比较

的输出
show variables LIKE 'sql_mode';

并验证my.cnf中的设置。

如果从一个服务器转储数据库并将其导入另一个服务器,则数据文件也不相同。行的内部顺序可以不同。

您也可以为您的表运行此更新优化程序统计信息

SELECT * FROM YOUR_TABLE PROCEDURE ANALYSE();