* MM-45956: Optimize FileInfo stats query We Denormalize Post.ChannelId on FileInfo.ChannelId ```release-note The file info stats query is now optimized by denormalizing the channelID column into the table itself. This will speed up the query to get the file count for a channel on clicking the RHS. Migration times: On a MySQL 8.0.31 DB with 1405 rows in FileInfo and 11M posts, it took around 0.3s On a Postgres 12.14 DB with 1731 rows in FileInfo and 11M posts, it took around 0.27s ``` https://mattermost.atlassian.net/browse/MM-45956
35 строки
1.0 KiB
SQL
35 строки
1.0 KiB
SQL
SET @preparedStatement = (SELECT IF(
|
|
NOT EXISTS(
|
|
SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE table_name = 'FileInfo'
|
|
AND table_schema = DATABASE()
|
|
AND column_name = 'ChannelId'
|
|
),
|
|
'ALTER TABLE FileInfo ADD COLUMN ChannelId varchar(26);',
|
|
'SELECT 1;'
|
|
));
|
|
|
|
PREPARE addColumnIfNotExists FROM @preparedStatement;
|
|
EXECUTE addColumnIfNotExists;
|
|
DEALLOCATE PREPARE addColumnIfNotExists;
|
|
|
|
UPDATE FileInfo, Posts
|
|
SET FileInfo.ChannelId = Posts.ChannelId
|
|
WHERE FileInfo.PostId = Posts.Id
|
|
AND FileInfo.ChannelId IS NULL;
|
|
|
|
SET @preparedStatement = (SELECT IF(
|
|
NOT EXISTS(
|
|
SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
|
|
WHERE table_name = 'FileInfo'
|
|
AND table_schema = DATABASE()
|
|
AND index_name = 'idx_fileinfo_channel_id_create_at'
|
|
),
|
|
'CREATE INDEX idx_fileinfo_channel_id_create_at ON FileInfo(ChannelId, CreateAt);',
|
|
'SELECT 1'
|
|
));
|
|
|
|
PREPARE createIndexIfNotExists FROM @preparedStatement;
|
|
EXECUTE createIndexIfNotExists;
|
|
DEALLOCATE PREPARE createIndexIfNotExists;
|