如何触发Mysql并行查询功能启动?

How to trigger Mysql parallel query function to start?

MySQL 8.0.19

mysql> show variables like 'innodb_parallel_read_threads';
+------------------------------+-------+
| Variable_name                | Value |
+------------------------------+-------+
| innodb_parallel_read_threads | 4     |
+------------------------------+-------+
1 row in set (0.00 sec)

从执行计划来看,cost值虽然高,但不会触发并行查询

mysql> explain analyze SELECT count(a.name) FROM b,a WHERE b.id = a.id AND a.id < 10000\G
*************************** 1. row ***************************
EXPLAIN: -> Aggregate: count(a.`name`)  (actual time=79199.970..79199.971 rows=1 loops=1)
    -> Inner hash join (b.id = a.id)  (cost=29804029261222.55 rows=29803521726988) (actual time=52129.791..79198.823 rows=9999 loops=1)
        -> Table scan on b  (cost=31.81 rows=18758239) (actual time=0.385..19630.712 rows=20000000 loops=1)
        -> Hash
            -> Filter: (a.id < 10000)  (cost=4909601.51 rows=15888229) (actual time=2.266..52117.583 rows=9999 loops=1)
                -> Table scan on a  (cost=4909601.51 rows=47669458) (actual time=2.262..48985.413 rows=50000000 loops=1)

1 row in set (1 min 19.25 sec)

并行读取的当前用例非常有限。而 documentation

innodb_parallel_read_threads

Defines the number of threads that can be used for parallel clustered index reads. Parallel scanning of partitions is supported as of MySQL 8.0.17. Parallel read threads can improve CHECK TABLE performance.

有点模糊,feature documentation更详细地描述了当前的用例:

FR1: SELECT COUNT(*) FROM TABLE T; will scan the index in parallel only if the scan is a non-locking scan and --innodb-parallel-read-threads > 1. Otherwise it will fallback to the old row by row scan.

FR2: The second phase of CHECK TABLE T; will also do a parallel scan.

FR3: Support MVCC semantics.

FR4: New session level variable --innodb-parallel-read-threads to control the number of threads to use for parallel SELECT COUNT(*) ...;

  1. Minimum value 1
  2. Default value 4
  3. Maximum value 256.

NFR1: Speed up should be 10x for SELECT COUNT(*) FROM t; on relevant hardware (e.g., tetra02).

简而言之:您应该只看到 select count(*) from table 的改进(没有任何 where 子句)。并且在执行计划中也不会提及,目前只能通过innodb_parallel_read_threads.

不同值的执行时间对比来看效果

这是一项新功能,目前的状态基本上是 laying the groundwork 未来的发展:

Current scope is limited to providing sufficient infrastructure for DDL operations to read the data in parallel. Making the second phase of CHECK TABLE parallel is an added bonus for now. This speeds up CHECK TABLE a little.

开发人员将测试并逐步扩展此功能(例如 MySQL8.0.17 添加了对分区表的支持,并且对二级索引的支持似乎正在进行中),但可能需要一段时间while until your query will be able to use it.