From 35a0c6fa14a7ef66f82ace94232760cd157a375b Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 25 Jul 2021 18:54:24 +0200 Subject: [PATCH] Improve performance of retrieving chat history by avoiding double-sort and outer join (#1261) --- core/chat/persistence.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/chat/persistence.go b/core/chat/persistence.go index d5a5562fe..03e3cb6da 100644 --- a/core/chat/persistence.go +++ b/core/chat/persistence.go @@ -156,8 +156,15 @@ func GetChatModerationHistory() []events.UserMessageEvent { func GetChatHistory() []events.UserMessageEvent { // Get all visible messages - var query = fmt.Sprintf("SELECT id, user_id, body, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at FROM (SELECT * FROM messages LEFT OUTER JOIN users ON messages.user_id = users.id WHERE hidden_at IS NULL AND disabled_at IS NULL ORDER BY timestamp DESC LIMIT %d) ORDER BY timestamp asc", maxBacklogNumber) - return getChat(query) + var query = fmt.Sprintf("SELECT messages.id, user_id, body, eventType, hidden_at, timestamp, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at FROM messages, users WHERE messages.user_id = users.id AND hidden_at IS NULL AND disabled_at IS NULL ORDER BY timestamp DESC LIMIT %d", maxBacklogNumber) + m := getChat(query) + + // Invert order of messages + for i, j := 0, len(m)-1; i < j; i, j = i+1, j-1 { + m[i], m[j] = m[j], m[i] + } + + return m } // SetMessageVisibilityForUserId will bulk change the visibility of messages for a user