@@ -21,6 +21,7 @@ import (
21
21
"os"
22
22
"runtime/pprof"
23
23
"slices"
24
+ "strconv"
24
25
"strings"
25
26
"testing"
26
27
"time"
@@ -1286,6 +1287,45 @@ func TestOrderingIdxSelectivityRatio(t *testing.T) {
1286
1287
}
1287
1288
}
1288
1289
1290
+ func TestOrderingIdxSelectivityRatioForJoin (t * testing.T ) {
1291
+ store , dom := testkit .CreateMockStoreAndDomain (t )
1292
+ testKit := testkit .NewTestKit (t , store )
1293
+
1294
+ testKit .MustExec ("use test" )
1295
+ testKit .MustExec ("drop table if exists t" )
1296
+ testKit .MustExec ("create table t(a int, b int, c int, index ibc(b, c))" )
1297
+ testKit .MustExec ("insert into t values (1,1,1), (2,2,2), (3,3,3), (4,4,4), (5,5,5), (6,6,6), (7,7,7), (8,8,8), (9,9,9), (10,10,10)" )
1298
+ testKit .MustExec ("insert into t select a,b,c from t" )
1299
+ testKit .MustExec ("analyze table t" )
1300
+ h := dom .StatsHandle ()
1301
+ require .Nil (t , h .DumpStatsDeltaToKV (true ))
1302
+
1303
+ // Discourage merge join and hash join to encourage index join, and discourage topn to encourage limit.
1304
+ testKit .MustExec ("set @@session.tidb_opt_merge_join_cost_factor = 1000" )
1305
+ testKit .MustExec ("set @@session.tidb_opt_hash_join_cost_factor = 1000" )
1306
+ testKit .MustExec ("set @@session.tidb_opt_topn_cost_factor = 1000" )
1307
+ // Disable idx_selectivity_ratio using -1 and 0 - both should have no effect.
1308
+ testKit .MustExec ("set @@session.tidb_opt_ordering_index_selectivity_ratio = -1" )
1309
+ rs := testKit .MustQuery ("explain format=verbose select t1.* from t t1 use index (ibc) join t t2 on t1.b=t2.b where t2.c=5 order by t1.b limit 2" ).Rows ()
1310
+ planCost1 , err1 := strconv .ParseFloat (rs [0 ][2 ].(string ), 64 )
1311
+ require .Nil (t , err1 )
1312
+ testKit .MustExec ("set @@session.tidb_opt_ordering_index_selectivity_ratio = 0" )
1313
+ rs = testKit .MustQuery ("explain format=verbose select t1.* from t t1 use index (ibc) join t t2 on t1.b=t2.b where t2.c=5 order by t1.b limit 2" ).Rows ()
1314
+ planCost2 , err2 := strconv .ParseFloat (rs [0 ][2 ].(string ), 64 )
1315
+ require .Nil (t , err2 )
1316
+ require .Equal (t , planCost1 , planCost2 )
1317
+ // Increasing the ratio should increase the cost of index join, so the plan cost should increase.
1318
+ testKit .MustExec ("set @@session.tidb_opt_ordering_index_selectivity_ratio = 0.5" )
1319
+ rs = testKit .MustQuery ("explain format=verbose select t1.* from t t1 use index (ibc) join t t2 on t1.b=t2.b where t2.c=5 order by t1.b limit 2" ).Rows ()
1320
+ planCost3 , err3 := strconv .ParseFloat (rs [0 ][2 ].(string ), 64 )
1321
+ require .Nil (t , err3 )
1322
+ require .Less (t , planCost2 , planCost3 )
1323
+ testKit .MustExec ("set @@session.tidb_opt_ordering_index_selectivity_ratio = 1" )
1324
+ rs = testKit .MustQuery ("explain format=verbose select t1.* from t t1 use index (ibc) join t t2 on t1.b=t2.b where t2.c=5 order by t1.b limit 2" ).Rows ()
1325
+ planCost4 , err4 := strconv .ParseFloat (rs [0 ][2 ].(string ), 64 )
1326
+ require .Nil (t , err4 )
1327
+ require .Less (t , planCost3 , planCost4 )
1328
+ }
1289
1329
func TestCrossValidationSelectivity (t * testing.T ) {
1290
1330
store , dom := testkit .CreateMockStoreAndDomain (t )
1291
1331
tk := testkit .NewTestKit (t , store )
0 commit comments