MySQL 8.0新特性-并行查询innodb_parallel_read_threads

来源:这里教程网 时间:2026-03-01 15:08:24 作者:

原文: https://www.percona.com/blog/2019/01/23/mysql-8-0-14-a-road-to-parallel-query-execution-is-wide-open/原文很好翻译,直接读好理解 长久以来MySQL没有并行查询,并且在其他数据库已经有了的情况下,MySQL终于在8.0.14版本开始有了自己的并行查询,但使用面非常的窄,只适用于并行聚集索引的count(*) 并且只是在没有where条件的情况下的查询

mysql> set local innodb_parallel_read_threads=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from ontime;
+-----------+
| count(*)  |
+-----------+
| 177920306 |
+-----------+
1 row in set (2 min 33.93 sec)
mysql> set local innodb_parallel_read_threads=DEFAULT; -- 4 is default
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from ontime;
+-----------+
| count(*)  |
+-----------+
| 177920306 |
+-----------+
1 row in set (21.85 sec)
mysql> set local innodb_parallel_read_threads=32;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from ontime;
+-----------+
| count(*)  |
+-----------+
| 177920306 |
+-----------+
1 row in set (5.35 sec)

任何事情没有一开始就完美,而是日复一日的坚持,对MySQL来说,这是一个很好的开端,并为真正的并行查询执行开辟了一条道路。 下面是我的测试结果

mysql>set local innodb_parallel_read_threads = 1;
执行成功,耗时:8 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:2275 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:2316 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:2191 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:2196 ms.
mysql>set local innodb_parallel_read_threads = 16;
执行成功,耗时:8 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:594 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:557 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:570 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:594 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:582 ms.
mysql>set local innodb_parallel_read_threads=32;
执行成功,耗时:9 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:265 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:251 ms.
mysql>set local innodb_parallel_read_threads=64;
执行成功,耗时:9 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:340 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:363 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:306 ms.
mysql>set local innodb_parallel_read_threads=32;
执行成功,耗时:9 ms.
mysql>select count(*) from PARALLELTEST;
+--------------------+
| count(*)           |
+--------------------+
| 9175040            |
+--------------------+
返回行数:[1],耗时:276 ms.

结论:和文章中的结论一致,但是我参数设置到64的360ms 时反而比32时200多ms慢,也是符合预期的,与Oracle类似

相关推荐