Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#38358
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
Yisaer authored and ti-chi-bot committed Oct 18, 2022
1 parent 7228bb3 commit a9c8223
Show file tree
Hide file tree
Showing 7 changed files with 864 additions and 3 deletions.
79 changes: 79 additions & 0 deletions executor/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ package executor_test
import (
"context"
"fmt"
<<<<<<< HEAD
=======
"io/ioutil"
>>>>>>> e8d265981a (statistics: support merge global topn in concurrency (#38358))
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -1159,3 +1163,78 @@ func (s *testSuite10) TestAdjustSampleRateNote(c *C) {
tk.MustExec("analyze table t")
tk.MustQuery("show warnings").Check(testkit.Rows("Note 1105 Analyze use auto adjusted sample rate 1.000000 for table test.t."))
}

func TestAnalyzePartitionTableByConcurrencyInDynamic(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("set @@tidb_partition_prune_mode='dynamic'")
tk.MustExec("use test")
tk.MustExec("create table t(id int) partition by hash(id) partitions 4")
testcases := []struct {
concurrency string
}{
{
concurrency: "1",
},
{
concurrency: "2",
},
{
concurrency: "3",
},
{
concurrency: "4",
},
{
concurrency: "5",
},
}
// assert empty table
for _, tc := range testcases {
concurrency := tc.concurrency
fmt.Println("testcase ", concurrency)
tk.MustExec(fmt.Sprintf("set @@tidb_merge_partition_stats_concurrency=%v", concurrency))
tk.MustQuery("select @@tidb_merge_partition_stats_concurrency").Check(testkit.Rows(concurrency))
tk.MustExec("analyze table t")
tk.MustQuery("show stats_topn where partition_name = 'global' and table_name = 't'")
}

for i := 1; i <= 500; i++ {
for j := 1; j <= 20; j++ {
tk.MustExec(fmt.Sprintf("insert into t (id) values (%v)", j))
}
}
var expected [][]interface{}
for i := 1; i <= 20; i++ {
expected = append(expected, []interface{}{
strconv.FormatInt(int64(i), 10), "500",
})
}
testcases = []struct {
concurrency string
}{
{
concurrency: "1",
},
{
concurrency: "2",
},
{
concurrency: "3",
},
{
concurrency: "4",
},
{
concurrency: "5",
},
}
for _, tc := range testcases {
concurrency := tc.concurrency
fmt.Println("testcase ", concurrency)
tk.MustExec(fmt.Sprintf("set @@tidb_merge_partition_stats_concurrency=%v", concurrency))
tk.MustQuery("select @@tidb_merge_partition_stats_concurrency").Check(testkit.Rows(concurrency))
tk.MustExec("analyze table t")
tk.MustQuery("show stats_topn where partition_name = 'global' and table_name = 't'").CheckAt([]int{5, 6}, expected)
}
}
129 changes: 129 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,135 @@ type SessionVars struct {
curr int8
data [2]stmtctx.StatementContext
}
<<<<<<< HEAD
=======

// Rng stores the rand_seed1 and rand_seed2 for Rand() function
Rng *mathutil.MysqlRng

// EnablePaging indicates whether enable paging in coprocessor requests.
EnablePaging bool

// EnableLegacyInstanceScope says if SET SESSION can be used to set an instance
// scope variable. The default is TRUE.
EnableLegacyInstanceScope bool

// ReadConsistency indicates the read consistency requirement.
ReadConsistency ReadConsistencyLevel

// StatsLoadSyncWait indicates how long to wait for stats load before timeout.
StatsLoadSyncWait int64

// SysdateIsNow indicates whether Sysdate is an alias of Now function
SysdateIsNow bool
// EnableMutationChecker indicates whether to check data consistency for mutations
EnableMutationChecker bool
// AssertionLevel controls how strict the assertions on data mutations should be.
AssertionLevel AssertionLevel
// IgnorePreparedCacheCloseStmt controls if ignore the close-stmt command for prepared statement.
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
// RcWriteCheckTS indicates whether some special write statements don't get latest tso from PD at RC
RcWriteCheckTS bool
// RemoveOrderbyInSubquery indicates whether to remove ORDER BY in subquery.
RemoveOrderbyInSubquery bool
// NonTransactionalIgnoreError indicates whether to ignore error in non-transactional statements.
// When set to false, returns immediately when it meets the first error.
NonTransactionalIgnoreError bool

// MaxAllowedPacket indicates the maximum size of a packet for the MySQL protocol.
MaxAllowedPacket uint64

// TiFlash related optimization, only for MPP.
TiFlashFineGrainedShuffleStreamCount int64
TiFlashFineGrainedShuffleBatchSize uint64

// RequestSourceType is the type of inner request.
RequestSourceType string

// MemoryDebugModeMinHeapInUse indicated the minimum heapInUse threshold that triggers the memoryDebugMode.
MemoryDebugModeMinHeapInUse int64
// MemoryDebugModeAlarmRatio indicated the allowable bias ratio of memory tracking accuracy check.
// When `(memory trakced by tidb) * (1+MemoryDebugModeAlarmRatio) < actual heapInUse`, an alarm log will be recorded.
MemoryDebugModeAlarmRatio int64

// EnableAnalyzeSnapshot indicates whether to read data on snapshot when collecting statistics.
// When it is false, ANALYZE reads the latest data.
// When it is true, ANALYZE reads data on the snapshot at the beginning of ANALYZE.
EnableAnalyzeSnapshot bool

// DefaultStrMatchSelectivity adjust the estimation strategy for string matching expressions that can't be estimated by building into range.
// when > 0: it's the selectivity for the expression.
// when = 0: try to use TopN to evaluate the like expression to estimate the selectivity.
DefaultStrMatchSelectivity float64

// TiFlashFastScan indicates whether use fast scan in TiFlash
TiFlashFastScan bool

// PrimaryKeyRequired indicates if sql_require_primary_key sysvar is set
PrimaryKeyRequired bool

// EnablePreparedPlanCache indicates whether to enable prepared plan cache.
EnablePreparedPlanCache bool

// GeneralPlanCacheSize controls the size of general plan cache.
PreparedPlanCacheSize uint64

// EnableGeneralPlanCache indicates whether to enable general plan cache.
EnableGeneralPlanCache bool

// GeneralPlanCacheSize controls the size of general plan cache.
GeneralPlanCacheSize uint64

// ConstraintCheckInPlacePessimistic controls whether to skip the locking of some keys in pessimistic transactions.
// Postpone the conflict check and constraint check to prewrite or later pessimistic locking requests.
ConstraintCheckInPlacePessimistic bool

// EnableTiFlashReadForWriteStmt indicates whether to enable TiFlash to read for write statements.
EnableTiFlashReadForWriteStmt bool

// EnableUnsafeSubstitute indicates whether to enable generate column takes unsafe substitute.
EnableUnsafeSubstitute bool

// ForeignKeyChecks indicates whether to enable foreign key constraint check.
ForeignKeyChecks bool

// RangeMaxSize is the max memory limit for ranges. When the optimizer estimates that the memory usage of complete
// ranges would exceed the limit, it chooses less accurate ranges such as full range. 0 indicates that there is no
// memory limit for ranges.
RangeMaxSize int64

// LastPlanReplayerToken indicates the last plan replayer token
LastPlanReplayerToken string

// AnalyzePartitionMergeConcurrency indicates concurrency for merging partition stats
AnalyzePartitionMergeConcurrency int

HookContext
}

// GetPreparedStmtByName returns the prepared statement specified by stmtName.
func (s *SessionVars) GetPreparedStmtByName(stmtName string) (interface{}, error) {
stmtID, ok := s.PreparedStmtNameToID[stmtName]
if !ok {
return nil, ErrStmtNotFound
}
return s.GetPreparedStmtByID(stmtID)
}

// GetPreparedStmtByID returns the prepared statement specified by stmtID.
func (s *SessionVars) GetPreparedStmtByID(stmtID uint32) (interface{}, error) {
stmt, ok := s.PreparedStmts[stmtID]
if !ok {
return nil, ErrStmtNotFound
}
return stmt, nil
>>>>>>> e8d265981a (statistics: support merge global topn in concurrency (#38358))
}

// InitStatementContext initializes a StatementContext, the object is reused to reduce allocation.
Expand Down
10 changes: 10 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,7 @@ var defaultSysVars = []*SysVar{
s.EnablePseudoForOutdatedStats = TiDBOptOn(val)
return nil
}},
<<<<<<< HEAD

{Scope: ScopeNone, Name: "version_compile_os", Value: runtime.GOOS},
{Scope: ScopeNone, Name: "version_compile_machine", Value: runtime.GOARCH},
Expand All @@ -1888,6 +1889,15 @@ var GAFunction4ExpressionIndex = map[string]struct{}{
ast.MD5: {},
ast.Reverse: {},
ast.VitessHash: {},
=======
{
Scope: ScopeGlobal | ScopeSession, Name: TiDBMergePartitionStatsConcurrency, Value: strconv.FormatInt(DefTiDBMergePartitionStatsConcurrency, 10), Type: TypeInt, MinValue: 1, MaxValue: MaxConfigurableConcurrency,
SetSession: func(s *SessionVars, val string) error {
s.AnalyzePartitionMergeConcurrency = TidbOptInt(val, DefTiDBMergePartitionStatsConcurrency)
return nil
},
},
>>>>>>> e8d265981a (statistics: support merge global topn in concurrency (#38358))
}

// FeedbackProbability points to the FeedbackProbability in statistics package.
Expand Down
Loading

0 comments on commit a9c8223

Please sign in to comment.