Optimize searching for users on Postgres (#9782)

The searches on Postgres using LIKE with prefix matching will only
use an index if it has the text_pattern_ops flag (forcing C collation).
This drops the old indexes and adds properly optimized ones.
This commit is contained in:
Daniel Fiori
2018-11-09 15:34:31 -05:00
committed by Jesse Hallam
parent 0c5f60f89b
commit 7a758eae72
2 changed files with 13 additions and 5 deletions

View File

@@ -521,6 +521,14 @@ func UpgradeDatabaseToVersion56(sqlStore SqlStore) {
// migrating user's accepted terms of service data into the new table
sqlStore.GetMaster().Exec("INSERT INTO UserTermsOfService SELECT Id, AcceptedTermsOfServiceId as TermsOfServiceId, :CreateAt FROM Users WHERE AcceptedTermsOfServiceId != \"\" AND AcceptedTermsOfServiceId IS NOT NULL", map[string]interface{}{"CreateAt": model.GetMillis()})
if sqlStore.DriverName() == model.DATABASE_DRIVER_POSTGRES {
sqlStore.RemoveIndexIfExists("idx_users_email_lower", "lower(Email)")
sqlStore.RemoveIndexIfExists("idx_users_username_lower", "lower(Username)")
sqlStore.RemoveIndexIfExists("idx_users_nickname_lower", "lower(Nickname)")
sqlStore.RemoveIndexIfExists("idx_users_firstname_lower", "lower(FirstName)")
sqlStore.RemoveIndexIfExists("idx_users_lastname_lower", "lower(LastName)")
}
//saveSchemaVersion(sqlStore, VERSION_5_6_0)
//}
}

View File

@@ -94,11 +94,11 @@ func (us SqlUserStore) CreateIndexesIfNotExists() {
us.CreateIndexIfNotExists("idx_users_delete_at", "Users", "DeleteAt")
if us.DriverName() == model.DATABASE_DRIVER_POSTGRES {
us.CreateIndexIfNotExists("idx_users_email_lower", "Users", "lower(Email)")
us.CreateIndexIfNotExists("idx_users_username_lower", "Users", "lower(Username)")
us.CreateIndexIfNotExists("idx_users_nickname_lower", "Users", "lower(Nickname)")
us.CreateIndexIfNotExists("idx_users_firstname_lower", "Users", "lower(FirstName)")
us.CreateIndexIfNotExists("idx_users_lastname_lower", "Users", "lower(LastName)")
us.CreateIndexIfNotExists("idx_users_email_lower_textpattern", "Users", "lower(Email) text_pattern_ops")
us.CreateIndexIfNotExists("idx_users_username_lower_textpattern", "Users", "lower(Username) text_pattern_ops")
us.CreateIndexIfNotExists("idx_users_nickname_lower_textpattern", "Users", "lower(Nickname) text_pattern_ops")
us.CreateIndexIfNotExists("idx_users_firstname_lower_textpattern", "Users", "lower(FirstName) text_pattern_ops")
us.CreateIndexIfNotExists("idx_users_lastname_lower_textpattern", "Users", "lower(LastName) text_pattern_ops")
}
us.CreateFullTextIndexIfNotExists("idx_users_all_txt", "Users", strings.Join(USER_SEARCH_TYPE_ALL, ", "))