Skip to content

Commit

Permalink
planner: introduce some new variables for the cost model version2 (pi…
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 committed Jul 12, 2022
1 parent a0da136 commit 761bb3c
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
9 changes: 9 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,15 @@ func TestSetVar(t *testing.T) {
tk.MustQuery("select @@tidb_max_auto_analyze_time").Check(testkit.Rows("60"))
tk.MustExec("set global tidb_max_auto_analyze_time = -1")
tk.MustQuery("select @@tidb_max_auto_analyze_time").Check(testkit.Rows("0"))

// test variables for cost model ver2
tk.MustQuery("select @@tidb_cost_model_version").Check(testkit.Rows("1"))
tk.MustExec("set tidb_cost_model_version=3")
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Truncated incorrect tidb_cost_model_version value: '3'"))
tk.MustExec("set tidb_cost_model_version=0")
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Truncated incorrect tidb_cost_model_version value: '0'"))
tk.MustExec("set tidb_cost_model_version=2")
tk.MustQuery("select @@tidb_cost_model_version").Check(testkit.Rows("2"))
}

func TestTruncateIncorrectIntSessionVar(t *testing.T) {
Expand Down
63 changes: 63 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,30 @@ type SessionVars struct {
// concurrencyFactor is the CPU cost of additional one goroutine.
concurrencyFactor float64

// factors for cost model v2
// cpuFactorV2 is the CPU factor for the Cost Model Ver2.
cpuFactorV2 float64
// copCPUFactorV2 is the cop-cpu factor for the Cost Model Ver2.
copCPUFactorV2 float64
// tiflashCPUFactorV2 is the cop-cpu factor for the Cost Model Ver2.
tiflashCPUFactorV2 float64
// networkFactorV2 is the network factor for the Cost Model Ver2.
networkFactorV2 float64
// scanFactorV2 is the scan factor for the Cost Model Ver2.
scanFactorV2 float64
// descScanFactorV2 is the desc-scan factor for the Cost Model Ver2.
descScanFactorV2 float64
// tiflashScanFactorV2 is the tiflash-scan factor for the Cost Model Ver2.
tiflashScanFactorV2 float64
// seekFactorV2 is the seek factor for the Cost Model Ver2.
seekFactorV2 float64
// memoryFactorV2 is the memory factor for the Cost Model Ver2.
memoryFactorV2 float64
// diskFactorV2 is the disk factor for the Cost Model Ver2.
diskFactorV2 float64
// concurrencyFactorV2 is the concurrency factor for the Cost Model Ver2.
concurrencyFactorV2 float64

// CopTiFlashConcurrencyFactor is the concurrency number of computation in tiflash coprocessor.
CopTiFlashConcurrencyFactor float64

Expand Down Expand Up @@ -1034,6 +1058,8 @@ type SessionVars struct {
IgnorePreparedCacheCloseStmt bool
// EnableNewCostInterface is a internal switch to indicates whether to use the new cost calculation interface.
EnableNewCostInterface bool
// CostModelVersion is a internal switch to indicates the Cost Model Version.
CostModelVersion int
// BatchPendingTiFlashCount shows the threshold of pending TiFlash tables when batch adding.
BatchPendingTiFlashCount int
// RcReadCheckTS indicates if ts check optimization is enabled for current session.
Expand Down Expand Up @@ -2350,26 +2376,46 @@ func (s *SessionVars) CleanupTxnReadTSIfUsed() {

// GetCPUFactor returns the session variable cpuFactor
func (s *SessionVars) GetCPUFactor() float64 {
if s.CostModelVersion == 2 {
return s.cpuFactorV2
}
return s.cpuFactor
}

// GetCopCPUFactor returns the session variable copCPUFactor
func (s *SessionVars) GetCopCPUFactor() float64 {
if s.CostModelVersion == 2 {
return s.copCPUFactorV2
}
return s.copCPUFactor
}

// GetTiFlashCPUFactor returns the session
func (s *SessionVars) GetTiFlashCPUFactor() float64 {
return s.tiflashCPUFactorV2
}

// GetMemoryFactor returns the session variable memoryFactor
func (s *SessionVars) GetMemoryFactor() float64 {
if s.CostModelVersion == 2 {
return s.memoryFactorV2
}
return s.memoryFactor
}

// GetDiskFactor returns the session variable diskFactor
func (s *SessionVars) GetDiskFactor() float64 {
if s.CostModelVersion == 2 {
return s.diskFactorV2
}
return s.diskFactor
}

// GetConcurrencyFactor returns the session variable concurrencyFactor
func (s *SessionVars) GetConcurrencyFactor() float64 {
if s.CostModelVersion == 2 {
return s.concurrencyFactorV2
}
return s.concurrencyFactor
}

Expand All @@ -2381,6 +2427,9 @@ func (s *SessionVars) GetNetworkFactor(tbl *model.TableInfo) float64 {
return 0
}
}
if s.CostModelVersion == 2 {
return s.networkFactorV2
}
return s.networkFactor
}

Expand All @@ -2392,6 +2441,9 @@ func (s *SessionVars) GetScanFactor(tbl *model.TableInfo) float64 {
return 0
}
}
if s.CostModelVersion == 2 {
return s.scanFactorV2
}
return s.scanFactor
}

Expand All @@ -2403,9 +2455,17 @@ func (s *SessionVars) GetDescScanFactor(tbl *model.TableInfo) float64 {
return 0
}
}
if s.CostModelVersion == 2 {
return s.descScanFactorV2
}
return s.descScanFactor
}

