Skip to content

Commit

Permalink
Merge pull request #875 from traPtitech/impr-performance
Browse files Browse the repository at this point in the history
improve performance #874
  • Loading branch information
wtks authored May 1, 2020
2 parents 63e48c9 + c17f816 commit aa80727
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
3 changes: 3 additions & 0 deletions migration/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Migrations() []*gormigrate.Migration {
v15(), // 外部ログイン機能追加
v16(), // パーミッション修正
v17(), // ユーザーホームチャンネル
v18(), // インデックス追加
}
}

Expand Down Expand Up @@ -140,6 +141,8 @@ func AllCompositeIndexes() [][]string {
{"idx_files_channel_id_created_at", "files", "channel_id", "created_at"},
{"idx_files_creator_id_created_at", "files", "creator_id", "created_at"},
{"idx_messages_stamps_user_id_stamp_id_updated_at", "messages_stamps", "user_id", "stamp_id", "updated_at"},
{"idx_channel_channels_id_is_public_is_forced", "channels", "id", "is_public", "is_forced"},
{"idx_messages_deleted_at_created_at", "messages", "deleted_at", "created_at"},
}
}

Expand Down
26 changes: 26 additions & 0 deletions migration/v18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package migration

import (
"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)

// v18 インデックス追加
func v18() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "18",
Migrate: func(db *gorm.DB) error {
// 複合インデックス
indexes := [][]string{
{"idx_channels_channels_id_is_public_is_forced", "channels", "id", "is_public", "is_forced"},
{"idx_messages_deleted_at_created_at", "messages", "deleted_at", "created_at"},
}
for _, c := range indexes {
if err := db.Table(c[1]).AddIndex(c[0], c[2:]...).Error; err != nil {
return err
}
}
return nil
},
}
}
22 changes: 18 additions & 4 deletions repository/message_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ func (repo *GormRepository) GetMessages(query MessagesQuery) (messages []*model.
tx = tx.Order("messages.created_at DESC")
}

if query.Offset > 0 {
tx = tx.Offset(query.Offset)
}

if query.ExcludeDMs && query.Channel == uuid.Nil && query.User == uuid.Nil && query.ChannelsSubscribedByUser == uuid.Nil && !query.Since.Valid && !query.Until.Valid && query.Limit > 0 {
// アクティビティ用にUSE INDEX指定でクエリ発行
// TODO 綺麗じゃない
err = tx.
Limit(query.Limit + 1).
Raw("SELECT messages.* FROM messages USE INDEX (idx_messages_deleted_at_created_at) INNER JOIN channels ON messages.channel_id = channels.id WHERE messages.deleted_at IS NULL AND channels.is_public = true").
Scan(&messages).
Error
if len(messages) > query.Limit {
return messages[:len(messages)-1], true, err
}
return messages, false, err
}

if query.ChannelsSubscribedByUser != uuid.Nil || query.ExcludeDMs {
tx = tx.Joins("INNER JOIN channels ON messages.channel_id = channels.id")
}
Expand Down Expand Up @@ -224,10 +242,6 @@ func (repo *GormRepository) GetMessages(query MessagesQuery) (messages []*model.
tx = tx.Where("channels.is_public = true")
}

if query.Offset > 0 {
tx = tx.Offset(query.Offset)
}

if query.Limit > 0 {
err = tx.Limit(query.Limit + 1).Find(&messages).Error
if len(messages) > query.Limit {
Expand Down
1 change: 1 addition & 0 deletions router/v3/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (h *Handlers) PostFile(c echo.Context) error {

// GetFileMeta GET /files/:fileID/meta
func (h *Handlers) GetFileMeta(c echo.Context) error {
c.Response().Header().Set(consts.HeaderCacheControl, "private, max-age=86400") // 1日キャッシュ
return c.JSON(http.StatusOK, formatFileInfo(getParamFile(c)))
}

Expand Down

0 comments on commit aa80727

Please sign in to comment.