|
| 1 | +# |
| 2 | +# MDEV-39492 Parallel Query: the I/O term divides only by what has to be read. |
| 3 | +# |
| 4 | +# The workers overlap each other's reads, so the I/O part of a scan's cost is |
| 5 | +# divided among them. But a page the engine already holds in its buffer pool is |
| 6 | +# fetched from memory, and there is no read latency to overlap, so a scan of a |
| 7 | +# table that fits in the pool takes its parallelism from the CPU and row-copy |
| 8 | +# terms and nothing from the I/O one. scale_cost_for_parallel_scan() therefore |
| 9 | +# divides the I/O term by the fraction of the table that cannot be resident, and |
| 10 | +# the other two terms by the worker count as before. |
| 11 | +# |
| 12 | +# The ratio is taken from innodb_buffer_pool_size and the table's size on disk, |
| 13 | +# never from what the pool happens to hold, so costing a query twice gives the |
| 14 | +# same answer. That is why the two tables here differ in size rather than the |
| 15 | +# test warming or flushing anything. |
| 16 | +# |
| 17 | +# The divisor reaches the worker count only for a table much larger than the |
| 18 | +# pool, which is where a parallel scan is worth the most: measured on TPC-H SF1 |
| 19 | +# Q6, LINEITEM at 1176 MB against a 128 MB pool went from 885 MB/s serial to |
| 20 | +# 4210 MB/s at a hundred workers. |
| 21 | +# |
| 22 | +# |
| 23 | +# Adapted on import: the probe queries select a column rather than COUNT(*). |
| 24 | +# COUNT(*) was only a way to run the query without printing rows, and this tree's |
| 25 | +# gate does not admit an aggregate. |
| 26 | +# |
| 27 | +--source include/have_innodb.inc |
| 28 | +--source include/have_sequence.inc |
| 29 | +--source include/not_embedded.inc |
| 30 | + |
| 31 | +SET @s= @@session.parallel_worker_threads; |
| 32 | +SET @t= @@session.optimizer_trace; |
| 33 | + |
| 34 | +--echo # the buffer pool the two tables are measured against |
| 35 | +SELECT @@global.innodb_buffer_pool_size <= 16*1024*1024 AS pool_is_the_mtr_default; |
| 36 | + |
| 37 | +CREATE TABLE t_small (pk INT PRIMARY KEY, a INT, f CHAR(50) CHARACTER SET latin1) |
| 38 | + ENGINE=InnoDB; |
| 39 | +INSERT INTO t_small SELECT seq, seq % 97, REPEAT('x',50) FROM seq_1_to_20000; |
| 40 | + |
| 41 | +CREATE TABLE t_big (pk INT PRIMARY KEY, a INT, f CHAR(250) CHARACTER SET latin1) |
| 42 | + ENGINE=InnoDB; |
| 43 | +INSERT INTO t_big SELECT seq, seq % 97, REPEAT('y',250) FROM seq_1_to_120000; |
| 44 | + |
| 45 | +ANALYZE TABLE t_small, t_big PERSISTENT FOR ALL; |
| 46 | + |
| 47 | +--echo # |
| 48 | +--echo # t_small fits in the pool and t_big is several times its size |
| 49 | +--echo # |
| 50 | +SELECT (SELECT data_length FROM information_schema.tables |
| 51 | + WHERE table_schema='test' AND table_name='t_small') |
| 52 | + < @@global.innodb_buffer_pool_size AS small_fits_in_pool, |
| 53 | + (SELECT data_length FROM information_schema.tables |
| 54 | + WHERE table_schema='test' AND table_name='t_big') |
| 55 | + > @@global.innodb_buffer_pool_size AS big_exceeds_pool; |
| 56 | + |
| 57 | +SET SESSION parallel_worker_threads= 8; |
| 58 | +SET SESSION optimizer_trace='enabled=on'; |
| 59 | + |
| 60 | +--echo # |
| 61 | +--echo # A table that fits in the pool is still scanned in parallel -- the CPU and |
| 62 | +--echo # row-copy terms still divide -- but its I/O term is not discounted at all, |
| 63 | +--echo # so the divisor is exactly 1. |
| 64 | +--echo # |
| 65 | +--disable_result_log |
| 66 | +SELECT pk FROM t_small WHERE a = 5; |
| 67 | +--enable_result_log |
| 68 | +SELECT CAST(JSON_VALUE(JSON_EXTRACT(trace,'$**.parallel_scan_workers'),'$[0]') AS UNSIGNED) |
| 69 | + AS workers, |
| 70 | + CAST(JSON_VALUE(JSON_EXTRACT(trace,'$**.parallel_scan_io_divisor'),'$[0]') AS DOUBLE) |
| 71 | + AS io_divisor |
| 72 | + FROM information_schema.optimizer_trace; |
| 73 | + |
| 74 | +--echo # |
| 75 | +--echo # A table several times the pool divides its I/O term, up to the worker |
| 76 | +--echo # count. The exact figure follows the table's size on disk, so what is |
| 77 | +--echo # checked is the relationship rather than the number. |
| 78 | +--echo # |
| 79 | +--disable_result_log |
| 80 | +SELECT pk FROM t_big WHERE a = 5; |
| 81 | +--enable_result_log |
| 82 | +SELECT CAST(JSON_VALUE(JSON_EXTRACT(trace,'$**.parallel_scan_workers'),'$[0]') AS UNSIGNED) |
| 83 | + AS workers, |
| 84 | + CAST(JSON_VALUE(JSON_EXTRACT(trace,'$**.parallel_scan_io_divisor'),'$[0]') AS DOUBLE) > 1 |
| 85 | + AS io_term_divides, |
| 86 | + CAST(JSON_VALUE(JSON_EXTRACT(trace,'$**.parallel_scan_io_divisor'),'$[0]') AS DOUBLE) <= |
| 87 | + CAST(JSON_VALUE(JSON_EXTRACT(trace,'$**.parallel_scan_workers'),'$[0]') AS UNSIGNED) |
| 88 | + AS at_most_the_workers |
| 89 | + FROM information_schema.optimizer_trace; |
| 90 | + |
| 91 | +--echo # |
| 92 | +--echo # and it grows with the worker count, since it is the workers that overlap |
| 93 | +--echo # the reads |
| 94 | +--echo # |
| 95 | +SET SESSION parallel_worker_threads= 4; |
| 96 | +--disable_result_log |
| 97 | +SELECT pk FROM t_big WHERE a = 5; |
| 98 | +--enable_result_log |
| 99 | +SELECT CAST(JSON_VALUE(JSON_EXTRACT(trace,'$**.parallel_scan_io_divisor'),'$[0]') AS DOUBLE) |
| 100 | + INTO @d4 FROM information_schema.optimizer_trace; |
| 101 | +SET SESSION parallel_worker_threads= 16; |
| 102 | +--disable_result_log |
| 103 | +SELECT pk FROM t_big WHERE a = 5; |
| 104 | +--enable_result_log |
| 105 | +SELECT CAST(JSON_VALUE(JSON_EXTRACT(trace,'$**.parallel_scan_io_divisor'),'$[0]') AS DOUBLE) |
| 106 | + INTO @d16 FROM information_schema.optimizer_trace; |
| 107 | +SELECT @d4 < @d16 AS more_workers_divide_more, |
| 108 | + ROUND(@d16/@d4,1) AS ratio_16_to_4_workers; |
| 109 | + |
| 110 | +--echo # |
| 111 | +--echo # with parallelism off there is no parallel scan to cost, so no term |
| 112 | +--echo # |
| 113 | +SET SESSION parallel_worker_threads= 0; |
| 114 | +--disable_result_log |
| 115 | +SELECT pk FROM t_big WHERE a = 5; |
| 116 | +--enable_result_log |
| 117 | +SELECT JSON_EXTRACT(trace,'$**.parallel_scan_io_divisor') IS NULL AS no_term |
| 118 | + FROM information_schema.optimizer_trace; |
| 119 | + |
| 120 | +DROP TABLE t_small, t_big; |
| 121 | +SET SESSION optimizer_trace= @t; |
| 122 | +SET SESSION parallel_worker_threads= @s; |
0 commit comments