diff --git a/pkg/domain/BUILD.bazel b/pkg/domain/BUILD.bazel index 2fdf3a0df3..d369511566 100644 --- a/pkg/domain/BUILD.bazel +++ b/pkg/domain/BUILD.bazel @@ -84,6 +84,7 @@ go_library( "//pkg/util/globalconn", "//pkg/util/intest", "//pkg/util/logutil", + "//pkg/util/mathutil", "//pkg/util/memory", "//pkg/util/memoryusagealarm", "//pkg/util/printer", diff --git a/pkg/domain/domain.go b/pkg/domain/domain.go index 621ded193d..239bab3ff1 100644 --- a/pkg/domain/domain.go +++ b/pkg/domain/domain.go @@ -87,6 +87,7 @@ import ( "github.com/pingcap/tidb/pkg/util/globalconn" "github.com/pingcap/tidb/pkg/util/intest" "github.com/pingcap/tidb/pkg/util/logutil" + "github.com/pingcap/tidb/pkg/util/mathutil" "github.com/pingcap/tidb/pkg/util/memory" "github.com/pingcap/tidb/pkg/util/memoryusagealarm" "github.com/pingcap/tidb/pkg/util/replayer" @@ -462,20 +463,22 @@ func (do *Domain) fetchAllSchemasWithTables(m *meta.Meta) ([]*model.DBInfo, erro const fetchSchemaConcurrency = 1 func (*Domain) splitForConcurrentFetch(schemas []*model.DBInfo) [][]*model.DBInfo { - groupSize := (len(schemas) + fetchSchemaConcurrency - 1) / fetchSchemaConcurrency - if variable.SchemaCacheSize.Load() > 0 && len(schemas) > 1000 { - // TODO: Temporary solution to speed up when too many databases, will refactor it later. - groupSize = 8 - } - splitted := make([][]*model.DBInfo, 0, fetchSchemaConcurrency) + groupCnt := fetchSchemaConcurrency schemaCnt := len(schemas) - for i := 0; i < schemaCnt; i += groupSize { - end := i + groupSize - if end > schemaCnt { - end = schemaCnt - } - splitted = append(splitted, schemas[i:end]) + if variable.SchemaCacheSize.Load() > 0 && schemaCnt > 1000 { + // TODO: Temporary solution to speed up when too many databases, will refactor it later. + groupCnt = 8 } + + splitted := make([][]*model.DBInfo, 0, groupCnt) + groupSizes := mathutil.Divide2Batches(schemaCnt, groupCnt) + + start := 0 + for _, groupSize := range groupSizes { + splitted = append(splitted, schemas[start:start+groupSize]) + start += groupSize + } + return splitted }