Skip to content

Commit fe9f690

Browse files
authored
infoschema: add btree index for reference foreign key (#59422)
close #61126
1 parent c85f333 commit fe9f690

File tree

8 files changed

+392
-75
lines changed

8 files changed

+392
-75
lines changed

pkg/infoschema/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ go_test(
9595
],
9696
embed = [":infoschema"],
9797
flaky = True,
98-
shard_count = 26,
98+
shard_count = 28,
9999
deps = [
100100
"//pkg/ddl/placement",
101101
"//pkg/domain",

pkg/infoschema/builder.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,6 @@ func applyCreateTable(b *Builder, m meta.Reader, dbInfo *model.DBInfo, tableID i
753753
return nil, errors.Trace(err)
754754
}
755755

756-
b.infoSchema.addReferredForeignKeys(dbInfo.Name, tblInfo)
757-
758756
if !b.enableV2 {
759757
tableNames := b.infoSchema.schemaMap[dbInfo.Name.L]
760758
tableNames.tables[tblInfo.Name.L] = tbl
@@ -958,7 +956,7 @@ func (b *Builder) InitWithDBInfos(dbInfos []*model.DBInfo, policies []*model.Pol
958956
}
959957

960958
// initMisc depends on the tables and schemas, so it should be called after createSchemaTablesForDB
961-
b.initMisc(dbInfos, policies, resourceGroups)
959+
b.initMisc(policies, resourceGroups)
962960

963961
err := b.initVirtualTables(schemaVersion)
964962
if err != nil {
@@ -1048,6 +1046,7 @@ func (b *Builder) addDB(schemaVersion int64, di *model.DBInfo, schTbls *schemaTa
10481046

10491047
func (b *Builder) addTable(schemaVersion int64, di *model.DBInfo, tblInfo *model.TableInfo, tbl table.Table) {
10501048
if b.enableV2 {
1049+
b.infoData.addReferredForeignKeys(di.Name, tblInfo, schemaVersion)
10511050
b.infoData.add(tableItem{
10521051
dbName: di.Name,
10531052
dbID: di.ID,
@@ -1058,6 +1057,8 @@ func (b *Builder) addTable(schemaVersion int64, di *model.DBInfo, tblInfo *model
10581057
} else {
10591058
sortedTbls := b.infoSchema.sortedTablesBuckets[tableBucketIdx(tblInfo.ID)]
10601059
b.infoSchema.sortedTablesBuckets[tableBucketIdx(tblInfo.ID)] = append(sortedTbls, tbl)
1060+
// Maintain foreign key reference information.
1061+
b.infoSchema.addReferredForeignKeys(di.Name, tblInfo)
10611062
}
10621063
}
10631064

pkg/infoschema/builder_misc.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"fmt"
1919

2020
"github.com/pingcap/errors"
21-
infoschemacontext "github.com/pingcap/tidb/pkg/infoschema/context"
2221
"github.com/pingcap/tidb/pkg/meta"
2322
"github.com/pingcap/tidb/pkg/meta/model"
2423
)
@@ -102,7 +101,7 @@ func (b *Builder) addTemporaryTable(tblID int64) {
102101
b.infoSchema.temporaryTableIDs[tblID] = struct{}{}
103102
}
104103

105-
func (b *Builder) initMisc(dbInfos []*model.DBInfo, policies []*model.PolicyInfo, resourceGroups []*model.ResourceGroupInfo) {
104+
func (b *Builder) initMisc(policies []*model.PolicyInfo, resourceGroups []*model.ResourceGroupInfo) {
106105
info := b.infoSchema
107106
// build the policies.
108107
for _, policy := range policies {
@@ -113,20 +112,4 @@ func (b *Builder) initMisc(dbInfos []*model.DBInfo, policies []*model.PolicyInfo
113112
for _, group := range resourceGroups {
114113
info.setResourceGroup(group)
115114
}
116-
117-
// Maintain foreign key reference information.
118-
if b.enableV2 {
119-
rs := b.ListTablesWithSpecialAttribute(infoschemacontext.ForeignKeysAttribute)
120-
for _, db := range rs {
121-
for _, tbl := range db.TableInfos {
122-
info.addReferredForeignKeys(db.DBName, tbl)
123-
}
124-
}
125-
return
126-
}
127-
for _, di := range dbInfos {
128-
for _, t := range di.Deprecated.Tables {
129-
b.infoSchema.addReferredForeignKeys(di.Name, t)
130-
}
131-
}
132115
}

pkg/infoschema/context/infoschema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ type MetaOnlyInfoSchema interface {
111111
AllSchemaNames() []ast.CIStr
112112
SchemaSimpleTableInfos(ctx stdctx.Context, schema ast.CIStr) ([]*model.TableNameInfo, error)
113113
ListTablesWithSpecialAttribute(filter SpecialAttributeFilter) []TableInfoResult
114+
// GetTableReferredForeignKeys gets the table's ReferredFKInfo by lowercase schema and table name.
115+
GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo
114116
Misc
115117
}
116118

@@ -138,8 +140,6 @@ type Misc interface {
138140
CloneResourceGroups() map[string]*model.ResourceGroupInfo
139141
// HasTemporaryTable returns whether information schema has temporary table
140142
HasTemporaryTable() bool
141-
// GetTableReferredForeignKeys gets the table's ReferredFKInfo by lowercase schema and table name.
142-
GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo
143143
}
144144

145145
// DBInfoAsInfoSchema is used mainly in test.

pkg/infoschema/infoschema.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ type infoSchema struct {
6767

6868
// sortedTablesBuckets is a slice of sortedTables, a table's bucket index is (tableID % bucketCount).
6969
sortedTablesBuckets []sortedTables
70+
71+
// referredForeignKeyMap records all table's ReferredFKInfo.
72+
// referredSchemaAndTableName => child SchemaAndTableAndForeignKeyName => *model.ReferredFKInfo
73+
referredForeignKeyMap map[SchemaAndTableName][]*model.ReferredFKInfo
7074
}
7175

7276
type infoSchemaMisc struct {
@@ -86,10 +90,6 @@ type infoSchemaMisc struct {
8690

8791
// temporaryTables stores the temporary table ids
8892
temporaryTableIDs map[int64]struct{}
89-
90-
// referredForeignKeyMap records all table's ReferredFKInfo.
91-
// referredSchemaAndTableName => child SchemaAndTableAndForeignKeyName => *model.ReferredFKInfo
92-
referredForeignKeyMap map[SchemaAndTableName][]*model.ReferredFKInfo
9393
}
9494

9595
// SchemaAndTableName contains the lower-case schema name and table name.
@@ -199,14 +199,14 @@ func (is *infoSchema) base() *infoSchema {
199199
func newInfoSchema() *infoSchema {
200200
return &infoSchema{
201201
infoSchemaMisc: infoSchemaMisc{
202-
policyMap: map[string]*model.PolicyInfo{},
203-
resourceGroupMap: map[string]*model.ResourceGroupInfo{},
204-
ruleBundleMap: map[int64]*placement.Bundle{},
205-
referredForeignKeyMap: make(map[SchemaAndTableName][]*model.ReferredFKInfo),
202+
policyMap: map[string]*model.PolicyInfo{},
203+
resourceGroupMap: map[string]*model.ResourceGroupInfo{},
204+
ruleBundleMap: map[int64]*placement.Bundle{},
206205
},
207-
schemaMap: map[string]*schemaTables{},
208-
schemaID2Name: map[int64]string{},
209-
sortedTablesBuckets: make([]sortedTables, bucketCount),
206+
schemaMap: map[string]*schemaTables{},
207+
schemaID2Name: map[int64]string{},
208+
sortedTablesBuckets: make([]sortedTables, bucketCount),
209+
referredForeignKeyMap: make(map[SchemaAndTableName][]*model.ReferredFKInfo),
210210
}
211211
}
212212

@@ -625,7 +625,7 @@ func (is *infoSchemaMisc) deletePolicy(name string) {
625625
delete(is.policyMap, name)
626626
}
627627

628-
func (is *infoSchemaMisc) addReferredForeignKeys(schema ast.CIStr, tbInfo *model.TableInfo) {
628+
func (is *infoSchema) addReferredForeignKeys(schema ast.CIStr, tbInfo *model.TableInfo) {
629629
for _, fk := range tbInfo.ForeignKeys {
630630
if fk.Version < model.FKVersion1 {
631631
continue
@@ -665,7 +665,7 @@ func (is *infoSchemaMisc) addReferredForeignKeys(schema ast.CIStr, tbInfo *model
665665
}
666666
}
667667

668-
func (is *infoSchemaMisc) deleteReferredForeignKeys(schema ast.CIStr, tbInfo *model.TableInfo) {
668+
func (is *infoSchema) deleteReferredForeignKeys(schema ast.CIStr, tbInfo *model.TableInfo) {
669669
for _, fk := range tbInfo.ForeignKeys {
670670
if fk.Version < model.FKVersion1 {
671671
continue
@@ -687,7 +687,7 @@ func (is *infoSchemaMisc) deleteReferredForeignKeys(schema ast.CIStr, tbInfo *mo
687687
}
688688

689689
// GetTableReferredForeignKeys gets the table's ReferredFKInfo by lowercase schema and table name.
690-
func (is *infoSchemaMisc) GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo {
690+
func (is *infoSchema) GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo {
691691
name := SchemaAndTableName{schema: schema, table: table}
692692
return is.referredForeignKeyMap[name]
693693
}

0 commit comments

Comments
 (0)