@@ -31,10 +31,12 @@ import (
31
31
type analyzeType string
32
32
33
33
const (
34
- analyzeTable analyzeType = "analyzeTable"
35
- analyzeIndex analyzeType = "analyzeIndex"
36
- analyzePartition analyzeType = "analyzePartition"
37
- analyzePartitionIndex analyzeType = "analyzePartitionIndex"
34
+ analyzeTable analyzeType = "analyzeTable"
35
+ analyzeIndex analyzeType = "analyzeIndex"
36
+ analyzeStaticPartition analyzeType = "analyzeStaticPartition"
37
+ analyzeStaticPartitionIndex analyzeType = "analyzeStaticPartitionIndex"
38
+ analyzePartition analyzeType = "analyzePartition"
39
+ analyzePartitionIndex analyzeType = "analyzePartitionIndex"
38
40
)
39
41
40
42
// defaultFailedAnalysisWaitTime is the default wait time for the next analysis after a failed analysis.
@@ -50,22 +52,103 @@ type TableAnalysisJob struct {
50
52
// and we don't want to analyze the same partition multiple times.
51
53
// For example, the user may analyze some partitions manually, and we don't want to analyze them again.
52
54
PartitionIndexes map [string ][]string
53
- TableSchema string
54
- TableName string
55
- // Only set when table's indexes need to be analyzed.
55
+
56
+ TableSchema string
57
+ // For physical or global tables.
58
+ TableName string
59
+ // For static pruned tables.
60
+ StaticPartitionName string
61
+ // Only set when table or static partition's indexes need to be analyzed.
56
62
// This is only for newly added indexes.
57
63
Indexes []string
64
+
58
65
// Only set when table's partitions need to be analyzed.
59
66
// This will analyze all indexes and columns of the specified partitions.
60
- Partitions []string
61
- TableID int64
67
+ Partitions []string
68
+ // The global table ID.
69
+ TableID int64
70
+ // The static partition ID.
71
+ StaticPartitionID int64
72
+
62
73
TableStatsVer int
63
74
ChangePercentage float64
64
75
TableSize float64
65
76
LastAnalysisDuration time.Duration
66
77
Weight float64
67
78
}
68
79
80
+ // NewStaticPartitionTableAnalysisJob creates a new TableAnalysisJob for analyzing the static partition.
81
+ func NewStaticPartitionTableAnalysisJob (
82
+ schema , globalTableName string ,
83
+ globalTableID int64 ,
84
+ partitionName string ,
85
+ partitionID int64 ,
86
+ indexes []string ,
87
+ tableStatsVer int ,
88
+ changePercentage float64 ,
89
+ tableSize float64 ,
90
+ lastAnalysisDuration time.Duration ,
91
+ ) * TableAnalysisJob {
92
+ return & TableAnalysisJob {
93
+ TableSchema : schema ,
94
+ TableName : globalTableName ,
95
+ TableID : globalTableID ,
96
+ StaticPartitionName : partitionName ,
97
+ StaticPartitionID : partitionID ,
98
+ Indexes : indexes ,
99
+ TableStatsVer : tableStatsVer ,
100
+ ChangePercentage : changePercentage ,
101
+ TableSize : tableSize ,
102
+ LastAnalysisDuration : lastAnalysisDuration ,
103
+ }
104
+ }
105
+
106
+ // NewNonPartitionedTableAnalysisJob creates a new TableAnalysisJob for analyzing the table or partition.
107
+ func NewNonPartitionedTableAnalysisJob (
108
+ schema , tableName string ,
109
+ tableID int64 ,
110
+ indexes []string ,
111
+ tableStatsVer int ,
112
+ changePercentage float64 ,
113
+ tableSize float64 ,
114
+ lastAnalysisDuration time.Duration ,
115
+ ) * TableAnalysisJob {
116
+ return & TableAnalysisJob {
117
+ TableSchema : schema ,
118
+ TableName : tableName ,
119
+ TableID : tableID ,
120
+ Indexes : indexes ,
121
+ TableStatsVer : tableStatsVer ,
122
+ ChangePercentage : changePercentage ,
123
+ TableSize : tableSize ,
124
+ LastAnalysisDuration : lastAnalysisDuration ,
125
+ }
126
+ }
127
+
128
+ // NewDynamicPartitionTableAnalysisJob creates a new TableAnalysisJob for analyzing dynamic partitioned table.
129
+ func NewDynamicPartitionTableAnalysisJob (
130
+ schema , tableName string ,
131
+ tableID int64 ,
132
+ partitions []string ,
133
+ partitionIndexes map [string ][]string ,
134
+ tableStatsVer int ,
135
+ changePercentage float64 ,
136
+ tableSize float64 ,
137
+ lastAnalysisDuration time.Duration ,
138
+ ) * TableAnalysisJob {
139
+ return & TableAnalysisJob {
140
+ TableSchema : schema ,
141
+ TableName : tableName ,
142
+ TableID : tableID ,
143
+ Partitions : partitions ,
144
+ PartitionIndexes : partitionIndexes ,
145
+ TableStatsVer : tableStatsVer ,
146
+ ChangePercentage : changePercentage ,
147
+ TableSize : tableSize ,
148
+ LastAnalysisDuration : lastAnalysisDuration ,
149
+ }
150
+ }
151
+
69
152
// HasNewlyAddedIndex checks whether the table has newly added index.
70
153
func (j * TableAnalysisJob ) HasNewlyAddedIndex () bool {
71
154
return len (j .PartitionIndexes ) > 0 || len (j .Indexes ) > 0
@@ -79,9 +162,10 @@ func (j *TableAnalysisJob) IsValidToAnalyze(
79
162
sctx sessionctx.Context ,
80
163
) (bool , string ) {
81
164
// No need to analyze this table.
82
- // TODO: Usually, we should not put this kind of table into the queue.
83
- if j .Weight == 0 {
84
- return false , "weight is 0"
165
+ // Usually, we should not put this kind of table into the queue.
166
+ // This is just a double check.
167
+ if j .Weight <= 0 {
168
+ return false , fmt .Sprintf ("weight is less than or equal to 0: %.4f" , j .Weight )
85
169
}
86
170
87
171
// Check whether the table or partition is valid to analyze.
@@ -97,6 +181,17 @@ func (j *TableAnalysisJob) IsValidToAnalyze(
97
181
); ! valid {
98
182
return false , failReason
99
183
}
184
+ } else if j .StaticPartitionName != "" {
185
+ // For static partition table we only need to check the specified static partition.
186
+ partitionNames := []string {j .StaticPartitionName }
187
+ if valid , failReason := isValidToAnalyze (
188
+ sctx ,
189
+ j .TableSchema ,
190
+ j .TableName ,
191
+ partitionNames ... ,
192
+ ); ! valid {
193
+ return false , failReason
194
+ }
100
195
} else {
101
196
if valid , failReason := isValidToAnalyze (
102
197
sctx ,
@@ -219,7 +314,11 @@ func (j *TableAnalysisJob) Analyze(
219
314
case analyzePartition :
220
315
j .analyzePartitions (sctx , statsHandle , sysProcTracker )
221
316
case analyzePartitionIndex :
222
- j .AnalyzePartitionIndexes (sctx , statsHandle , sysProcTracker )
317
+ j .analyzePartitionIndexes (sctx , statsHandle , sysProcTracker )
318
+ case analyzeStaticPartition :
319
+ j .analyzeStaticPartition (sctx , statsHandle , sysProcTracker )
320
+ case analyzeStaticPartitionIndex :
321
+ j .analyzeStaticPartitionIndexes (sctx , statsHandle , sysProcTracker )
223
322
}
224
323
}
225
324
@@ -230,7 +329,12 @@ func (j *TableAnalysisJob) getAnalyzeType() analyzeType {
230
329
case len (j .Partitions ) > 0 :
231
330
return analyzePartition
232
331
case len (j .Indexes ) > 0 :
332
+ if j .StaticPartitionName != "" {
333
+ return analyzeStaticPartitionIndex
334
+ }
233
335
return analyzeIndex
336
+ case j .StaticPartitionName != "" :
337
+ return analyzeStaticPartition
234
338
default :
235
339
return analyzeTable
236
340
}
@@ -245,6 +349,15 @@ func (j *TableAnalysisJob) analyzeTable(
245
349
exec .AutoAnalyze (sctx , statsHandle , sysProcTracker , j .TableStatsVer , sql , params ... )
246
350
}
247
351
352
+ func (j * TableAnalysisJob ) analyzeStaticPartition (
353
+ sctx sessionctx.Context ,
354
+ statsHandle statstypes.StatsHandle ,
355
+ sysProcTracker sessionctx.SysProcTracker ,
356
+ ) {
357
+ sql , params := j .GenSQLForAnalyzeStaticPartition ()
358
+ exec .AutoAnalyze (sctx , statsHandle , sysProcTracker , j .TableStatsVer , sql , params ... )
359
+ }
360
+
248
361
func (j * TableAnalysisJob ) analyzeIndexes (
249
362
sctx sessionctx.Context ,
250
363
statsHandle statstypes.StatsHandle ,
@@ -256,6 +369,17 @@ func (j *TableAnalysisJob) analyzeIndexes(
256
369
}
257
370
}
258
371
372
+ func (j * TableAnalysisJob ) analyzeStaticPartitionIndexes (
373
+ sctx sessionctx.Context ,
374
+ statsHandle statstypes.StatsHandle ,
375
+ sysProcTracker sessionctx.SysProcTracker ,
376
+ ) {
377
+ for _ , index := range j .Indexes {
378
+ sql , params := j .GenSQLForAnalyzeStaticPartitionIndex (index )
379
+ exec .AutoAnalyze (sctx , statsHandle , sysProcTracker , j .TableStatsVer , sql , params ... )
380
+ }
381
+ }
382
+
259
383
// analyzePartitions performs analysis on the specified partitions.
260
384
// This function uses a batch mode for efficiency. After analyzing the partitions,
261
385
// it's necessary to merge their statistics. By analyzing them in batches,
@@ -283,8 +407,8 @@ func (j *TableAnalysisJob) analyzePartitions(
283
407
}
284
408
}
285
409
286
- // AnalyzePartitionIndexes performs analysis on the specified partition indexes.
287
- func (j * TableAnalysisJob ) AnalyzePartitionIndexes (
410
+ // analyzePartitionIndexes performs analysis on the specified partition indexes.
411
+ func (j * TableAnalysisJob ) analyzePartitionIndexes (
288
412
sctx sessionctx.Context ,
289
413
statsHandle statstypes.StatsHandle ,
290
414
sysProcTracker sessionctx.SysProcTracker ,
@@ -332,6 +456,14 @@ func (j *TableAnalysisJob) GenSQLForAnalyzeTable() (string, []any) {
332
456
return sql , params
333
457
}
334
458
459
+ // GenSQLForAnalyzeStaticPartition generates the SQL for analyzing the specified static partition.
460
+ func (j * TableAnalysisJob ) GenSQLForAnalyzeStaticPartition () (string , []any ) {
461
+ sql := "analyze table %n.%n partition %n"
462
+ params := []any {j .TableSchema , j .TableName , j .StaticPartitionName }
463
+
464
+ return sql , params
465
+ }
466
+
335
467
// GenSQLForAnalyzeIndex generates the SQL for analyzing the specified index.
336
468
func (j * TableAnalysisJob ) GenSQLForAnalyzeIndex (index string ) (string , []any ) {
337
469
sql := "analyze table %n.%n index %n"
@@ -340,6 +472,14 @@ func (j *TableAnalysisJob) GenSQLForAnalyzeIndex(index string) (string, []any) {
340
472
return sql , params
341
473
}
342
474
475
+ // GenSQLForAnalyzeStaticPartitionIndex generates the SQL for analyzing the specified static partition index.
476
+ func (j * TableAnalysisJob ) GenSQLForAnalyzeStaticPartitionIndex (index string ) (string , []any ) {
477
+ sql := "analyze table %n.%n partition %n index %n"
478
+ params := []any {j .TableSchema , j .TableName , j .StaticPartitionName , index }
479
+
480
+ return sql , params
481
+ }
482
+
343
483
func (j * TableAnalysisJob ) String () string {
344
484
analyzeType := j .getAnalyzeType ()
345
485
switch analyzeType {
@@ -355,6 +495,12 @@ func (j *TableAnalysisJob) String() string {
355
495
case analyzePartitionIndex :
356
496
return fmt .Sprintf (`TableAnalysisJob: {AnalyzeType: partitionIndex, PartitionIndexes: %v, Schema: %s, Table: %s, TableID: %d, TableStatsVer: %d, ChangePercentage: %.2f, Weight: %.4f}` ,
357
497
j .PartitionIndexes , j .TableSchema , j .TableName , j .TableID , j .TableStatsVer , j .ChangePercentage , j .Weight )
498
+ case analyzeStaticPartition :
499
+ return fmt .Sprintf (`TableAnalysisJob: {AnalyzeType: staticPartition, Schema: %s, Table: %s, TableID: %d, StaticPartition: %s, StaticPartitionID: %d, TableStatsVer: %d, ChangePercentage: %.2f, Weight: %.4f}` ,
500
+ j .TableSchema , j .TableName , j .TableID , j .StaticPartitionName , j .StaticPartitionID , j .TableStatsVer , j .ChangePercentage , j .Weight )
501
+ case analyzeStaticPartitionIndex :
502
+ return fmt .Sprintf (`TableAnalysisJob: {AnalyzeType: staticPartitionIndex, Indexes: %s, Schema: %s, Table: %s, TableID: %d, StaticPartition: %s, StaticPartitionID: %d, TableStatsVer: %d, ChangePercentage: %.2f, Weight: %.4f}` ,
503
+ strings .Join (j .Indexes , ", " ), j .TableSchema , j .TableName , j .TableID , j .StaticPartitionName , j .StaticPartitionID , j .TableStatsVer , j .ChangePercentage , j .Weight )
358
504
default :
359
505
return "TableAnalysisJob: {AnalyzeType: unknown}"
360
506
}
0 commit comments