From 75cce840ce52f2b57ce94bd74f7c5aa5b596bd17 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Mon, 4 Nov 2019 13:55:44 +0800 Subject: [PATCH 1/5] fix bug cost model for tiflash do not worl --- planner/core/find_best_task.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index fd2e8e476c744..11e625f5bcce2 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -985,6 +985,12 @@ func (ds *DataSource) convertToTableScan(prop *property.PhysicalProperty, candid return invalidTask, nil } ts, cost, _ := ds.getOriginalPhysicalTableScan(prop, candidate.path, candidate.isMatchProp) + if ds.preferStoreType&preferTiFlash != 0 && ts.StoreType == kv.TiKV { + return invalidTask, nil + } + if ds.preferStoreType&preferTiKV != 0 && ts.StoreType == kv.TiFlash { + return invalidTask, nil + } copTask := &copTask{ tablePlan: ts, indexPlanFinished: true, @@ -1026,14 +1032,12 @@ func (ds *DataSource) getOriginalPhysicalTableScan(prop *property.PhysicalProper Ranges: path.ranges, AccessCondition: path.accessConds, filterCondition: path.tableFilters, + StoreType: path.storeType, }.Init(ds.ctx, ds.blockOffset) - if ds.preferStoreType&preferTiFlash != 0 { - ts.StoreType = kv.TiFlash + if ts.StoreType == kv.TiFlash { ts.filterCondition = append(ts.filterCondition, ts.AccessCondition...) ts.AccessCondition = nil ts.Ranges = ranger.FullIntRange(false) - } else { - ts.StoreType = kv.TiKV } ts.SetSchema(ds.schema) if ts.Table.PKIsHandle { From 6c4809da313b16e0fd0f62bc700d3de26d20ab49 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Mon, 4 Nov 2019 14:28:27 +0800 Subject: [PATCH 2/5] fix ci --- planner/core/find_best_task.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index 11e625f5bcce2..c0aaa7b170ade 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -985,12 +985,6 @@ func (ds *DataSource) convertToTableScan(prop *property.PhysicalProperty, candid return invalidTask, nil } ts, cost, _ := ds.getOriginalPhysicalTableScan(prop, candidate.path, candidate.isMatchProp) - if ds.preferStoreType&preferTiFlash != 0 && ts.StoreType == kv.TiKV { - return invalidTask, nil - } - if ds.preferStoreType&preferTiKV != 0 && ts.StoreType == kv.TiFlash { - return invalidTask, nil - } copTask := &copTask{ tablePlan: ts, indexPlanFinished: true, @@ -1034,6 +1028,12 @@ func (ds *DataSource) getOriginalPhysicalTableScan(prop *property.PhysicalProper filterCondition: path.tableFilters, StoreType: path.storeType, }.Init(ds.ctx, ds.blockOffset) + if ds.preferStoreType&preferTiFlash != 0 { + ts.StoreType = kv.TiFlash + } + if ds.preferStoreType&preferTiKV != 0 { + ts.StoreType = kv.TiKV + } if ts.StoreType == kv.TiFlash { ts.filterCondition = append(ts.filterCondition, ts.AccessCondition...) ts.AccessCondition = nil From d92c37ae9e4612d674e6b82476fccc9e2c584197 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Mon, 4 Nov 2019 15:32:59 +0800 Subject: [PATCH 3/5] add test --- planner/core/cbo_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/planner/core/cbo_test.go b/planner/core/cbo_test.go index e3dd1f006cdce..fa01708a57ca8 100644 --- a/planner/core/cbo_test.go +++ b/planner/core/cbo_test.go @@ -17,6 +17,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/pingcap/parser/model" "io/ioutil" "path/filepath" "strings" @@ -1018,3 +1019,37 @@ func (s *testAnalyzeSuite) TestUpdateProjEliminate(c *C) { tk.MustExec("create table t(a int, b int)") tk.MustExec("explain update t t1, (select distinct b from t) t2 set t1.b = t2.b") } + +func (s *testAnalyzeSuite) TestTiFlashCostModel(c *C) { + store, dom, err := newStoreWithBootstrap() + c.Assert(err, IsNil) + tk := testkit.NewTestKit(c, store) + defer func() { + dom.Close() + store.Close() + }() + + tk.MustExec("use test") + tk.MustExec("create table t (a int, b int, c int, primary key(a))") + tk.MustExec("insert into t values(1,1,1), (2,2,2), (3,3,3)") + + tbl, err := dom.InfoSchema().TableByName(model.CIStr{"test", "test"}, model.CIStr{"t", "t"}) + c.Assert(err, IsNil) + // Set the hacked TiFlash replica for explain tests. + tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{Count: 1, Available: true} + + tk.MustQuery("desc select * from t").Check(testkit.Rows( + "TableReader_7 10000.00 root data:TableScan_6", + "└─TableScan_6 10000.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo", + )) + tk.MustQuery("desc select * from t where t.a = 1 or t.a = 2").Check(testkit.Rows( + "TableReader_6 2.00 root data:TableScan_5", + "└─TableScan_5 2.00 cop[tikv] table:t, range:[1,1], [2,2], keep order:false, stats:pseudo", + )) + tk.MustExec("set @@session.tidb_isolation_read_engines='tiflash'") + tk.MustQuery("desc select * from t where t.a = 1 or t.a = 2").Check(testkit.Rows( + "TableReader_7 2.00 root data:Selection_6", + "└─Selection_6 2.00 cop[tiflash] or(eq(Column#1, 1), eq(Column#1, 2))", + " └─TableScan_5 2.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo", + )) +} From 3342834f057b179380d7cd9a6c64e7a83b2fc152 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Mon, 4 Nov 2019 15:33:50 +0800 Subject: [PATCH 4/5] format --- planner/core/cbo_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/cbo_test.go b/planner/core/cbo_test.go index fa01708a57ca8..de8abfa4588e5 100644 --- a/planner/core/cbo_test.go +++ b/planner/core/cbo_test.go @@ -17,7 +17,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/pingcap/parser/model" "io/ioutil" "path/filepath" "strings" @@ -25,6 +24,7 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/errors" + "github.com/pingcap/parser/model" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" From 89777fe65ad8731b748af9d052fbff88ae730328 Mon Sep 17 00:00:00 2001 From: lzmhhh123 Date: Mon, 4 Nov 2019 15:36:37 +0800 Subject: [PATCH 5/5] fix format --- planner/core/cbo_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/cbo_test.go b/planner/core/cbo_test.go index de8abfa4588e5..75cc9c04f815d 100644 --- a/planner/core/cbo_test.go +++ b/planner/core/cbo_test.go @@ -1033,7 +1033,7 @@ func (s *testAnalyzeSuite) TestTiFlashCostModel(c *C) { tk.MustExec("create table t (a int, b int, c int, primary key(a))") tk.MustExec("insert into t values(1,1,1), (2,2,2), (3,3,3)") - tbl, err := dom.InfoSchema().TableByName(model.CIStr{"test", "test"}, model.CIStr{"t", "t"}) + tbl, err := dom.InfoSchema().TableByName(model.CIStr{O: "test", L: "test"}, model.CIStr{O: "t", L: "t"}) c.Assert(err, IsNil) // Set the hacked TiFlash replica for explain tests. tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{Count: 1, Available: true}