optimize table index

This commit is contained in:
MaysWind
2020-12-13 19:12:00 +08:00
parent c968aabbc1
commit 879051f02b
7 changed files with 34 additions and 12 deletions
+5 -1
View File
@@ -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
}
+5 -1
View File
@@ -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
}
+17 -3
View File
@@ -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
}
+2 -2
View File
@@ -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"`
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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
}
+1 -1
View File
@@ -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
}