MySQL 5.7中的查询比5.1中的查询慢2倍或更多倍

时间:2018-07-13 15:24:21

标签: mysql query-optimization

我们将完整的数据库从MySQL 5.1.63迁移到另一台服务器(更好一点)到MySQL 5.7.22。现在,大多数查询的平均速度要慢2倍。对于MySQL 5.1,我们还没有做太多优化。

这是我们已更改的配置:

s$.pipe(
    map((x) => x + 1),
    mergeMap(x => from(alreadyExist(x)).pipe(
      filter(Boolean),
      mapTo(x),
    )),
  )
  .subscribe(...)

这是一个具体的例子:

我想让所有被标记为课程老师的人

table_open_cache            = 4096
tmp_table_size=256M
max_heap_table_size=256M
query_cache_limit       = 1000000
query_cache_size        = 32000000
innodb_buffer_pool_size = 3200M
innodb_log_buffer_size  = 1024M

运行时间:

  • mysql 5.1:0.03秒。后续查询:0.00秒
  • mysql 5.7:1.27s。后续查询:0.80秒

相差40倍以上。

在5.1中说明:

SELECT id, TRIM(CONCAT_WS(" ", name, lastname)) AS name
FROM person
WHERE id IN (SELECT DISTINCT teacher_id FROM course)
ORDER BY name;

在5.7版中解释:

+----+--------------------+----------+----------------+--------------------+--------------------+---------+------+-------+-----------------------------+
| id | select_type        | table    | type           | possible_keys      | key                | key_len | ref  | rows  | Extra                       |
+----+--------------------+----------+----------------+--------------------+--------------------+---------+------+-------+-----------------------------+
|  1 | PRIMARY            | person   | ALL            | NULL               | NULL               | NULL    | NULL | 16293 | Using where; Using filesort |
|  2 | DEPENDENT SUBQUERY | course   | index_subquery | teacher_id         | teacher_id         | 5       | func |  2677 | Using index; Using where    |
+----+--------------------+----------+----------------+--------------------+--------------------+---------+------+-------+-----------------------------+

现在,如果我这样编写查询:

+----+--------------+-------------+------------+--------+--------------------+--------------------+---------+----------------------+--------+----------+-------------+
| id | select_type  | table       | partitions | type   | possible_keys      | key                | key_len | ref                  | rows   | filtered | Extra       |
+----+--------------+-------------+------------+--------+--------------------+--------------------+---------+----------------------+--------+----------+-------------+
|  1 | SIMPLE       | person      | NULL       | ALL    | PRIMARY            | NULL               | NULL    | NULL                 |  16491 |   100.00 | Using where |
|  1 | SIMPLE       | <subquery2> | NULL       | eq_ref | <auto_key>         | <auto_key>         | 5       | db.person.id         |      1 |   100.00 | Using where |
|  2 | MATERIALIZED | course      | NULL       | index  | teacher_id         | teacher_id         | 5       | NULL                 | 109741 |   100.00 | Using index |
+----+--------------+-------------+------------+--------+--------------------+--------------------+---------+----------------------+--------+----------+-------------+

时间将变为:

  • mysql 5.1:0.01秒
  • mysql 5.7:0.03秒

所以,它虽然好一点,但仍然慢一些。

select distinct person.id, trim(concat_ws(" ", name, lastname)) as name from person, course where person.id = course.teacher_id order by name; 有一个索引。

安装之间的差异之一是,在5.7版服务器中,数据文件夹位于另一个驱动器上(SSD,性能比5.1版服务器中的驱动器要好一些)。

任何建议如何配置才能使5.7达到与5.1相同的速度?也许某些查询也必须重写,但我认为配置似乎很有必要。

1 个答案:

答案 0 :(得分:1)

您可以通过内部联接使用更好的查询优化

  SELECT id, TRIM(CONCAT_WS(" ", name, lastname)) AS name
  FROM person
  INNER JOIN (
    SELECT DISTINCT teacher_id FROM course
  ) t on  t.teacher_id = person.id 

  ORDER BY name;

,并确保您测试的条件是相同的..相同的数据..并且首次执行两个查询