Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: support more pattern for index join inner side #40999

Merged
merged 15 commits into from
Feb 7, 2023
132 changes: 132 additions & 0 deletions executor/index_advise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,135 @@ func TestIndexAdvise(t *testing.T) {
require.Equal(t, uint64(4), ia.MaxIndexNum.PerTable)
require.Equal(t, uint64(5), ia.MaxIndexNum.PerDB)
}

func TestIndexJoinProjPattern(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table t1(
pnbrn_cnaps varchar(5) not null,
new_accno varchar(18) not null,
primary key(pnbrn_cnaps,new_accno) nonclustered
);`)
tk.MustExec(`create table t2(
pnbrn_cnaps varchar(5) not null,
txn_accno varchar(18) not null,
txn_dt date not null,
yn_frz varchar(1) default null
);`)
tk.MustExec(`insert into t1(pnbrn_cnaps,new_accno) values ("40001","123")`)
tk.MustExec(`insert into t2(pnbrn_cnaps, txn_accno, txn_dt, yn_frz) values ("40001","123","20221201","0");`)

sql := `update
/*+ inl_join(a) */
t2 b,
(
select t1.pnbrn_cnaps,
t1.new_accno
from t1
where t1.pnbrn_cnaps = '40001'
) a
set b.yn_frz = '1'
where b.txn_dt = str_to_date('20221201', '%Y%m%d')
and b.pnbrn_cnaps = a.pnbrn_cnaps
and b.txn_accno = a.new_accno;`
rows := [][]interface{}{
{"Update_8"},
{"└─IndexJoin_14"},
{" ├─TableReader_25(Build)"},
{" │ └─Selection_24"},
{" │ └─TableFullScan_23"},
{" └─IndexReader_12(Probe)"},
{" └─Selection_11"},
{" └─IndexRangeScan_10"},
}
tk.Session().GetSessionVars().EnableIndexJoinInnerSideMultiPattern = true
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
rows = [][]interface{}{
{"Update_8"},
{"└─HashJoin_10"},
{" ├─IndexReader_17(Build)"},
{" │ └─IndexRangeScan_16"},
{" └─TableReader_14(Probe)"},
{" └─Selection_13"},
{" └─TableFullScan_12"},
}
tk.Session().GetSessionVars().EnableIndexJoinInnerSideMultiPattern = false
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)

tk.Session().GetSessionVars().EnableIndexJoinInnerSideMultiPattern = true
tk.MustExec(sql)
tk.MustQuery("select yn_frz from t2").Check(testkit.Rows("1"))
}

func TestIndexJoinSelPattern(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(` create table tbl_miss(
id bigint(20) unsigned not null
,txn_dt date default null
,perip_sys_uuid varchar(32) not null
,rvrs_idr varchar(1) not null
,primary key(id) clustered
,key idx1 (txn_dt, perip_sys_uuid, rvrs_idr)
);
`)
tk.MustExec(`insert into tbl_miss (id,txn_dt,perip_sys_uuid,rvrs_idr) values (1,"20221201","123","1");`)
tk.MustExec(`create table tbl_src(
txn_dt date default null
,uuid varchar(32) not null
,rvrs_idr char(1)
,expd_inf varchar(5000)
,primary key(uuid,rvrs_idr) nonclustered
);
`)
tk.MustExec(`insert into tbl_src (txn_dt,uuid,rvrs_idr) values ("20221201","123","1");`)
sql := `select /*+ use_index(mis,) inl_join(src) */
*
from tbl_miss mis
,tbl_src src
where src.txn_dt >= str_to_date('20221201', '%Y%m%d')
and mis.id between 1 and 10000
and mis.perip_sys_uuid = src.uuid
and mis.rvrs_idr = src.rvrs_idr
and mis.txn_dt = src.txn_dt
and (
case when isnull(src.expd_inf) = 1 then ''
else
substr(concat_ws('',src.expd_inf,'~~'),
instr(concat_ws('',src.expd_inf,'~~'),'~~a4') + 4,
instr(substr(concat_ws('',src.expd_inf,'~~'),
instr(concat_ws('',src.expd_inf,'~~'),'~~a4') + 4, length(concat_ws('',src.expd_inf,'~~'))),'~~') -1)
end
) != '01';`
rows := [][]interface{}{
{"HashJoin_9"},
{"├─TableReader_12(Build)"},
{"│ └─Selection_11"},
{"│ └─TableRangeScan_10"},
{"└─Selection_13(Probe)"},
{" └─TableReader_16"},
{" └─Selection_15"},
{" └─TableFullScan_14"},
}
tk.Session().GetSessionVars().EnableIndexJoinInnerSideMultiPattern = false
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
rows = [][]interface{}{
{"IndexJoin_13"},
{"├─TableReader_25(Build)"},
{"│ └─Selection_24"},
{"│ └─TableRangeScan_23"},
{"└─Selection_12(Probe)"},
{" └─IndexLookUp_11"},
{" ├─IndexRangeScan_8(Build)"},
{" └─Selection_10(Probe)"},
{" └─TableRowIDScan_9"},
}
tk.Session().GetSessionVars().EnableIndexJoinInnerSideMultiPattern = true
tk.MustQuery("explain "+sql).CheckAt([]int{0}, rows)
tk.Session().GetSessionVars().EnableIndexJoinInnerSideMultiPattern = true
tk.MustQuery(sql).Check(testkit.Rows("1 2022-12-01 123 1 2022-12-01 123 1 <nil>"))
tk.Session().GetSessionVars().EnableIndexJoinInnerSideMultiPattern = false
tk.MustQuery(sql).Check(testkit.Rows("1 2022-12-01 123 1 2022-12-01 123 1 <nil>"))
}
Loading