MM-45956: Optimize FileInfo stats query (#22603)

* 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
Этот коммит содержится в:
Agniva De Sarker
2023-03-23 22:14:04 +05:30
коммит произвёл GitHub
родитель 1edbde8aa3
Коммит 56b18ca7bf
25 изменённых файлов: 266 добавлений и 138 удалений

Просмотреть файл

@@ -210,6 +210,8 @@ channels/db/migrations/mysql/000104_upgrade_notifyadmin.down.sql
channels/db/migrations/mysql/000104_upgrade_notifyadmin.up.sql
channels/db/migrations/mysql/000105_remove_tokens.down.sql
channels/db/migrations/mysql/000105_remove_tokens.up.sql
channels/db/migrations/mysql/000106_fileinfo_channelid.down.sql
channels/db/migrations/mysql/000106_fileinfo_channelid.up.sql
channels/db/migrations/postgres/000001_create_teams.down.sql
channels/db/migrations/postgres/000001_create_teams.up.sql
channels/db/migrations/postgres/000002_create_team_members.down.sql
@@ -420,3 +422,5 @@ channels/db/migrations/postgres/000104_upgrade_notifyadmin.down.sql
channels/db/migrations/postgres/000104_upgrade_notifyadmin.up.sql
channels/db/migrations/postgres/000105_remove_tokens.down.sql
channels/db/migrations/postgres/000105_remove_tokens.up.sql
channels/db/migrations/postgres/000106_fileinfo_channelid.down.sql
channels/db/migrations/postgres/000106_fileinfo_channelid.up.sql

Просмотреть файл

@@ -0,0 +1,29 @@
SET @preparedStatement = (SELECT IF(
EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_name = 'FileInfo'
AND table_schema = DATABASE()
AND index_name = 'idx_fileinfo_channel_id_create_at'
) > 0,
'DROP INDEX idx_fileinfo_channel_id_create_at ON FileInfo;',
'SELECT 1'
));
PREPARE removeIndexIfExists FROM @preparedStatement;
EXECUTE removeIndexIfExists;
DEALLOCATE PREPARE removeIndexIfExists;
SET @preparedStatement = (SELECT IF(
EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_name = 'FileInfo'
AND table_schema = DATABASE()
AND column_name = 'ChannelId'
) > 0,
'ALTER TABLE FileInfo DROP COLUMN ChannelId;',
'SELECT 1;'
));
PREPARE removeColumnIfExists FROM @preparedStatement;
EXECUTE removeColumnIfExists;
DEALLOCATE PREPARE removeColumnIfExists;

Просмотреть файл

@@ -0,0 +1,34 @@
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;

Просмотреть файл

@@ -0,0 +1,2 @@
DROP INDEX IF EXISTS idx_fileinfo_channel_id_create_at;
ALTER TABLE fileinfo DROP COLUMN IF EXISTS channelid;

Просмотреть файл

@@ -0,0 +1,3 @@
ALTER TABLE fileinfo ADD COLUMN IF NOT EXISTS channelid varchar(26);
UPDATE fileinfo SET channelid = posts.channelid FROM posts WHERE fileinfo.channelid IS NULL AND fileinfo.postid = posts.id;
CREATE INDEX IF NOT EXISTS idx_fileinfo_channel_id_create_at ON fileinfo(channelid, createat);