diff --git a/pkg/executor/test/tiflashtest/BUILD.bazel b/pkg/executor/test/tiflashtest/BUILD.bazel index 7713977d4ca95..cbba6bb46361a 100644 --- a/pkg/executor/test/tiflashtest/BUILD.bazel +++ b/pkg/executor/test/tiflashtest/BUILD.bazel @@ -9,7 +9,7 @@ go_test( ], flaky = True, race = "on", - shard_count = 44, + shard_count = 45, deps = [ "//pkg/config", "//pkg/domain", diff --git a/pkg/executor/test/tiflashtest/tiflash_test.go b/pkg/executor/test/tiflashtest/tiflash_test.go index e19bcea99a403..e210f120e6a70 100644 --- a/pkg/executor/test/tiflashtest/tiflash_test.go +++ b/pkg/executor/test/tiflashtest/tiflash_test.go @@ -2117,3 +2117,35 @@ func TestMppTableReaderCacheForSingleSQL(t *testing.T) { require.Equal(t, tc.expectMissNum, missNum.Load()) } } + +func TestIndexMergeCarePreferTiflash(t *testing.T) { + store := testkit.CreateMockStore(t, withMockTiFlash(1)) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + tk.MustExec("drop table if exists t") + tk.MustExec("CREATE TABLE `t` (" + + "`i` bigint(20) NOT NULL, " + + "`w` varchar(32) NOT NULL," + + "`l` varchar(32) NOT NULL," + + "`a` tinyint(4) NOT NULL DEFAULT '0'," + + "`m` int(11) NOT NULL DEFAULT '0'," + + "`s` int(11) NOT NULL DEFAULT '0'," + + "PRIMARY KEY (`i`) /*T![clustered_index] NONCLUSTERED */," + + "KEY `idx_win_user_site_code` (`w`,`m`)," + + "KEY `idx_lose_user_site_code` (`l`,`m`)," + + "KEY `idx_win_site_code_status` (`w`,`a`)," + + "KEY `idx_lose_site_code_status` (`l`,`a`)" + + ")") + tk.MustExec("alter table t set tiflash replica 1") + tb := external.GetTableByName(t, tk, "test", "t") + err := domain.GetDomain(tk.Session()).DDLExecutor().UpdateTableReplicaInfo(tk.Session(), tb.Meta().ID, true) + require.NoError(t, err) + tk.MustQuery("explain format=\"brief\" SELECT" + + " /*+ read_from_storage(tiflash[a]) */ a.i FROM t a WHERE a.s = 0 AND a.a NOT IN (-1, 0) AND m >= 1726910326 AND m <= 1726910391 AND ( a.w IN ('1123') OR a.l IN ('1123'))").Check( + testkit.Rows("TableReader 0.00 root MppVersion: 2, data:ExchangeSender", + "└─ExchangeSender 0.00 mpp[tiflash] ExchangeType: PassThrough", + " └─Projection 0.00 mpp[tiflash] test.t.i", + " └─Selection 0.00 mpp[tiflash] ge(test.t.m, 1726910326), le(test.t.m, 1726910391), not(in(test.t.a, -1, 0)), or(eq(test.t.w, \"1123\"), eq(test.t.l, \"1123\"))", + " └─TableFullScan 10.00 mpp[tiflash] table:a pushed down filter:eq(test.t.s, 0), keep order:false, stats:pseudo")) +} diff --git a/pkg/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json b/pkg/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json index b440550618083..15ed32b7b1fa3 100644 --- a/pkg/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json +++ b/pkg/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json @@ -406,11 +406,11 @@ "└─ExchangeSender 4439.11 mpp[tiflash] ExchangeType: PassThrough", " └─Projection 4439.11 mpp[tiflash] test.t.a, Column#5", " └─Projection 4439.11 mpp[tiflash] Column#5, test.t.a", - " └─HashAgg 4439.11 mpp[tiflash] group by:test.t.a, test.t.c, funcs:sum(Column#14)->Column#5, funcs:firstrow(test.t.a)->test.t.a", + " └─HashAgg 4439.11 mpp[tiflash] group by:test.t.a, test.t.c, funcs:sum(Column#8)->Column#5, funcs:firstrow(test.t.a)->test.t.a", " └─ExchangeReceiver 4439.11 mpp[tiflash] ", " └─ExchangeSender 4439.11 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.a, collate: binary], [name: test.t.c, collate: binary]", - " └─HashAgg 4439.11 mpp[tiflash] group by:Column#17, Column#18, funcs:sum(Column#16)->Column#14", - " └─Projection 5548.89 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#16, test.t.a->Column#17, test.t.c->Column#18", + " └─HashAgg 4439.11 mpp[tiflash] group by:Column#11, Column#12, funcs:sum(Column#10)->Column#8", + " └─Projection 5548.89 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#10, test.t.a->Column#11, test.t.c->Column#12", " └─Selection 5548.89 mpp[tiflash] or(lt(test.t.b, 2), gt(test.t.a, 2))", " └─TableFullScan 10000.00 mpp[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo" ], diff --git a/pkg/planner/core/find_best_task.go b/pkg/planner/core/find_best_task.go index 09a6077fa3f57..78acf3208b8ba 100644 --- a/pkg/planner/core/find_best_task.go +++ b/pkg/planner/core/find_best_task.go @@ -1356,6 +1356,10 @@ func findBestTask4DS(ds *DataSource, prop *property.PhysicalProperty, planCounte for _, candidate := range candidates { path := candidate.path if path.PartialIndexPaths != nil { + // prefer tiflash, while current table path is tikv, skip it. + if ds.PreferStoreType&h.PreferTiFlash != 0 && path.StoreType == kv.TiKV { + continue + } idxMergeTask, err := convertToIndexMergeScan(ds, prop, candidate, opt) if err != nil { return nil, 0, err @@ -1491,9 +1495,11 @@ func findBestTask4DS(ds *DataSource, prop *property.PhysicalProperty, planCounte } } if path.IsTablePath() { + // prefer tiflash, while current table path is tikv, skip it. if ds.PreferStoreType&h.PreferTiFlash != 0 && path.StoreType == kv.TiKV { continue } + // prefer tikv, while current table path is tiflash, skip it. if ds.PreferStoreType&h.PreferTiKV != 0 && path.StoreType == kv.TiFlash { continue }