From 879051f02bbe224ff45646cafd53939bcf17ffb5 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 13 Dec 2020 19:12:00 +0800 Subject: [PATCH] optimize table index --- pkg/api/tokens.go | 6 +++++- pkg/api/transaction_tags.go | 6 +++++- pkg/models/token_record.go | 20 +++++++++++++++++--- pkg/models/transaction.go | 4 ++-- pkg/models/transaction_tag.go | 4 ++-- pkg/services/tokens.go | 4 ++-- pkg/services/transaction_tags.go | 2 +- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/pkg/api/tokens.go b/pkg/api/tokens.go index dbb3c0fe..3757a7cd 100644 --- a/pkg/api/tokens.go +++ b/pkg/api/tokens.go @@ -1,6 +1,8 @@ package api import ( + "sort" + "github.com/mayswind/lab/pkg/core" "github.com/mayswind/lab/pkg/errs" "github.com/mayswind/lab/pkg/log" @@ -30,7 +32,7 @@ func (a *TokensApi) TokenListHandler(c *core.Context) (interface{}, *errs.Error) return nil, errs.ErrOperationFailed } - tokenResps := make([]*models.TokenInfoResponse, len(tokens)) + tokenResps := make(models.TokenInfoResponseSlice, len(tokens)) claims := c.GetTokenClaims() for i := 0; i < len(tokens); i++ { @@ -50,6 +52,8 @@ func (a *TokensApi) TokenListHandler(c *core.Context) (interface{}, *errs.Error) tokenResps[i] = tokenResp } + sort.Sort(tokenResps) + return tokenResps, nil } diff --git a/pkg/api/transaction_tags.go b/pkg/api/transaction_tags.go index 09ef450b..606aab29 100644 --- a/pkg/api/transaction_tags.go +++ b/pkg/api/transaction_tags.go @@ -1,6 +1,8 @@ package api import ( + "sort" + "github.com/mayswind/lab/pkg/core" "github.com/mayswind/lab/pkg/errs" "github.com/mayswind/lab/pkg/log" @@ -27,12 +29,14 @@ func (a *TransactionTagsApi) TagListHandler(c *core.Context) (interface{}, *errs return nil, errs.ErrOperationFailed } - tagResps := make([]*models.TransactionTagInfoResponse, len(tags)) + tagResps := make(models.TransactionTagInfoResponseSlice, len(tags)) for i := 0; i < len(tags); i++ { tagResps[i] = tags[i].ToTransactionTagInfoResponse() } + sort.Sort(tagResps) + return tagResps, nil } diff --git a/pkg/models/token_record.go b/pkg/models/token_record.go index 57a91044..3e48d4c2 100644 --- a/pkg/models/token_record.go +++ b/pkg/models/token_record.go @@ -5,13 +5,13 @@ import "github.com/mayswind/lab/pkg/core" const TOKEN_USER_AGENT_MAX_LENGTH = 255 type TokenRecord struct { - Uid int64 `xorm:"PK"` + Uid int64 `xorm:"PK INDEX(IDX_token_record_uid_type_expired_time)"` UserTokenId int64 `xorm:"PK"` - TokenType core.TokenType `xorm:"TINYINT NOT NULL"` + TokenType core.TokenType `xorm:"INDEX(IDX_token_record_uid_type_expired_time) TINYINT NOT NULL"` Secret string `xorm:"VARCHAR(10) NOT NULL"` UserAgent string `xorm:"VARCHAR(255)"` CreatedUnixTime int64 `xorm:"PK"` - ExpiredUnixTime int64 + ExpiredUnixTime int64 `xorm:"INDEX(IDX_token_record_uid_type_expired_time)"` } type TokenRevokeRequest struct { @@ -32,3 +32,17 @@ type TokenInfoResponse struct { ExpiredAt int64 `json:"expiredAt"` IsCurrent bool `json:"isCurrent"` } + +type TokenInfoResponseSlice []*TokenInfoResponse + +func (a TokenInfoResponseSlice) Len() int { + return len(a) +} + +func (a TokenInfoResponseSlice) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} + +func (a TokenInfoResponseSlice) Less(i, j int) bool { + return a[i].ExpiredAt > a[j].ExpiredAt +} diff --git a/pkg/models/transaction.go b/pkg/models/transaction.go index 6072d49e..1ac71e8d 100644 --- a/pkg/models/transaction.go +++ b/pkg/models/transaction.go @@ -11,11 +11,11 @@ const ( type Transaction struct { TransactionId int64 `xorm:"PK"` - Uid int64 `xorm:"UNIQUE(IDX_transaction_uid_transaction_time) INDEX(IDX_transaction_uid_deleted_transaction_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"` + Uid int64 `xorm:"UNIQUE(UQE_transaction_uid_transaction_time) INDEX(IDX_transaction_uid_deleted_transaction_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"` Deleted bool `xorm:"INDEX(IDX_transaction_uid_deleted_transaction_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"` Type TransactionType `xorm:"INDEX(IDX_transaction_uid_deleted_type_time) NOT NULL"` CategoryId int64 `xorm:"INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"` - TransactionTime int64 `xorm:"UNIQUE(IDX_transaction_uid_transaction_time) INDEX(IDX_transaction_uid_deleted_transaction_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"` + TransactionTime int64 `xorm:"UNIQUE(UQE_transaction_uid_transaction_time) INDEX(IDX_transaction_uid_deleted_transaction_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"` SourceAccountId int64 `xorm:"NOT NULL"` DestinationAccountId int64 `xorm:"NOT NULL"` SourceAmount int64 `xorm:"NOT NULL"` diff --git a/pkg/models/transaction_tag.go b/pkg/models/transaction_tag.go index 2f44b7d4..4575d5ca 100644 --- a/pkg/models/transaction_tag.go +++ b/pkg/models/transaction_tag.go @@ -2,8 +2,8 @@ package models type TransactionTag struct { TagId int64 `xorm:"PK"` - Uid int64 `xorm:"UNIQUE(IDX_tag_uid_name) NOT NULL"` - Name string `xorm:"UNIQUE(IDX_tag_uid_name) VARCHAR(32) NOT NULL"` + Uid int64 `xorm:"UNIQUE(UQE_tag_uid_name) NOT NULL"` + Name string `xorm:"UNIQUE(UQE_tag_uid_name) VARCHAR(32) NOT NULL"` DisplayOrder int `xorm:"NOT NULL"` Hidden bool `xorm:"NOT NULL"` CreatedUnixTime int64 diff --git a/pkg/services/tokens.go b/pkg/services/tokens.go index 693be9b5..fa9338f2 100644 --- a/pkg/services/tokens.go +++ b/pkg/services/tokens.go @@ -41,7 +41,7 @@ func (s *TokenService) GetAllTokensByUid(uid int64) ([]*models.TokenRecord, erro } var tokenRecords []*models.TokenRecord - err := s.TokenDB(uid).Cols("uid", "user_token_id", "token_type", "user_agent", "created_unix_time", "expired_unix_time").Where("uid=?", uid).OrderBy("created_unix_time desc").Find(&tokenRecords) + err := s.TokenDB(uid).Cols("uid", "user_token_id", "token_type", "user_agent", "created_unix_time", "expired_unix_time").Where("uid=?", uid).Find(&tokenRecords) return tokenRecords, err } @@ -54,7 +54,7 @@ func (s *TokenService) GetAllUnexpiredMormalTokensByUid(uid int64) ([]*models.To now := time.Now().Unix() var tokenRecords []*models.TokenRecord - err := s.TokenDB(uid).Cols("uid", "user_token_id", "token_type", "user_agent", "created_unix_time", "expired_unix_time").Where("uid=? AND token_type=? AND expired_unix_time>?", uid, core.USER_TOKEN_TYPE_NORMAL, now).OrderBy("created_unix_time desc").Find(&tokenRecords) + err := s.TokenDB(uid).Cols("uid", "user_token_id", "token_type", "user_agent", "created_unix_time", "expired_unix_time").Where("uid=? AND token_type=? AND expired_unix_time>?", uid, core.USER_TOKEN_TYPE_NORMAL, now).Find(&tokenRecords) return tokenRecords, err } diff --git a/pkg/services/transaction_tags.go b/pkg/services/transaction_tags.go index 091d1931..9ce58330 100644 --- a/pkg/services/transaction_tags.go +++ b/pkg/services/transaction_tags.go @@ -33,7 +33,7 @@ func (s *TransactionTagService) GetAllTagsByUid(uid int64) ([]*models.Transactio } var tags []*models.TransactionTag - err := s.UserDataDB(uid).Where("uid=?", uid).OrderBy("display_order asc").Find(&tags) + err := s.UserDataDB(uid).Where("uid=?", uid).Find(&tags) return tags, err }