Adding Posts table indexes for 20M rows. (#7728)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
1e2506b2df
Коммит
91b9514aaf
@@ -11,6 +11,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
"github.com/mattermost/mattermost-server/einterfaces"
|
"github.com/mattermost/mattermost-server/einterfaces"
|
||||||
"github.com/mattermost/mattermost-server/model"
|
"github.com/mattermost/mattermost-server/model"
|
||||||
@@ -73,6 +74,9 @@ func (s SqlPostStore) CreateIndexesIfNotExists() {
|
|||||||
s.CreateIndexIfNotExists("idx_posts_user_id", "Posts", "UserId")
|
s.CreateIndexIfNotExists("idx_posts_user_id", "Posts", "UserId")
|
||||||
s.CreateIndexIfNotExists("idx_posts_is_pinned", "Posts", "IsPinned")
|
s.CreateIndexIfNotExists("idx_posts_is_pinned", "Posts", "IsPinned")
|
||||||
|
|
||||||
|
s.CreateCompositeIndexIfNotExists("idx_posts_channel_id_update_at", "Posts", []string{"ChannelId", "UpdateAt"})
|
||||||
|
s.CreateCompositeIndexIfNotExists("idx_posts_channel_id_delete_at_create_at", "Posts", []string{"ChannelId", "DeleteAt", "CreateAt"})
|
||||||
|
|
||||||
s.CreateFullTextIndexIfNotExists("idx_posts_message_txt", "Posts", "Message")
|
s.CreateFullTextIndexIfNotExists("idx_posts_message_txt", "Posts", "Message")
|
||||||
s.CreateFullTextIndexIfNotExists("idx_posts_hashtags_txt", "Posts", "Hashtags")
|
s.CreateFullTextIndexIfNotExists("idx_posts_hashtags_txt", "Posts", "Hashtags")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ type SqlStore interface {
|
|||||||
AlterColumnTypeIfExists(tableName string, columnName string, mySqlColType string, postgresColType string) bool
|
AlterColumnTypeIfExists(tableName string, columnName string, mySqlColType string, postgresColType string) bool
|
||||||
CreateUniqueIndexIfNotExists(indexName string, tableName string, columnName string) bool
|
CreateUniqueIndexIfNotExists(indexName string, tableName string, columnName string) bool
|
||||||
CreateIndexIfNotExists(indexName string, tableName string, columnName string) bool
|
CreateIndexIfNotExists(indexName string, tableName string, columnName string) bool
|
||||||
|
CreateCompositeIndexIfNotExists(indexName string, tableName string, columnNames []string) bool
|
||||||
CreateFullTextIndexIfNotExists(indexName string, tableName string, columnName string) bool
|
CreateFullTextIndexIfNotExists(indexName string, tableName string, columnName string) bool
|
||||||
RemoveIndexIfExists(indexName string, tableName string) bool
|
RemoveIndexIfExists(indexName string, tableName string) bool
|
||||||
GetAllConns() []*gorp.DbMap
|
GetAllConns() []*gorp.DbMap
|
||||||
|
|||||||
@@ -547,18 +547,22 @@ func (ss *SqlSupplier) AlterColumnTypeIfExists(tableName string, columnName stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SqlSupplier) CreateUniqueIndexIfNotExists(indexName string, tableName string, columnName string) bool {
|
func (ss *SqlSupplier) CreateUniqueIndexIfNotExists(indexName string, tableName string, columnName string) bool {
|
||||||
return ss.createIndexIfNotExists(indexName, tableName, columnName, INDEX_TYPE_DEFAULT, true)
|
return ss.createIndexIfNotExists(indexName, tableName, []string{columnName}, INDEX_TYPE_DEFAULT, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SqlSupplier) CreateIndexIfNotExists(indexName string, tableName string, columnName string) bool {
|
func (ss *SqlSupplier) CreateIndexIfNotExists(indexName string, tableName string, columnName string) bool {
|
||||||
return ss.createIndexIfNotExists(indexName, tableName, columnName, INDEX_TYPE_DEFAULT, false)
|
return ss.createIndexIfNotExists(indexName, tableName, []string{columnName}, INDEX_TYPE_DEFAULT, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *SqlSupplier) CreateCompositeIndexIfNotExists(indexName string, tableName string, columnNames []string) bool {
|
||||||
|
return ss.createIndexIfNotExists(indexName, tableName, columnNames, INDEX_TYPE_DEFAULT, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SqlSupplier) CreateFullTextIndexIfNotExists(indexName string, tableName string, columnName string) bool {
|
func (ss *SqlSupplier) CreateFullTextIndexIfNotExists(indexName string, tableName string, columnName string) bool {
|
||||||
return ss.createIndexIfNotExists(indexName, tableName, columnName, INDEX_TYPE_FULL_TEXT, false)
|
return ss.createIndexIfNotExists(indexName, tableName, []string{columnName}, INDEX_TYPE_FULL_TEXT, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SqlSupplier) createIndexIfNotExists(indexName string, tableName string, columnName string, indexType string, unique bool) bool {
|
func (ss *SqlSupplier) createIndexIfNotExists(indexName string, tableName string, columnNames []string, indexType string, unique bool) bool {
|
||||||
|
|
||||||
uniqueStr := ""
|
uniqueStr := ""
|
||||||
if unique {
|
if unique {
|
||||||
@@ -574,10 +578,15 @@ func (ss *SqlSupplier) createIndexIfNotExists(indexName string, tableName string
|
|||||||
|
|
||||||
query := ""
|
query := ""
|
||||||
if indexType == INDEX_TYPE_FULL_TEXT {
|
if indexType == INDEX_TYPE_FULL_TEXT {
|
||||||
|
if len(columnNames) != 1 {
|
||||||
|
l4g.Critical("Unable to create multi column full text index")
|
||||||
|
os.Exit(EXIT_CREATE_INDEX_POSTGRES)
|
||||||
|
}
|
||||||
|
columnName := columnNames[0]
|
||||||
postgresColumnNames := convertMySQLFullTextColumnsToPostgres(columnName)
|
postgresColumnNames := convertMySQLFullTextColumnsToPostgres(columnName)
|
||||||
query = "CREATE INDEX " + indexName + " ON " + tableName + " USING gin(to_tsvector('english', " + postgresColumnNames + "))"
|
query = "CREATE INDEX " + indexName + " ON " + tableName + " USING gin(to_tsvector('english', " + postgresColumnNames + "))"
|
||||||
} else {
|
} else {
|
||||||
query = "CREATE " + uniqueStr + "INDEX " + indexName + " ON " + tableName + " (" + columnName + ")"
|
query = "CREATE " + uniqueStr + "INDEX " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ss.GetMaster().ExecNoTimeout(query)
|
_, err := ss.GetMaster().ExecNoTimeout(query)
|
||||||
@@ -605,7 +614,7 @@ func (ss *SqlSupplier) createIndexIfNotExists(indexName string, tableName string
|
|||||||
fullTextIndex = " FULLTEXT "
|
fullTextIndex = " FULLTEXT "
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = ss.GetMaster().ExecNoTimeout("CREATE " + uniqueStr + fullTextIndex + " INDEX " + indexName + " ON " + tableName + " (" + columnName + ")")
|
_, err = ss.GetMaster().ExecNoTimeout("CREATE " + uniqueStr + fullTextIndex + " INDEX " + indexName + " ON " + tableName + " (" + strings.Join(columnNames, ", ") + ")")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l4g.Critical(utils.T("store.sql.create_index.critical"), err)
|
l4g.Critical(utils.T("store.sql.create_index.critical"), err)
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user