数据库加索引

This commit is contained in:
meixiongfeng 2022-12-05 10:04:48 +08:00
parent 90a4964e11
commit 7ec319f40d
4 changed files with 23 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import (
"go_dreamfactory/utils"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
// 记录一些扩展数据
@ -22,6 +23,9 @@ func (this *ModelExpand) Init(service core.IService, module core.IModule, comp c
this.TableName = comm.TableUserExpand
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*User)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
@ -35,7 +39,7 @@ func (this *ModelExpand) GetUserExpand(uid string) (result *pb.DBUserExpand, err
} else {
if err = model.Get(uid, result); err != nil && mongo.ErrNoDocuments != err {
this.module.Error("Cross Get", log.Fields{"uid": uid})
return result,err
return result, err
}
}
} else {

View File

@ -12,6 +12,8 @@ import (
"time"
"github.com/spf13/cast"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type ModelSetting struct {
@ -23,6 +25,9 @@ func (this *ModelSetting) Init(service core.IService, module core.IModule, comp
this.TableName = comm.TableSetting
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*User)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}

View File

@ -11,6 +11,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
// 签到
@ -23,6 +24,9 @@ func (this *ModelSign) Init(service core.IService, module core.IModule, comp cor
this.TableName = comm.TableSign
err = this.MCompModel.Init(service, module, comp, options)
this.module = module.(*User)
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}

View File

@ -17,6 +17,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
type ModelUser struct {
@ -32,6 +33,14 @@ func (this *ModelUser) Init(service core.IService, module core.IModule, comp cor
this.EventApp = event_v2.NewApp()
this.EventApp.Listen(comm.EventUserChanged, this.ChangeExp)
this.EventApp.Listen(comm.EventUserChanged, this.ChangeLevel)
// 通过uid创建索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "name", Value: bsonx.Int32(1)}},
})
return
}