Skip to content

Commit bd43f39

Browse files
committed
MDEV-24325: Optimizer trace doesn't cover LATERAL DERIVED
Provide basic coverage in the Optimizer Trace
1 parent e1a514d commit bd43f39

File tree

4 files changed

+183
-1
lines changed

4 files changed

+183
-1
lines changed

mysql-test/main/opt_trace.result

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9070,4 +9070,120 @@ json_detailed(json_extract(trace, '$**.condition_processing'))
90709070
}
90719071
]
90729072
drop table t1,t2,t3;
9073+
#
9074+
# MDEV-24325: Optimizer trace doesn't cover LATERAL DERIVED
9075+
#
9076+
create table t1 (a int, b int, index idx_b(b)) engine=myisam;
9077+
insert into t1 values
9078+
(8,3), (5,7), (1,2), (2,1), (9,7), (7,5), (2,2), (7,3),
9079+
(9,3), (8,1), (4,5), (2,3);
9080+
create table t2 (a int, b int, c char(127), index idx_a(a)) engine=myisam;
9081+
insert into t2 values
9082+
(7,10,'x'), (1,20,'a'), (2,23,'b'), (7,18,'z'), (1,30,'c'),
9083+
(4,71,'d'), (3,15,'x'), (7,82,'y'), (8,12,'t'), (4,15,'b'),
9084+
(11,33,'a'), (10,42,'u'), (4,53,'p'), (10,17,'r'), (2,90,'x'),
9085+
(17,10,'s'), (11,20,'v'), (12,23,'y'), (17,18,'a'), (11,30,'d'),
9086+
(24,71,'h'), (23,15,'i'), (27,82,'k'), (28,12,'p'), (24,15,'q'),
9087+
(31,33,'f'), (30,42,'h'), (40,53,'m'), (30,17,'o'), (21,90,'b'),
9088+
(37,10,'e'), (31,20,'g'), (32,23,'f'), (37,18,'n'), (41,30,'l'),
9089+
(54,71,'j'), (53,15,'w'), (57,82,'z'), (58,12,'k'), (54,15,'p'),
9090+
(61,33,'c'), (60,42,'a'), (62,53,'x'), (67,17,'g'), (64,90,'v');
9091+
insert into t2 select a+10, b+10, concat(c,'f') from t2;
9092+
analyze table t1,t2;
9093+
Table Op Msg_type Msg_text
9094+
test.t1 analyze status Engine-independent statistics collected
9095+
test.t1 analyze status OK
9096+
test.t2 analyze status Engine-independent statistics collected
9097+
test.t2 analyze status OK
9098+
explain
9099+
select t1.a,t.s,t.m
9100+
from t1 join
9101+
(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
9102+
on t1.a=t.a
9103+
where t1.b < 3;
9104+
id select_type table type possible_keys key key_len ref rows Extra
9105+
1 PRIMARY t1 range idx_b idx_b 5 NULL 4 Using index condition; Using where
9106+
1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
9107+
2 LATERAL DERIVED t2 ref idx_a idx_a 5 test.t1.a 1
9108+
select
9109+
json_detailed(json_extract(trace, '$**.choose_best_splitting'))
9110+
from
9111+
information_schema.optimizer_trace;
9112+
json_detailed(json_extract(trace, '$**.choose_best_splitting'))
9113+
[
9114+
9115+
[
9116+
9117+
{
9118+
"considered_execution_plans":
9119+
[
9120+
9121+
{
9122+
"plan_prefix":
9123+
[
9124+
],
9125+
"table": "t2",
9126+
"best_access_path":
9127+
{
9128+
"considered_access_paths":
9129+
[
9130+
9131+
{
9132+
"access_type": "ref",
9133+
"index": "idx_a",
9134+
"used_range_estimates": false,
9135+
"cause": "not available",
9136+
"rows": 1.8367,
9137+
"cost": 2.000585794,
9138+
"chosen": true
9139+
},
9140+
9141+
{
9142+
"type": "scan",
9143+
"chosen": false,
9144+
"cause": "cost"
9145+
}
9146+
],
9147+
"chosen_access_method":
9148+
{
9149+
"type": "ref",
9150+
"records": 1.8367,
9151+
"cost": 2.000585794,
9152+
"uses_join_buffering": false
9153+
}
9154+
},
9155+
"rows_for_plan": 1.8367,
9156+
"cost_for_plan": 2.367925794,
9157+
"cost_for_sorting": 1.8367,
9158+
"estimated_join_cardinality": 1.8367
9159+
}
9160+
]
9161+
},
9162+
9163+
{
9164+
"best_splitting":
9165+
{
9166+
"table": "t2",
9167+
"key": "idx_a",
9168+
"record_count": 4,
9169+
"cost": 2.488945919,
9170+
"unsplit_cost": 25.72361682
9171+
}
9172+
}
9173+
]
9174+
]
9175+
select
9176+
json_detailed(json_extract(trace, '$**.lateral_derived'))
9177+
from
9178+
information_schema.optimizer_trace;
9179+
json_detailed(json_extract(trace, '$**.lateral_derived'))
9180+
[
9181+
9182+
{
9183+
"startup_cost": 9.955783677,
9184+
"splitting_cost": 2.488945919,
9185+
"records": 1
9186+
}
9187+
]
9188+
drop table t1,t2;
90739189
set optimizer_trace='enabled=off';

mysql-test/main/opt_trace.test

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,4 +788,53 @@ from
788788

789789
drop table t1,t2,t3;
790790

791+
792+
--echo #
793+
--echo # MDEV-24325: Optimizer trace doesn't cover LATERAL DERIVED
794+
--echo #
795+
create table t1 (a int, b int, index idx_b(b)) engine=myisam;
796+
insert into t1 values
797+
(8,3), (5,7), (1,2), (2,1), (9,7), (7,5), (2,2), (7,3),
798+
(9,3), (8,1), (4,5), (2,3);
799+
800+
create table t2 (a int, b int, c char(127), index idx_a(a)) engine=myisam;
801+
insert into t2 values
802+
(7,10,'x'), (1,20,'a'), (2,23,'b'), (7,18,'z'), (1,30,'c'),
803+
(4,71,'d'), (3,15,'x'), (7,82,'y'), (8,12,'t'), (4,15,'b'),
804+
(11,33,'a'), (10,42,'u'), (4,53,'p'), (10,17,'r'), (2,90,'x'),
805+
(17,10,'s'), (11,20,'v'), (12,23,'y'), (17,18,'a'), (11,30,'d'),
806+
(24,71,'h'), (23,15,'i'), (27,82,'k'), (28,12,'p'), (24,15,'q'),
807+
(31,33,'f'), (30,42,'h'), (40,53,'m'), (30,17,'o'), (21,90,'b'),
808+
(37,10,'e'), (31,20,'g'), (32,23,'f'), (37,18,'n'), (41,30,'l'),
809+
(54,71,'j'), (53,15,'w'), (57,82,'z'), (58,12,'k'), (54,15,'p'),
810+
(61,33,'c'), (60,42,'a'), (62,53,'x'), (67,17,'g'), (64,90,'v');
811+
812+
insert into t2 select a+10, b+10, concat(c,'f') from t2;
813+
814+
analyze table t1,t2;
815+
816+
explain
817+
select t1.a,t.s,t.m
818+
from t1 join
819+
(select a, sum(t2.b) as s, min(t2.c) as m from t2 group by t2.a) t
820+
on t1.a=t.a
821+
where t1.b < 3;
822+
823+
#
824+
# Just show that choose_best_splitting function has coverage in the
825+
# optimizer trace and re-optmization of child select inside it is distinct
826+
# from the rest of join optimization.
827+
select
828+
json_detailed(json_extract(trace, '$**.choose_best_splitting'))
829+
from
830+
information_schema.optimizer_trace;
831+
832+
# Same as above. just to show that splitting plan has some coverage in the
833+
# trace.
834+
select
835+
json_detailed(json_extract(trace, '$**.lateral_derived'))
836+
from
837+
information_schema.optimizer_trace;
838+
839+
drop table t1,t2;
791840
set optimizer_trace='enabled=off';

sql/opt_split.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187

188188
#include "mariadb.h"
189189
#include "sql_select.h"
190+
#include "opt_trace.h"
190191

191192
/* Info on a splitting field */
192193
struct SplM_field_info
@@ -957,6 +958,7 @@ SplM_plan_info * JOIN_TAB::choose_best_splitting(double record_count,
957958
The key for splitting was chosen, look for the plan for this key
958959
in the cache
959960
*/
961+
Json_writer_array spl_trace(thd, "choose_best_splitting");
960962
spl_plan= spl_opt_info->find_plan(best_table, best_key, best_key_parts);
961963
if (!spl_plan &&
962964
(spl_plan= (SplM_plan_info *) thd->alloc(sizeof(SplM_plan_info))) &&
@@ -988,6 +990,16 @@ SplM_plan_info * JOIN_TAB::choose_best_splitting(double record_count,
988990
spl_plan->cost= join->best_positions[join->table_count-1].read_time +
989991
+ oper_cost;
990992

993+
if (unlikely(thd->trace_started()))
994+
{
995+
Json_writer_object wrapper(thd);
996+
Json_writer_object find_trace(thd, "best_splitting");
997+
find_trace.add("table", best_table->alias.c_ptr());
998+
find_trace.add("key", best_table->key_info[best_key].name);
999+
find_trace.add("record_count", record_count);
1000+
find_trace.add("cost", spl_plan->cost);
1001+
find_trace.add("unsplit_cost", spl_opt_info->unsplit_cost);
1002+
}
9911003
memcpy((char *) spl_plan->best_positions,
9921004
(char *) join->best_positions,
9931005
sizeof(POSITION) * join->table_count);
@@ -1014,6 +1026,11 @@ SplM_plan_info * JOIN_TAB::choose_best_splitting(double record_count,
10141026
{
10151027
startup_cost= record_count * spl_plan->cost;
10161028
records= (ha_rows) (records * spl_plan->split_sel);
1029+
1030+
Json_writer_object trace(thd, "lateral_derived");
1031+
trace.add("startup_cost", startup_cost);
1032+
trace.add("splitting_cost", spl_plan->cost);
1033+
trace.add("records", records);
10171034
}
10181035
else
10191036
startup_cost= spl_opt_info->unsplit_cost;

sql/sql_select.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7441,14 +7441,14 @@ best_access_path(JOIN *join,
74417441
DBUG_ENTER("best_access_path");
74427442

74437443
Json_writer_object trace_wrapper(thd, "best_access_path");
7444-
Json_writer_array trace_paths(thd, "considered_access_paths");
74457444

74467445
bitmap_clear_all(eq_join_set);
74477446

74487447
loose_scan_opt.init(join, s, remaining_tables);
74497448

74507449
if (s->table->is_splittable())
74517450
spl_plan= s->choose_best_splitting(record_count, remaining_tables);
7451+
Json_writer_array trace_paths(thd, "considered_access_paths");
74527452

74537453
if (s->keyuse)
74547454
{ /* Use key if possible */

0 commit comments

Comments
 (0)