Skip to content

Commit

Permalink
feat(shannon): cost model
Browse files Browse the repository at this point in the history
1: cost model
2: refine # of records in rapid.
  • Loading branch information
ShannonBase committed Dec 8, 2024
1 parent ea868f8 commit 1d50026
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 103 deletions.
32 changes: 16 additions & 16 deletions mysql-test/suite/secondary_engine/r/explain.result
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Table Op Msg_type Msg_text
test.t1 analyze status OK
EXPLAIN SELECT * FROM t1;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 1 100.00 Using secondary engine Rapid
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 0 0.00 Using secondary engine Rapid
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1`
EXPLAIN FORMAT=JSON SELECT * FROM t1, t1 t2 WHERE t1.a = t2.a;
Expand All @@ -61,22 +61,22 @@ EXPLAIN
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1.20"
"query_cost": "-0.00"
},
"nested_loop": [
{
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows_examined_per_scan": 1,
"rows_produced_per_join": 1,
"filtered": "100.00",
"rows_examined_per_scan": 0,
"rows_produced_per_join": 0,
"filtered": "0.00",
"using_secondary_engine": "Rapid",
"cost_info": {
"read_cost": "0.50",
"eval_cost": "0.10",
"prefix_cost": "0.60",
"data_read_per_join": "10"
"read_cost": "0.00",
"eval_cost": "0.00",
"prefix_cost": "0.00",
"data_read_per_join": "0"
},
"used_columns": [
"a"
Expand All @@ -87,16 +87,16 @@ EXPLAIN
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows_examined_per_scan": 1,
"rows_produced_per_join": 1,
"filtered": "100.00",
"rows_examined_per_scan": 0,
"rows_produced_per_join": 0,
"filtered": "0.00",
"using_join_buffer": "hash join",
"using_secondary_engine": "Rapid",
"cost_info": {
"read_cost": "0.50",
"eval_cost": "0.10",
"prefix_cost": "1.20",
"data_read_per_join": "10"
"read_cost": "0.00",
"eval_cost": "0.00",
"prefix_cost": "0.00",
"data_read_per_join": "0"
},
"used_columns": [
"a"
Expand Down
18 changes: 9 additions & 9 deletions mysql-test/suite/secondary_engine/r/explain_hypergraph.result
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,32 @@ EXPLAIN
"operation": "Table scan on t1 in secondary engine Rapid",
"table_name": "t1",
"access_type": "table",
"estimated_rows": 1.0,
"estimated_rows": 0.0,
"secondary_engine": "Rapid",
"estimated_total_cost": 0.5,
"estimated_first_row_cost": 0.5
"estimated_total_cost": 0.0,
"estimated_first_row_cost": 0.0
},
{
"heading": "Hash",
"operation": "Table scan on t2 in secondary engine Rapid",
"table_name": "t2",
"access_type": "table",
"estimated_rows": 1.0,
"estimated_rows": 0.0,
"secondary_engine": "Rapid",
"estimated_total_cost": 0.5,
"estimated_first_row_cost": 0.5
"estimated_total_cost": 0.0,
"estimated_first_row_cost": 0.0
}
],
"join_type": "inner join",
"operation": "Inner hash join (t1.a = t2.a)",
"access_type": "join",
"estimated_rows": 0.10000000149011612,
"estimated_rows": 0.0,
"hash_condition": [
"(t1.a = t2.a)"
],
"join_algorithm": "hash",
"estimated_total_cost": 1.2070000001043084,
"estimated_first_row_cost": 1.2070000001043084
"estimated_total_cost": 0.0,
"estimated_first_row_cost": 0.0
}
Warnings:
Note 1003 Query is executed in secondary engine; the actual query plan may diverge from the printed one
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/suite/secondary_engine/r/plan_cost_debug.result
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`x` AS `x`,`test`.`t1`.`y` AS `y`,`test`.`t2`.`x` AS `x`,`test`.`t2`.`y` AS `y`,`test`.`t3`.`x` AS `x`,`test`.`t3`.`y` AS `y` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`y` = `test`.`t2`.`x`)) where (`test`.`t1`.`x` = `test`.`t2`.`y`)
SELECT TRACE->'$**.secondary_engine_cost' FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
TRACE->'$**.secondary_engine_cost'
[2.5001]
[1.37503]
SET @@optimizer_trace='enabled=off';
SET DEBUG = '+d,secondary_engine_mock_change_join_order';
EXPLAIN SELECT * FROM t1 AS X JOIN t2 ON X.x = t2.y LEFT JOIN t3 ON t2.x = t3.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ FROM t1 AS alias1
WHERE (alias1.col_varchar_255_key IN (SELECT 'f' UNION ALL SELECT 'r'))
GROUP BY field1;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 PRIMARY alias1 NULL ALL NULL NULL NULL NULL 1 100.00 Using where; Using temporary; Using secondary engine Rapid
1 PRIMARY alias1 NULL ALL NULL NULL NULL NULL 0 0.00 Using where; Using temporary; Using secondary engine Rapid
3 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
4 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL NULL NULL No tables used
2 SUBQUERY t1 NULL ALL NULL NULL NULL NULL 1 100.00 Using secondary engine Rapid
2 SUBQUERY t1 NULL ALL NULL NULL NULL NULL 0 0.00 Using secondary engine Rapid
Warnings:
Note 1003 /* select#1 */ select (/* select#2 */ select max(`test`.`t1`.`col_varchar_255_key`) from `test`.`t1`) AS `field1` from `test`.`t1` `alias1` where <in_optimizer>(`test`.`alias1`.`col_varchar_255_key`,<exists>(/* select#3 */ select 'f' having (<cache>(`test`.`alias1`.`col_varchar_255_key`) = <ref_null_helper>('f')) union all /* select#4 */ select 'r' having (<cache>(`test`.`alias1`.`col_varchar_255_key`) = <ref_null_helper>('r')))) group by `field1`
SELECT (SELECT MAX(col_varchar_255_key ) FROM t1) AS field1
Expand Down
16 changes: 8 additions & 8 deletions mysql-test/suite/secondary_engine/r/shannon_case6.result
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ score name id gender
4 n4 4 f
explain select score, name, gender from tt6 order by score;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 3 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 7 100.00 Using filesort; Using secondary engine Rapid
select score, name, gender from tt6 order by score;
score name gender
1 n1 m
Expand Down Expand Up @@ -67,7 +67,7 @@ id
4
explain select sum(score), name, gender from tt6 group by name, gender;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 3 100.00 Using temporary; Using secondary engine Rapid
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 7 100.00 Using temporary; Using secondary engine Rapid
select sum(score), name, gender from tt6 group by name, gender;
sum(score) name gender
11 n1 m
Expand All @@ -76,7 +76,7 @@ sum(score) name gender
4 n4 f
explain select avg(score), gender from tt6 group by gender;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 3 100.00 Using temporary; Using secondary engine Rapid
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 7 100.00 Using temporary; Using secondary engine Rapid
select avg(score), gender from tt6 group by gender;
avg(score) gender
11.0000 m
Expand Down Expand Up @@ -113,7 +113,7 @@ score name gender
delete from tt6 where score=40;
explain select score, name, gender from tt6 order by score desc;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 8 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 9 100.00 Using filesort; Using secondary engine Rapid
select score, name, gender from tt6 order by score desc;
score name gender
50 n5 m
Expand All @@ -127,7 +127,7 @@ score name gender
delete from tt6 where id=3;
explain select score, name, gender from tt6 order by score desc;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 6 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 9 100.00 Using filesort; Using secondary engine Rapid
select score, name, gender from tt6 order by score desc;
score name gender
50 n5 m
Expand All @@ -139,7 +139,7 @@ score name gender
update tt6 set name ='n5_new2' where score=50;
explain select score, name, gender from tt6 order by score desc;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 6 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 9 100.00 Using filesort; Using secondary engine Rapid
select score, name, gender from tt6 order by score desc;
score name gender
50 n5_new2 m
Expand All @@ -151,7 +151,7 @@ score name gender
update tt6 set name ='n5_new22' where id=5;
explain select score, name, gender from tt6 order by score desc;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 6 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 9 100.00 Using filesort; Using secondary engine Rapid
select score, name, gender from tt6 order by score desc;
score name gender
50 n5_new2 m
Expand All @@ -163,7 +163,7 @@ score name gender
delete from tt6 where id=2;
explain select score, name, gender from tt6 order by score desc;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 4 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE tt6 NULL ALL NULL NULL NULL NULL 9 100.00 Using filesort; Using secondary engine Rapid
select score, name, gender from tt6 order by score desc;
score name gender
50 n5_new2 m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ select * from tt order by id;
score name id gender
explain select * from tt order by id;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt NULL ALL NULL NULL NULL NULL 1 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE tt NULL ALL NULL NULL NULL NULL 0 0.00 Using filesort; Using secondary engine Rapid
[conn2]
set use_secondary_engine=off;
show variables like '%use_secondary_engine%' ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ name gender
[conn1]
explain select * from tt2;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt2 NULL ALL NULL NULL NULL NULL 1 100.00 Using secondary engine Rapid
1 SIMPLE tt2 NULL ALL NULL NULL NULL NULL 3 100.00 Using secondary engine Rapid
select * from tt2;
name gender
[conn2]
Expand Down Expand Up @@ -218,7 +218,7 @@ set use_secondary_engine = forced;
alter table tt secondary_load;
explain select *, length(name) from tt;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE tt NULL ALL NULL NULL NULL NULL 3 100.00 Using secondary engine Rapid
1 SIMPLE tt NULL ALL NULL NULL NULL NULL 4 100.00 Using secondary engine Rapid
select *, length(name) from tt;
score name id gender length(name)
1 n1 1 m 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ select * from ti order by name;
id score name name2
explain select * from ti order by name;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE ti NULL ALL NULL NULL NULL NULL 1 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE ti NULL ALL NULL NULL NULL NULL 0 0.00 Using filesort; Using secondary engine Rapid
[conn2]
set use_secondary_engine=off;
show variables like '%use_secondary_engine%' ;
Expand All @@ -27,7 +27,7 @@ select * from ti order by name;
id score name name2
explain select * from ti order by name;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE ti NULL ALL NULL NULL NULL NULL 1 100.00 Using filesort; Using secondary engine Rapid
1 SIMPLE ti NULL ALL NULL NULL NULL NULL 0 0.00 Using filesort; Using secondary engine Rapid
[conn2]
insert into ti values(1, 1.12, '创建相应的情况下d', '创建相应的 lsn值d');
insert into ti values(2, 1.102, '创建相应的情况下s', '创建相应的 lsn值s');
Expand Down
38 changes: 19 additions & 19 deletions mysql-test/suite/secondary_engine/r/transforms.result
Original file line number Diff line number Diff line change
Expand Up @@ -363,21 +363,21 @@ ON t2.f1 = t1.f2 AND t1.f1 IN (SELECT f1 FROM t1 AS t3))
WHERE (SELECT SUM(f2) FROM t1 AS t4) IS NULL
AND t1.f1 = t2.f1 OR t2.f2 <= 0;
EXPLAIN
-> Filter: (((t1.f2 = t1.f1) and (derived_1_3.`SUM(f2)` is null)) or (t2.f2 <= 0)) (rows=1)
-> Left hash join (no condition) (rows=1)
-> Hash semijoin (t3.f1 = t1.f1) (rows=1)
-> Filter: ((t1.f2 = t1.f1) or (t2.f2 <= 0)) (rows=1)
-> Inner hash join (t2.f1 = t1.f2) (rows=1)
-> Table scan on t2 in secondary engine Rapid (rows=1)
-> Filter: (((t1.f2 = t1.f1) and (derived_1_3.`SUM(f2)` is null)) or (t2.f2 <= 0)) (rows=0)
-> Left hash join (no condition) (rows=0)
-> Hash semijoin (t3.f1 = t1.f1) (rows=0)
-> Filter: ((t1.f2 = t1.f1) or (t2.f2 <= 0)) (rows=0)
-> Inner hash join (t2.f1 = t1.f2) (rows=0)
-> Table scan on t2 in secondary engine Rapid (rows=0)
-> Hash
-> Table scan on t1 in secondary engine Rapid (rows=1)
-> Table scan on t1 in secondary engine Rapid (rows=0)
-> Hash
-> Table scan on t3 in secondary engine Rapid (rows=1)
-> Table scan on t3 in secondary engine Rapid (rows=0)
-> Hash
-> Table scan on derived_1_3 (rows=1)
-> Materialize (rows=1)
-> Aggregate: sum(t4.f2) (rows=1)
-> Table scan on t4 in secondary engine Rapid (rows=1)
-> Table scan on t4 in secondary engine Rapid (rows=0)

Warnings:
Note 1003 Query is executed in secondary engine; the actual query plan may diverge from the printed one
Expand All @@ -396,18 +396,18 @@ EXPLAIN FORMAT=tree SELECT 1
FROM t1 JOIN t1 AS t2 ON (t2.f1 = t1.f1)
LEFT JOIN t1 AS t3 ON (t2.f2 = t1.f2 AND t2.f2 IN (SELECT f1 FROM t1 AS t4));
EXPLAIN
-> Nested loop left join (rows=1)
-> Inner hash join (t2.f1 = t1.f1) (rows=1)
-> Table scan on t2 in secondary engine Rapid (rows=1)
-> Nested loop left join (rows=0)
-> Inner hash join (t2.f1 = t1.f1) (rows=0)
-> Table scan on t2 in secondary engine Rapid (rows=0)
-> Hash
-> Table scan on t1 in secondary engine Rapid (rows=1)
-> Filter: (t2.f2 = t1.f2) (rows=1)
-> Hash semijoin (no condition) (rows=1)
-> Table scan on t3 in secondary engine Rapid (rows=1)
-> Table scan on t1 in secondary engine Rapid (rows=0)
-> Filter: (t2.f2 = t1.f2) (rows=0)
-> Hash semijoin (no condition) (rows=0)
-> Table scan on t3 in secondary engine Rapid (rows=0)
-> Hash
-> Limit: 1 row(s) (rows=1)
-> Filter: (t4.f1 = t1.f2) (rows=1)
-> Table scan on t4 in secondary engine Rapid (rows=1)
-> Limit: 1 row(s) (rows=0)
-> Filter: (t4.f1 = t1.f2) (rows=0)
-> Table scan on t4 in secondary engine Rapid (rows=0)

Warnings:
Note 1003 Query is executed in secondary engine; the actual query plan may diverge from the printed one
Expand Down
16 changes: 8 additions & 8 deletions mysql-test/suite/secondary_engine/r/transforms_hypergraph.result
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,20 @@ ON t2.f1 = t1.f2 AND t1.f1 IN (SELECT f1 FROM t1 AS t3))
WHERE (SELECT SUM(f2) FROM t1 AS t4) IS NULL
AND t1.f1 = t2.f1 OR t2.f2 <= 0;
EXPLAIN
-> Filter: (((derived_1_3.`SUM(f2)` is null) and ((t1.f1 = t1.f2) and (t1.f1 = t2.f1))) or (t2.f2 <= 0)) (rows=0.01)
-> Left hash join (no condition) (rows=0.01)
-> Hash semijoin (t1.f1 = t3.f1), extra conditions: (((t1.f1 = t1.f2) and ((t1.f1 = t3.f1) and (t3.f1 = t2.f1))) or (t2.f2 <= 0)) (rows=0.01)
-> Inner hash join (t2.f1 = t1.f2), extra conditions: ((t1.f1 = t1.f2) or (t2.f2 <= 0)) (rows=0.1)
-> Table scan on t1 in secondary engine Rapid (rows=1)
-> Filter: (((derived_1_3.`SUM(f2)` is null) and ((t1.f1 = t1.f2) and (t1.f1 = t2.f1))) or (t2.f2 <= 0)) (rows=0)
-> Left hash join (no condition) (rows=0)
-> Hash semijoin (t1.f1 = t3.f1), extra conditions: (((t1.f1 = t1.f2) and ((t1.f1 = t3.f1) and (t3.f1 = t2.f1))) or (t2.f2 <= 0)) (rows=0)
-> Inner hash join (t2.f1 = t1.f2), extra conditions: ((t1.f1 = t1.f2) or (t2.f2 <= 0)) (rows=0)
-> Table scan on t1 in secondary engine Rapid (rows=0)
-> Hash
-> Table scan on t2 in secondary engine Rapid (rows=1)
-> Table scan on t2 in secondary engine Rapid (rows=0)
-> Hash
-> Table scan on t3 in secondary engine Rapid (rows=1)
-> Table scan on t3 in secondary engine Rapid (rows=0)
-> Hash
-> Table scan on derived_1_3 (rows=1)
-> Materialize (rows=1)
-> Aggregate: sum(t4.f2) (rows=1)
-> Table scan on t4 in secondary engine Rapid (rows=1)
-> Table scan on t4 in secondary engine Rapid (rows=0)

Warnings:
Note 1003 Query is executed in secondary engine; the actual query plan may diverge from the printed one
Expand Down
2 changes: 1 addition & 1 deletion sql/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@ class Field_longlong : public Field_num {
const CHARSET_INFO *charset) final;
type_conversion_status store(double nr) final;
type_conversion_status store(longlong nr, bool unsigned_val) override;
double val_real() const override;
double val_real() const final;
longlong val_int() const override;
String *val_str(String *, String *) const final;
bool send_to_protocol(Protocol *protocol) const final;
Expand Down
12 changes: 11 additions & 1 deletion storage/rapid_engine/cost/cost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@
*/
#include "storage/rapid_engine/cost/cost.h"

#include <assert.h>
#include <sys/types.h>

#include "lex_string.h"
#include "m_string.h"

class THD;
struct TABLE;

namespace ShannonBase {
namespace Optimizer {

CostEstimator::CostEstimator() {}
const double Rapid_SE_cost_constants::MEMORY_BLOCK_READ_COST = 0.25;
const double Rapid_SE_cost_constants::IO_BLOCK_READ_COST = 1.0;

} // namespace Optimizer
} // namespace ShannonBase
Loading

0 comments on commit 1d50026

Please sign in to comment.