File: opt_costmodel_flush.test

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (351 lines) | stat: -rw-r--r-- 9,249 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#
# Test of the FLUSH OPTIMIZER_COSTS command
#

# To get stable cost estimates, the test should only be run with 
# 16K InnoDB page size.

CREATE TABLE t0 (
  i1 INTEGER
);

INSERT INTO t0 VALUE (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

# Create a table with 100 records each having size approximately 1000 bytes
CREATE TABLE t1 (
  pk INTEGER PRIMARY KEY,
  i1 INTEGER NOT NULL,
  c1 CHAR(250),
  c2 CHAR(250),
  c3 CHAR(250),
  c4 CHAR(250),
  INDEX i1_key (i1)
) ENGINE=InnoDB;

--echo # Since ANALYZE TABLE only reads a subset of the data, the statistics for
--echo # table t1 depends on the row order. And since the INSERT INTO ... SELECT
--echo # may be executed using different execution plans, we've added ORDER BY
--echo # to ensure that we rows has the same order every time. If not, the
--echo # estimated number of rows in EXPLAIN may change on different platforms.

INSERT INTO t1
SELECT a0.i1 + 10 * a1.i1, a0.i1, 'abc', 'def', 'ghi', 'jkl'
FROM t0 AS a0, t0 AS a1 ORDER BY a0.i1, a1.i1;

ANALYZE TABLE t1;

# Test query for table scan
let query= SELECT * FROM t1;

# Create a user connection
connect (con1,localhost,root,,);
connection con1;

# Get cost estimates with default values
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

connection default;

--echo #
--echo # Test that updating the cost constants does not influence on an
--echo # existing connection
--echo #

# Master connection: Update the value of the row_evaluate_cost
UPDATE mysql.server_cost
SET cost_value=0.4
WHERE cost_name="row_evaluate_cost";

FLUSH OPTIMIZER_COSTS;

# Switch to existing user connection and verify that doing a change
# to a cost constant does not influence on an existing connection,
# ie the cost estimates in EXPLAIN should be the same
connection con1;

--echo #
--echo # The cost estimates should not have changed
--echo #
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

disconnect con1;
connection default;

# Reset cost constants
UPDATE mysql.server_cost
SET cost_value=DEFAULT;

--echo #
--echo # Test effect of changing the row_evalute_cost
--echo #

# Master connection: Update the value of the row_evaluate_cost
UPDATE mysql.server_cost
SET cost_value=0.2
WHERE cost_name="row_evaluate_cost";

FLUSH OPTIMIZER_COSTS;

# Create a NEW connection
connect (con1,localhost,root,,);
connection con1;

--echo #
--echo # Verify that the cost estimate for evalution cost has doubled
--echo #
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

disconnect con1;
connection default;

# Reset cost constants
UPDATE mysql.server_cost
SET cost_value=DEFAULT;

--echo #
--echo # Test effect of changing key_compare_cost
--echo #
--echo # Use a query that will use loose index scan since loose index scan
--echo # is comparing keys in order to determine group boundaries.
--echo # Result from running query with default cost constants:
--echo #
let query= SELECT DISTINCT(i1) FROM t1;
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

# Master connection: Update the value of the key_compare_cost
UPDATE mysql.server_cost
SET cost_value=0.1
WHERE cost_name="key_compare_cost";

FLUSH OPTIMIZER_COSTS;

# Create NEW connection
connect (con1,localhost,root,,);
connection con1;

--echo #
--echo # Verify that the cost estimate for read cost is increased
--echo #
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

disconnect con1;
connection default;

# Reset cost constants
UPDATE mysql.server_cost
SET cost_value=DEFAULT;

--echo #
--echo # Test effect of changing memory_temptable_create_cost
--echo #
--echo # Use a query that creates a temporary table for a subquery.
--echo # Run query with default cost constants.
--echo #
let query=
SELECT * FROM t1
WHERE i1 IN (SELECT i1 FROM t1);
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

# Master connection:
# Update the value of the memory_temptable_create_cost from default (1.0)
# to 10.0 (up by 9.0)
UPDATE mysql.server_cost
SET cost_value=10.0
WHERE cost_name="memory_temptable_create_cost";

FLUSH OPTIMIZER_COSTS;

# Create NEW connection
connect (con1,localhost,root,,);
connection con1;

--echo #
--echo # Verify that the cost estimate for the query is incrased (by 9.0)
--echo #
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

disconnect con1;
connection default;

# Reset cost constants
UPDATE mysql.server_cost
SET cost_value=DEFAULT;

--echo #
--echo # Test effect of changing memory_temptable_row_cost
--echo #
--echo # Use a query that creates a temporary table for a subquery.
--echo # Result from running query with default cost constants:
--echo #
let query=
SELECT * FROM t1
WHERE i1 IN (SELECT i1 FROM t1);
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

# Master connection:
# Update the value of the memory_temptable_row_cost from default (0.1)
# to 0.2
UPDATE mysql.server_cost
SET cost_value=0.2
WHERE cost_name="memory_temptable_row_cost";

FLUSH OPTIMIZER_COSTS;

# Create NEW connection
connect (con1,localhost,root,,);
connection con1;

--echo #
--echo # Verify that the cost estimate for the query is incrased by:
--echo # 100 rows that needs to be inserted and read from the temporary table.
--echo # The cost of each insert and read is increased by 0.1, ie cost should
--echo # be increased by 100 * 2 * 0.1 = 20.
--echo #
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

disconnect con1;
connection default;

# Reset cost constants
UPDATE mysql.server_cost
SET cost_value=DEFAULT;

# The next two tests are for cost of disk based temporary tables
# To ensure this temp table is written to a disk based storage engine
set global internal_tmp_mem_storage_engine='memory';
set session internal_tmp_mem_storage_engine='memory';
set @max_heap_table_size_save= @@max_heap_table_size;
set max_heap_table_size= 16384;

--echo #
--echo # Test effect of changing disk_temptable_create_cost
--echo #
--echo # Use a query that creates a temporary table for a subquery.
--echo # Result from running query with default cost constants:
--echo #
let query=
SELECT * FROM t1
WHERE c1 IN (SELECT c1 FROM t1);
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

# Master connection:
# Update the value of the disk_temptable_create_cost from default (20.0)
# to 40.0
UPDATE mysql.server_cost
SET cost_value=40.0
WHERE cost_name="disk_temptable_create_cost";

FLUSH OPTIMIZER_COSTS;

# Create NEW connection
connect (con1,localhost,root,,);
connection con1;
# To ensure a disk temp table is used
set max_heap_table_size= 16384;  

--echo #
--echo # Verify that the cost estimate for the query is incrased (by 20.0)
--echo #
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

disconnect con1;
connection default;

# Reset cost constants
UPDATE mysql.server_cost
SET cost_value=DEFAULT;

--echo #
--echo # Test effect of changing disk_temptable_row_cost
--echo #
--echo # Use a query that creates a temporary table for a subquery
--echo # Result from running query with default cost constants:
--echo #
let query=
SELECT * FROM t1
WHERE c1 IN (SELECT c1 FROM t1);
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

# Master connection:
# Update the value of the disk_temptable_row_cost from default (0.5)
# to 1.0
UPDATE mysql.server_cost
SET cost_value=1.0
WHERE cost_name="disk_temptable_row_cost";

FLUSH OPTIMIZER_COSTS;

# Create NEW connection
connect (con1,localhost,root,,);
connection con1;
set max_heap_table_size= 16384;

--echo #
--echo # Verify that the cost estimate for the query is incrased by:
--echo # 100 rows that needs to be inserted and read from the temporary table.
--echo # The cost of each insert and read is increased by 0.5, ie cost should
--echo # be increased by 100 * 2 * 0.5 = 100.
--echo #
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

disconnect con1;
connection default;

# Reset cost constants
UPDATE mysql.server_cost
SET cost_value=DEFAULT;

# Finished testing disk based temporary tables
set max_heap_table_size= @max_heap_table_size_save;
set session internal_tmp_mem_storage_engine=default;
set global internal_tmp_mem_storage_engine=default;

--echo #
--echo # Test effect of changing the memory_block_read_cost
--echo #
--echo # Result from running query with default cost constants:
--echo #
let query= SELECT * FROM t1;
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

# Master connection: Update the value of the memory_block_read_cost
UPDATE mysql.engine_cost
SET cost_value=0.5
WHERE cost_name="memory_block_read_cost";

FLUSH OPTIMIZER_COSTS;

# Create NEW connection
connect (con1,localhost,root,,);
connection con1;

--echo #
--echo # Verify that the cost estimate for read cost has doubled
--echo #
--skip_if_hypergraph  # Depends on the query plan.
eval EXPLAIN FORMAT=JSON $query;

disconnect con1;
connection default;

# Reset cost constants
UPDATE mysql.engine_cost
SET cost_value=DEFAULT;

FLUSH OPTIMIZER_COSTS;

DROP TABLE t0,t1;