Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 committed Sep 5, 2024
1 parent 7bdc5ee commit 0fef51a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 deletions pkg/planner/indexadvisor/indexadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,37 @@ func AdviseIndexes(ctx context.Context, sctx sessionctx.Context,
return nil, errors.New("nil input")
}

l().Info("start to recommend indexes")
advisorLogger().Info("start to recommend indexes")
defer func() {
if r := recover(); r != nil {
l().Error("panic in AdviseIndexes", zap.Any("recover", r))
advisorLogger().Error("panic in AdviseIndexes", zap.Any("recover", r))
err = fmt.Errorf("panic in AdviseIndexes: %v", r)
}
}()

// prepare what-if optimizer
opt := NewOptimizer(sctx)
l().Info("what-if optimizer prepared")
advisorLogger().Info("what-if optimizer prepared")

defaultDB := sctx.GetSessionVars().CurrentDB
querySet, err := prepareQuerySet(ctx, sctx, defaultDB, opt, option.SpecifiedSQLs)
if err != nil {
l().Error("prepare workload failed", zap.Error(err))
advisorLogger().Error("prepare workload failed", zap.Error(err))
return nil, err
}

// identify indexable columns
indexableColSet, err := CollectIndexableColumnsForQuerySet(opt, querySet)
if err != nil {
l().Error("fill indexable columns failed", zap.Error(err))
advisorLogger().Error("fill indexable columns failed", zap.Error(err))
return nil, err
}
l().Info("indexable columns filled", zap.Int("indexable-cols", indexableColSet.Size()))
advisorLogger().Info("indexable columns filled", zap.Int("indexable-cols", indexableColSet.Size()))

// start the advisor
indexes, err := adviseIndexes(querySet, indexableColSet, option.MaxNumIndexes, option.MaxIndexWidth, opt)
if err != nil {
l().Error("advise indexes failed", zap.Error(err))
advisorLogger().Error("advise indexes failed", zap.Error(err))
return nil, err
}

Expand All @@ -91,8 +91,8 @@ func AdviseIndexes(ctx context.Context, sctx sessionctx.Context,
// prepareQuerySet prepares the target queries for the index advisor.
func prepareQuerySet(ctx context.Context, sctx sessionctx.Context,
defaultDB string, opt Optimizer, specifiedSQLs []string) (s.Set[Query], error) {
l().Info("prepare target query set")
defer l().Info("prepare target query set finished")
advisorLogger().Info("prepare target query set")
defer advisorLogger().Info("prepare target query set finished")

querySet := s.NewSet[Query]()
if len(specifiedSQLs) > 0 { // if target queries are specified
Expand Down Expand Up @@ -133,15 +133,15 @@ func loadQuerySetFromStmtSummary(sessionctx.Context, string) (s.Set[Query], erro
}

func prepareRecommendation(indexes s.Set[Index], queries s.Set[Query], optimizer Optimizer) ([]*Recommendation, error) {
l().Info("recommend index", zap.Int("num-index", indexes.Size()))
advisorLogger().Info("recommend index", zap.Int("num-index", indexes.Size()))
results := make([]*Recommendation, 0, indexes.Size())
for _, idx := range indexes.ToList() {
workloadImpact := new(WorkloadImpact)
var cols []string
for _, col := range idx.Columns {
cols = append(cols, strings.Trim(col.ColumnName, `'" `))
}
l().Info("index columns", zap.Strings("columns", cols), zap.Any("index-cols", idx.Columns))
advisorLogger().Info("index columns", zap.Strings("columns", cols), zap.Any("index-cols", idx.Columns))
indexResult := &Recommendation{
Database: idx.SchemaName,
Table: idx.TableName,
Expand All @@ -150,12 +150,12 @@ func prepareRecommendation(indexes s.Set[Index], queries s.Set[Query], optimizer

// generate a graceful index name
indexResult.IndexName = gracefulIndexName(optimizer, idx.SchemaName, idx.TableName, cols)
l().Info("graceful index name", zap.String("index-name", indexResult.IndexName))
advisorLogger().Info("graceful index name", zap.String("index-name", indexResult.IndexName))

// calculate the index size
indexSize, err := optimizer.EstIndexSize(idx.SchemaName, idx.TableName, cols...)
if err != nil {
l().Info("show index stats failed", zap.Error(err))
advisorLogger().Info("show index stats failed", zap.Error(err))
return nil, err
}
indexResult.IndexSize = uint64(indexSize)
Expand All @@ -166,12 +166,12 @@ func prepareRecommendation(indexes s.Set[Index], queries s.Set[Query], optimizer
for _, query := range queries.ToList() {
costBefore, err := optimizer.QueryPlanCost(query.Text)
if err != nil {
l().Info("failed to get query plan cost", zap.Error(err))
advisorLogger().Info("failed to get query plan cost", zap.Error(err))
return nil, err
}
costAfter, err := optimizer.QueryPlanCost(query.Text, idx)
if err != nil {
l().Info("failed to get query plan cost", zap.Error(err))
advisorLogger().Info("failed to get query plan cost", zap.Error(err))
return nil, err
}
if costBefore == 0 { // avoid NaN
Expand Down
6 changes: 3 additions & 3 deletions pkg/planner/indexadvisor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func CollectIndexableColumnsFromQuery(q Query, opt Optimizer) (s.Set[Column], er
for _, schemaName := range schemaNames {
cols, err := opt.PossibleColumns(schemaName, x.Name.L)
if err != nil {
l().Warn("failed to get possible columns",
advisorLogger().Warn("failed to get possible columns",
zap.String("schema", schemaName),
zap.String("column", x.Name.L))
continue
Expand All @@ -411,7 +411,7 @@ func CollectIndexableColumnsFromQuery(q Query, opt Optimizer) (s.Set[Column], er
for _, c := range possibleColumns {
colType, err := opt.ColumnType(c)
if err != nil {
l().Warn("failed to get column type",
advisorLogger().Warn("failed to get column type",
zap.String("schema", c.SchemaName),
zap.String("table", c.TableName),
zap.String("column", c.ColumnName))
Expand Down Expand Up @@ -469,7 +469,7 @@ func isIndexableColumnType(tp *types.FieldType) bool {
return false
}

func l() *zap.Logger {
func advisorLogger() *zap.Logger {
return logutil.BgLogger().With(zap.String("component", "index_advisor"))
}

Expand Down

0 comments on commit 0fef51a

Please sign in to comment.