// GetTiFlashScanFactor returns the session variable tiflashScanFactorV2
func (s *SessionVars) GetTiFlashScanFactor() float64 {
return s.tiflashScanFactorV2
}

// GetSeekFactor returns the session variable seekFactor
// returns 0 when tbl is a temporary table.
func (s *SessionVars) GetSeekFactor(tbl *model.TableInfo) float64 {
Expand All @@ -2414,6 +2474,9 @@ func (s *SessionVars) GetSeekFactor(tbl *model.TableInfo) float64 {
return 0
}
}
if s.CostModelVersion == 2 {
return s.seekFactorV2
}
return s.seekFactor
}

Expand Down
50 changes: 50 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,50 @@ var defaultSysVars = []*SysVar{
s.concurrencyFactor = tidbOptFloat64(val, DefOptConcurrencyFactor)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptCPUFactorV2, Value: strconv.FormatFloat(DefOptCPUFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.cpuFactorV2 = tidbOptFloat64(val, DefOptCPUFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptCopCPUFactorV2, Value: strconv.FormatFloat(DefOptCopCPUFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.copCPUFactorV2 = tidbOptFloat64(val, DefOptCopCPUFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptTiFlashCPUFactorV2, Value: strconv.FormatFloat(DefOptTiFlashCPUFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.tiflashCPUFactorV2 = tidbOptFloat64(val, DefOptTiFlashCPUFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptNetworkFactorV2, Value: strconv.FormatFloat(DefOptNetworkFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.networkFactorV2 = tidbOptFloat64(val, DefOptNetworkFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptScanFactorV2, Value: strconv.FormatFloat(DefOptScanFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.scanFactorV2 = tidbOptFloat64(val, DefOptScanFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptDescScanFactorV2, Value: strconv.FormatFloat(DefOptDescScanFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.descScanFactorV2 = tidbOptFloat64(val, DefOptDescScanFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptTiFlashScanFactorV2, Value: strconv.FormatFloat(DefOptTiFlashScanFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.tiflashScanFactorV2 = tidbOptFloat64(val, DefOptTiFlashScanFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptSeekFactorV2, Value: strconv.FormatFloat(DefOptSeekFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.seekFactorV2 = tidbOptFloat64(val, DefOptSeekFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptMemoryFactorV2, Value: strconv.FormatFloat(DefOptMemoryFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.memoryFactorV2 = tidbOptFloat64(val, DefOptMemoryFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptDiskFactorV2, Value: strconv.FormatFloat(DefOptDiskFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.diskFactorV2 = tidbOptFloat64(val, DefOptDiskFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptConcurrencyFactorV2, Value: strconv.FormatFloat(DefOptConcurrencyFactorV2, 'f', -1, 64), Hidden: true, Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64, SetSession: func(s *SessionVars, val string) error {
s.concurrencyFactorV2 = tidbOptFloat64(val, DefOptConcurrencyFactorV2)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBIndexJoinBatchSize, Value: strconv.Itoa(DefIndexJoinBatchSize), Type: TypeUnsigned, MinValue: 1, MaxValue: math.MaxInt32, SetSession: func(s *SessionVars, val string) error {
s.IndexJoinBatchSize = tidbOptPositiveInt32(val, DefIndexJoinBatchSize)
return nil
Expand Down Expand Up @@ -1558,6 +1602,12 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBCostModelVersion, Value: strconv.Itoa(1), Hidden: true, Type: TypeInt, MinValue: 1, MaxValue: 2,
SetSession: func(vars *SessionVars, s string) error {
vars.CostModelVersion = int(TidbOptInt64(s, 1))
return nil
},
},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBRCReadCheckTS, Type: TypeBool, Value: BoolToOnOff(DefRCReadCheckTS), SetSession: func(s *SessionVars, val string) error {
s.RcReadCheckTS = TiDBOptOn(val)
return nil
Expand Down
38 changes: 38 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,30 @@ const (
// TiDBOptConcurrencyFactor is the CPU cost of additional one goroutine.
TiDBOptConcurrencyFactor = "tidb_opt_concurrency_factor"

// Variables for the Cost Model Ver2
// TiDBOptCPUFactorV2 is the CPU factor for the Cost Model Ver2
TiDBOptCPUFactorV2 = "tidb_opt_cpu_factor_v2"
// TiDBOptCopCPUFactorV2 is the CopCPU factor for the Cost Model Ver2
TiDBOptCopCPUFactorV2 = "tidb_opt_copcpu_factor_v2"
// TiDBOptTiFlashCPUFactorV2 is the TiFlashCPU factor for the Cost Model Ver2
TiDBOptTiFlashCPUFactorV2 = "tidb_opt_tiflash_cpu_factor_v2"
// TiDBOptNetworkFactorV2 is the network factor for the Cost Model Ver2
TiDBOptNetworkFactorV2 = "tidb_opt_network_factor_v2"
// TiDBOptScanFactorV2 is the scan factor for the Cost Model Ver2
TiDBOptScanFactorV2 = "tidb_opt_scan_factor_v2"
// TiDBOptDescScanFactorV2 is the desc scan factor for the Cost Model Ver2
TiDBOptDescScanFactorV2 = "tidb_opt_desc_factor_v2"
// TiDBOptTiFlashScanFactorV2 is the TiFlashScan factor for the Cost Model Ver2
TiDBOptTiFlashScanFactorV2 = "tidb_opt_tiflash_scan_factor_v2"
// TiDBOptSeekFactorV2 is the seek factor for the Cost Model Ver2
TiDBOptSeekFactorV2 = "tidb_opt_seek_factor_v2"
// TiDBOptMemoryFactorV2 is the memory factor for the Cost Model Ver2
TiDBOptMemoryFactorV2 = "tidb_opt_memory_factor_v2"
// TiDBOptDiskFactorV2 is the disk factor for the Cost Model Ver2
TiDBOptDiskFactorV2 = "tidb_opt_disk_factor_v2"
// TiDBOptConcurrencyFactorV2 is the concurrency factor for the Cost Model Ver2
TiDBOptConcurrencyFactorV2 = "tidb_opt_concurrency_factor_v2"

// TiDBIndexJoinBatchSize is used to set the batch size of an index lookup join.
// The index lookup join fetches batches of data from outer executor and constructs ranges for inner executor.
// This value controls how much of data in a batch to do the index join.
Expand Down Expand Up @@ -637,6 +661,9 @@ const (
// TiDBEnableNewCostInterface is a internal switch to indicates whether to use the new cost calculation interface.
TiDBEnableNewCostInterface = "tidb_enable_new_cost_interface"

// TiDBCostModelVersion is a internal switch to indicates the cost model version.
TiDBCostModelVersion = "tidb_cost_model_version"

// TiDBBatchPendingTiFlashCount indicates the maximum count of non-available TiFlash tables.
TiDBBatchPendingTiFlashCount = "tidb_batch_pending_tiflash_count"

Expand Down Expand Up @@ -751,6 +778,17 @@ const (
DefOptMemoryFactor = 0.001
DefOptDiskFactor = 1.5
DefOptConcurrencyFactor = 3.0
DefOptCPUFactorV2 = 30.0
DefOptCopCPUFactorV2 = 30.0
DefOptTiFlashCPUFactorV2 = 2.0
DefOptNetworkFactorV2 = 4.0
DefOptScanFactorV2 = 100.0
DefOptDescScanFactorV2 = 150.0
DefOptTiFlashScanFactorV2 = 15.0
DefOptSeekFactorV2 = 9500000.0
DefOptMemoryFactorV2 = 0.001
DefOptDiskFactorV2 = 1.5
DefOptConcurrencyFactorV2 = 3.0
DefOptInSubqToJoinAndAgg = true
DefOptPreferRangeScan = false
DefBatchInsert = false
Expand Down

0 comments on commit 761bb3c

Please sign in to comment.