Skip to content

Commit 9ff5f21

Browse files
committed
executor: use statement context for analyze sample rate
1 parent da1090b commit 9ff5f21

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

pkg/executor/builder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,7 +3173,7 @@ func (b *executorBuilder) buildAnalyzeSamplingPushdown(
31733173
if opts[ast.AnalyzeOptNumSamples] == 0 {
31743174
*sampleRate = math.Float64frombits(opts[ast.AnalyzeOptSampleRate])
31753175
if *sampleRate < 0 {
3176-
*sampleRate, sampleRateReason = b.getAdjustedSampleRate(task)
3176+
*sampleRate, sampleRateReason = b.getAdjustedSampleRate(b.stmtCtx, task)
31773177
if task.PartitionName != "" {
31783178
sc.AppendNote(errors.NewNoStackErrorf(
31793179
`Analyze use auto adjusted sample rate %f for table %s.%s's partition %s, reason to use this rate is "%s"`,
@@ -3255,7 +3255,7 @@ func (b *executorBuilder) buildAnalyzeSamplingPushdown(
32553255
// If we take n = 1e12, a 300*k sample still gives <= 0.66 bin size error with probability 0.99.
32563256
// So if we don't consider the top-n values, we can keep the sample size at 300*256.
32573257
// But we may take some top-n before building the histogram, so we increase the sample a little.
3258-
func (b *executorBuilder) getAdjustedSampleRate(task plannercore.AnalyzeColumnsTask) (sampleRate float64, reason string) {
3258+
func (b *executorBuilder) getAdjustedSampleRate(ctx context.Context, task plannercore.AnalyzeColumnsTask) (sampleRate float64, reason string) {
32593259
statsHandle := domain.GetDomain(b.sctx).StatsHandle()
32603260
defaultRate := 0.001
32613261
if statsHandle == nil {
@@ -3268,7 +3268,7 @@ func (b *executorBuilder) getAdjustedSampleRate(task plannercore.AnalyzeColumnsT
32683268
} else {
32693269
statsTbl = statsHandle.GetPhysicalTableStats(tid, task.TblInfo)
32703270
}
3271-
approxiCount, hasPD := b.getApproximateTableCountFromStorage(tid, task)
3271+
approxiCount, hasPD := b.getApproximateTableCountFromStorage(ctx, tid, task)
32723272
// If there's no stats meta and no pd, return the default rate.
32733273
if statsTbl == nil && !hasPD {
32743274
return defaultRate, fmt.Sprintf("TiDB cannot get the row count of the table, use the default-rate=%v", defaultRate)
@@ -3297,8 +3297,8 @@ func (b *executorBuilder) getAdjustedSampleRate(task plannercore.AnalyzeColumnsT
32973297
return sampleRate, fmt.Sprintf("use min(1, %v/%v) as the sample-rate=%v", config.DefRowsForSampleRate, statsTbl.RealtimeCount, sampleRate)
32983298
}
32993299

3300-
func (b *executorBuilder) getApproximateTableCountFromStorage(tid int64, task plannercore.AnalyzeColumnsTask) (float64, bool) {
3301-
return pdhelper.GlobalPDHelper.GetApproximateTableCountFromStorage(context.Background(), b.sctx, tid, task.DBName, task.TableName, task.PartitionName)
3300+
func (b *executorBuilder) getApproximateTableCountFromStorage(ctx context.Context, tid int64, task plannercore.AnalyzeColumnsTask) (float64, bool) {
3301+
return pdhelper.GlobalPDHelper.GetApproximateTableCountFromStorage(ctx, b.sctx, tid, task.DBName, task.TableName, task.PartitionName)
33023302
}
33033303

33043304
func (b *executorBuilder) buildAnalyze(v *plannercore.Analyze) exec.Executor {

0 commit comments

Comments
 (0)