Skip to content

Commit 6750186

Browse files
author
Olav Sandstaa
committed
WL#7315 Optimizer cost model: main memory management of cost constants
This worklog implements the following main components: 1. A main memory representation of the "set of cost constants". This contains the cost constants read from the cost constant tables stored in the mysql database (see WL#7276). For cost constants that do not have entries in the cost constant tables, default values defined in the source code will be used. 2. A cache for "cost constant sets". This is responsible for creating the set of cost constant, giving new sessions access to the latest versions of the cost constants, and for re-reading the cost constant tables in the case where these have been updated.
1 parent 3b0b1b1 commit 6750186

32 files changed

+3374
-98
lines changed

include/sql_string.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ class String
227227
return lex_string;
228228
}
229229

230+
LEX_CSTRING lex_cstring() const
231+
{
232+
LEX_CSTRING lex_cstring = { ptr(), length() };
233+
return lex_cstring;
234+
}
235+
230236
void set(String &str,size_t offset, size_t arg_length)
231237
{
232238
DBUG_ASSERT(&str != this);
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
SELECT cost_name,cost_value FROM mysql.server_cost;
2+
cost_name cost_value
3+
disk_temptable_create_cost NULL
4+
disk_temptable_row_cost NULL
5+
key_compare_cost NULL
6+
memory_temptable_create_cost NULL
7+
memory_temptable_row_cost NULL
8+
row_evaluate_cost NULL
9+
SELECT engine_name,cost_name,cost_value FROM mysql.engine_cost;
10+
engine_name cost_name cost_value
11+
default io_block_read_cost NULL
12+
CREATE TABLE t0 (
13+
i1 INTEGER
14+
);
15+
INSERT INTO t0 VALUE (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
16+
CREATE TABLE t1_innodb (
17+
pk INTEGER PRIMARY KEY,
18+
i1 INTEGER NOT NULL,
19+
c1 CHAR(250),
20+
c2 CHAR(250),
21+
c3 CHAR(250),
22+
c4 CHAR(250),
23+
INDEX i1_key (i1)
24+
) ENGINE=InnoDB;
25+
INSERT INTO t1_innodb
26+
SELECT a0.i1 + 10 * a1.i1, a0.i1, 'abc', 'def', 'ghi', 'jkl'
27+
FROM t0 AS a0, t0 AS a1;
28+
CREATE TABLE t1_myisam (
29+
pk INTEGER PRIMARY KEY,
30+
i1 INTEGER NOT NULL,
31+
c1 CHAR(250),
32+
c2 CHAR(250),
33+
c3 CHAR(250),
34+
c4 CHAR(250),
35+
INDEX i1_key (i1)
36+
) ENGINE=MyISAM;
37+
INSERT INTO t1_myisam
38+
SELECT a0.i1 + 10 * a1.i1, a0.i1, 'abc', 'def', 'ghi', 'jkl'
39+
FROM t0 AS a0, t0 AS a1;
40+
ANALYZE TABLE t1_innodb, t1_myisam;
41+
Table Op Msg_type Msg_text
42+
test.t1_innodb analyze status OK
43+
test.t1_myisam analyze status Table is already up to date
44+
"Explain with cost estimate against InnoDB"
45+
EXPLAIN FORMAT=JSON SELECT * FROM t1_innodb;
46+
EXPLAIN
47+
{
48+
"query_block": {
49+
"select_id": 1,
50+
"cost_info": {
51+
"query_cost": "29.00"
52+
},
53+
"table": {
54+
"table_name": "t1_innodb",
55+
"access_type": "ALL",
56+
"rows_examined_per_scan": 100,
57+
"rows_produced_per_join": 100,
58+
"filtered": 100,
59+
"cost_info": {
60+
"read_cost": "9.00",
61+
"eval_cost": "20.00",
62+
"prefix_cost": "29.00",
63+
"data_read_per_join": "99K"
64+
},
65+
"used_columns": [
66+
"pk",
67+
"i1",
68+
"c1",
69+
"c2",
70+
"c3",
71+
"c4"
72+
]
73+
}
74+
}
75+
}
76+
Warnings:
77+
Note 1003 /* select#1 */ select `test`.`t1_innodb`.`pk` AS `pk`,`test`.`t1_innodb`.`i1` AS `i1`,`test`.`t1_innodb`.`c1` AS `c1`,`test`.`t1_innodb`.`c2` AS `c2`,`test`.`t1_innodb`.`c3` AS `c3`,`test`.`t1_innodb`.`c4` AS `c4` from `test`.`t1_innodb`
78+
"Explain with cost estimate against MyISAM"
79+
EXPLAIN FORMAT=JSON SELECT * FROM t1_myisam;
80+
EXPLAIN
81+
{
82+
"query_block": {
83+
"select_id": 1,
84+
"cost_info": {
85+
"query_cost": "46.63"
86+
},
87+
"table": {
88+
"table_name": "t1_myisam",
89+
"access_type": "ALL",
90+
"rows_examined_per_scan": 100,
91+
"rows_produced_per_join": 100,
92+
"filtered": 100,
93+
"cost_info": {
94+
"read_cost": "26.63",
95+
"eval_cost": "20.00",
96+
"prefix_cost": "46.63",
97+
"data_read_per_join": "99K"
98+
},
99+
"used_columns": [
100+
"pk",
101+
"i1",
102+
"c1",
103+
"c2",
104+
"c3",
105+
"c4"
106+
]
107+
}
108+
}
109+
}
110+
Warnings:
111+
Note 1003 /* select#1 */ select `test`.`t1_myisam`.`pk` AS `pk`,`test`.`t1_myisam`.`i1` AS `i1`,`test`.`t1_myisam`.`c1` AS `c1`,`test`.`t1_myisam`.`c2` AS `c2`,`test`.`t1_myisam`.`c3` AS `c3`,`test`.`t1_myisam`.`c4` AS `c4` from `test`.`t1_myisam`
112+
UPDATE mysql.server_cost
113+
SET cost_value=0.4
114+
WHERE cost_name="row_evaluate_cost";
115+
UPDATE mysql.engine_cost
116+
SET cost_value=2.0
117+
WHERE cost_name="io_block_read_cost";
118+
"Restarting MySQL server"
119+
# restart
120+
"MySQL restarted"
121+
SELECT cost_name,cost_value FROM mysql.server_cost;
122+
cost_name cost_value
123+
disk_temptable_create_cost NULL
124+
disk_temptable_row_cost NULL
125+
key_compare_cost NULL
126+
memory_temptable_create_cost NULL
127+
memory_temptable_row_cost NULL
128+
row_evaluate_cost 0.4
129+
SELECT engine_name, cost_name,cost_value FROM mysql.engine_cost;
130+
engine_name cost_name cost_value
131+
default io_block_read_cost 2
132+
"Explain with cost estimate against InnoDB"
133+
EXPLAIN FORMAT=JSON SELECT * FROM t1_innodb;
134+
EXPLAIN
135+
{
136+
"query_block": {
137+
"select_id": 1,
138+
"cost_info": {
139+
"query_cost": "58.00"
140+
},
141+
"table": {
142+
"table_name": "t1_innodb",
143+
"access_type": "ALL",
144+
"rows_examined_per_scan": 100,
145+
"rows_produced_per_join": 100,
146+
"filtered": 100,
147+
"cost_info": {
148+
"read_cost": "18.00",
149+
"eval_cost": "40.00",
150+
"prefix_cost": "58.00",
151+
"data_read_per_join": "99K"
152+
},
153+
"used_columns": [
154+
"pk",
155+
"i1",
156+
"c1",
157+
"c2",
158+
"c3",
159+
"c4"
160+
]
161+
}
162+
}
163+
}
164+
Warnings:
165+
Note 1003 /* select#1 */ select `test`.`t1_innodb`.`pk` AS `pk`,`test`.`t1_innodb`.`i1` AS `i1`,`test`.`t1_innodb`.`c1` AS `c1`,`test`.`t1_innodb`.`c2` AS `c2`,`test`.`t1_innodb`.`c3` AS `c3`,`test`.`t1_innodb`.`c4` AS `c4` from `test`.`t1_innodb`
166+
"Explain with cost estimate against MyISAM"
167+
EXPLAIN FORMAT=JSON SELECT * FROM t1_myisam;
168+
EXPLAIN
169+
{
170+
"query_block": {
171+
"select_id": 1,
172+
"cost_info": {
173+
"query_cost": "93.27"
174+
},
175+
"table": {
176+
"table_name": "t1_myisam",
177+
"access_type": "ALL",
178+
"rows_examined_per_scan": 100,
179+
"rows_produced_per_join": 100,
180+
"filtered": 100,
181+
"cost_info": {
182+
"read_cost": "53.27",
183+
"eval_cost": "40.00",
184+
"prefix_cost": "93.27",
185+
"data_read_per_join": "99K"
186+
},
187+
"used_columns": [
188+
"pk",
189+
"i1",
190+
"c1",
191+
"c2",
192+
"c3",
193+
"c4"
194+
]
195+
}
196+
}
197+
}
198+
Warnings:
199+
Note 1003 /* select#1 */ select `test`.`t1_myisam`.`pk` AS `pk`,`test`.`t1_myisam`.`i1` AS `i1`,`test`.`t1_myisam`.`c1` AS `c1`,`test`.`t1_myisam`.`c2` AS `c2`,`test`.`t1_myisam`.`c3` AS `c3`,`test`.`t1_myisam`.`c4` AS `c4` from `test`.`t1_myisam`
200+
UPDATE mysql.server_cost
201+
SET cost_value=DEFAULT;
202+
UPDATE mysql.engine_cost
203+
SET cost_value=DEFAULT;
204+
INSERT INTO mysql.engine_cost VALUES
205+
("InnoDB", 0, "io_block_read_cost", 4.0, CURRENT_TIMESTAMP, DEFAULT);
206+
"Restarting MySQL server"
207+
# restart
208+
"MySQL restarted"
209+
"Explain with cost estimate against InnoDB"
210+
EXPLAIN FORMAT=JSON SELECT * FROM t1_innodb;
211+
EXPLAIN
212+
{
213+
"query_block": {
214+
"select_id": 1,
215+
"cost_info": {
216+
"query_cost": "56.00"
217+
},
218+
"table": {
219+
"table_name": "t1_innodb",
220+
"access_type": "ALL",
221+
"rows_examined_per_scan": 100,
222+
"rows_produced_per_join": 100,
223+
"filtered": 100,
224+
"cost_info": {
225+
"read_cost": "36.00",
226+
"eval_cost": "20.00",
227+
"prefix_cost": "56.00",
228+
"data_read_per_join": "99K"
229+
},
230+
"used_columns": [
231+
"pk",
232+
"i1",
233+
"c1",
234+
"c2",
235+
"c3",
236+
"c4"
237+
]
238+
}
239+
}
240+
}
241+
Warnings:
242+
Note 1003 /* select#1 */ select `test`.`t1_innodb`.`pk` AS `pk`,`test`.`t1_innodb`.`i1` AS `i1`,`test`.`t1_innodb`.`c1` AS `c1`,`test`.`t1_innodb`.`c2` AS `c2`,`test`.`t1_innodb`.`c3` AS `c3`,`test`.`t1_innodb`.`c4` AS `c4` from `test`.`t1_innodb`
243+
"Explain with cost estimate against MyISAM"
244+
EXPLAIN FORMAT=JSON SELECT * FROM t1_myisam;
245+
EXPLAIN
246+
{
247+
"query_block": {
248+
"select_id": 1,
249+
"cost_info": {
250+
"query_cost": "46.63"
251+
},
252+
"table": {
253+
"table_name": "t1_myisam",
254+
"access_type": "ALL",
255+
"rows_examined_per_scan": 100,
256+
"rows_produced_per_join": 100,
257+
"filtered": 100,
258+
"cost_info": {
259+
"read_cost": "26.63",
260+
"eval_cost": "20.00",
261+
"prefix_cost": "46.63",
262+
"data_read_per_join": "99K"
263+
},
264+
"used_columns": [
265+
"pk",
266+
"i1",
267+
"c1",
268+
"c2",
269+
"c3",
270+
"c4"
271+
]
272+
}
273+
}
274+
}
275+
Warnings:
276+
Note 1003 /* select#1 */ select `test`.`t1_myisam`.`pk` AS `pk`,`test`.`t1_myisam`.`i1` AS `i1`,`test`.`t1_myisam`.`c1` AS `c1`,`test`.`t1_myisam`.`c2` AS `c2`,`test`.`t1_myisam`.`c3` AS `c3`,`test`.`t1_myisam`.`c4` AS `c4` from `test`.`t1_myisam`
277+
DELETE FROM mysql.engine_cost
278+
WHERE engine_name NOT LIKE "default";
279+
"Restarting MySQL server"
280+
# restart
281+
DROP TABLE t0, t1_innodb, t1_myisam;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
SELECT * FROM mysql.server_cost;
2+
cost_name cost_value last_update comment
3+
disk_temptable_create_cost NULL # NULL
4+
disk_temptable_row_cost NULL # NULL
5+
key_compare_cost NULL # NULL
6+
memory_temptable_create_cost NULL # NULL
7+
memory_temptable_row_cost NULL # NULL
8+
row_evaluate_cost NULL # NULL
9+
SELECT * FROM mysql.engine_cost;
10+
engine_name device_type cost_name cost_value last_update comment
11+
default 0 io_block_read_cost NULL # NULL
12+
RENAME TABLE mysql.engine_cost TO mysql.engine_cost_renamed;
13+
"Restarting MySQL server"
14+
# restart
15+
"MySQL restarted"
16+
RENAME TABLE mysql.engine_cost_renamed TO mysql.engine_cost;
17+
SELECT * FROM mysql.engine_cost;
18+
engine_name device_type cost_name cost_value last_update comment
19+
default 0 io_block_read_cost NULL # NULL
20+
INSERT INTO mysql.server_cost
21+
VALUES ("lunch_cost", 1.0, CURRENT_TIMESTAMP, "Lunch is important");
22+
UPDATE mysql.server_cost
23+
SET cost_value=-1.0
24+
WHERE cost_name="row_evaluate_cost";
25+
UPDATE mysql.server_cost
26+
SET cost_value=0.0
27+
WHERE cost_name="key_compare_cost";
28+
"Restarting MySQL server"
29+
# restart
30+
"MySQL restarted"
31+
DELETE FROM mysql.server_cost
32+
WHERE cost_name LIKE "lunch_cost%";
33+
UPDATE mysql.server_cost
34+
SET cost_value=NULL
35+
WHERE cost_name="row_evaluate_cost";
36+
UPDATE mysql.server_cost
37+
SET cost_value=NULL
38+
WHERE cost_name="key_compare_cost";
39+
INSERT INTO mysql.engine_cost
40+
VALUES ("InnoDB", 0, "lunch_cost", 1.0, CURRENT_TIMESTAMP, "Lunch is important");
41+
UPDATE mysql.engine_cost
42+
SET cost_value=0.0
43+
WHERE cost_name="io_block_read_cost";
44+
INSERT INTO mysql.engine_cost
45+
VALUES ("Falcon", 0, "io_block_read_cost", 1.0, CURRENT_TIMESTAMP, "Unknown storage engine");
46+
INSERT INTO mysql.engine_cost
47+
VALUES ("InnoDB", -1, "io_block_read_cost", 1.0, CURRENT_TIMESTAMP, "1 is an illegal device type");
48+
"Restarting MySQL server"
49+
# restart
50+
"MySQL restarted"
51+
DELETE FROM mysql.engine_cost
52+
WHERE cost_name LIKE "lunch_cost%";
53+
UPDATE mysql.engine_cost
54+
SET cost_value=NULL;
55+
DELETE FROM mysql.engine_cost
56+
WHERE device_type = -1;
57+
DELETE FROM mysql.engine_cost
58+
WHERE engine_name LIKE "Falcon";
59+
SELECT * FROM mysql.server_cost;
60+
cost_name cost_value last_update comment
61+
disk_temptable_create_cost NULL # NULL
62+
disk_temptable_row_cost NULL # NULL
63+
key_compare_cost NULL # NULL
64+
memory_temptable_create_cost NULL # NULL
65+
memory_temptable_row_cost NULL # NULL
66+
row_evaluate_cost NULL # NULL
67+
SELECT * FROM mysql.engine_cost;
68+
engine_name device_type cost_name cost_value last_update comment
69+
default 0 io_block_read_cost NULL # NULL

mysql-test/suite/innodb/t/innodb_bug-13628249.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
call mtr.add_suppression('InnoDB: Failed to find tablespace for table "mysql"."slave_master_info" in the cache');
1515
call mtr.add_suppression('InnoDB: Failed to find tablespace for table "mysql"."slave_relay_log_info" in the cache');
1616
call mtr.add_suppression('InnoDB: Failed to find tablespace for table "mysql"."slave_worker_info" in the cache');
17+
call mtr.add_suppression('InnoDB: Failed to find tablespace for table "mysql"."server_cost" in the cache');
18+
call mtr.add_suppression('InnoDB: Failed to find tablespace for table "mysql"."engine_cost" in the cache');
1719
call mtr.add_suppression('InnoDB: Failed to find tablespace for table "mysql"."time_zone" in the cache');
1820
call mtr.add_suppression('InnoDB: Failed to find tablespace for table "mysql"."time_zone_leap_second" in the cache');
1921
call mtr.add_suppression('InnoDB: Failed to find tablespace for table "mysql"."time_zone_name" in the cache');

0 commit comments

Comments
 (0)