discourse/plugins/chat/db/migrate
Alan Guo Xiang Tan 80d289eb8b
PERF: Add index for chat unread counts query (#19516)
This commit adds an index for the query which the chat plugin executes
multiple times when preloading user data in `Chat::ChatChannelFetcher.unread_counts`.

Sample query plan from a query I grabbed from one of our production
instance.

Before:

```
                                                                                        QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 GroupAggregate  (cost=10.77..696.67 rows=7 width=16) (actual time=7.735..7.736 rows=0 loops=1)
   Group Key: cc.id
   ->  Nested Loop  (cost=10.77..696.54 rows=12 width=8) (actual time=7.734..7.735 rows=0 loops=1)
         Join Filter: (cc.id = cm.chat_channel_id)
         ->  Nested Loop  (cost=0.56..76.44 rows=1 width=16) (actual time=0.011..0.037 rows=7 loops=1)
               ->  Index Only Scan using chat_channels_pkey on chat_channels cc  (cost=0.28..22.08 rows=7 width=8) (actual time=0.004..0.014 rows=7 loops=1)
                     Index Cond: (id = ANY ('{192,300,228,727,8,612,1633}'::bigint[]))
                     Heap Fetches: 0
               ->  Index Scan using user_chat_channel_unique_memberships on user_chat_channel_memberships uccm  (cost=0.28..7.73 rows=1 width=8) (actual time=0.003..0.003 rows=1 loops=7)
                     Index Cond: ((user_id = 1338) AND (chat_channel_id = cc.id))
         ->  Bitmap Heap Scan on chat_messages cm  (cost=10.21..618.98 rows=89 width=12) (actual time=1.096..1.097 rows=0 loops=7)
               Recheck Cond: (chat_channel_id = uccm.chat_channel_id)
               Filter: ((deleted_at IS NULL) AND (user_id <> 1338) AND (id > COALESCE(uccm.last_read_message_id, 0)))
               Rows Removed by Filter: 2085
               Heap Blocks: exact=7106
               ->  Bitmap Index Scan on index_chat_messages_on_chat_channel_id_and_created_at  (cost=0.00..10.19 rows=270 width=0) (actual time=0.114..0.114 rows=2085 loops=7)
                     Index Cond: (chat_channel_id = uccm.chat_channel_id)
 Planning Time: 0.408 ms
 Execution Time: 7.762 ms
(19 rows)
```

After:

```
                                                                                        QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 GroupAggregate  (cost=5.84..367.39 rows=7 width=16) (actual time=0.130..0.131 rows=0 loops=1)
   Group Key: cc.id
   ->  Nested Loop  (cost=5.84..367.26 rows=12 width=8) (actual time=0.129..0.130 rows=0 loops=1)
         Join Filter: (cc.id = cm.chat_channel_id)
         ->  Nested Loop  (cost=0.56..76.44 rows=1 width=16) (actual time=0.038..0.069 rows=7 loops=1)
               ->  Index Only Scan using chat_channels_pkey on chat_channels cc  (cost=0.28..22.08 rows=7 width=8) (actual time=0.011..0.022 rows=7 loops=1)
                     Index Cond: (id = ANY ('{192,300,228,727,8,612,1633}'::bigint[]))
                     Heap Fetches: 0
               ->  Index Scan using user_chat_channel_unique_memberships on user_chat_channel_memberships uccm  (cost=0.28..7.73 rows=1 width=8) (actual time=0.006..0.006 rows=1 loops=7)
                     Index Cond: ((user_id = 1338) AND (chat_channel_id = cc.id))
         ->  Bitmap Heap Scan on chat_messages cm  (cost=5.28..289.71 rows=89 width=12) (actual time=0.008..0.008 rows=0 loops=7)
               Recheck Cond: ((chat_channel_id = uccm.chat_channel_id) AND (id > COALESCE(uccm.last_read_message_id, 0)) AND (deleted_at IS NULL))
               Filter: (user_id <> 1338)
               ->  Bitmap Index Scan on index_chat_messages_on_chat_channel_id_and_id  (cost=0.00..5.26 rows=90 width=0) (actual time=0.008..0.008 rows=0 loops=7)
                     Index Cond: ((chat_channel_id = uccm.chat_channel_id) AND (id > COALESCE(uccm.last_read_message_id, 0)))
 Planning Time: 1.217 ms
 Execution Time: 0.188 ms
(17 rows)
```
2022-12-20 05:10:53 +08:00
..
20210225230057_create_chat_tables.rb
20210403025854_add_action_code_to_topic_chat_message.rb
20210706214013_rename_topic_chats_to_chat_channels.rb
20210729134042_create_chat_message_revisions.rb
20210730134847_create_user_chat_channel_last_read.rb
20210812145801_create_direct_message_tables.rb
20210813141741_add_timestamps_to_chat_channels.rb
20210819202912_create_incoming_chat_webhooks.rb
20210823160357_create_chat_webhook_events.rb
20210901130308_create_user_chat_channel_membership.rb
20210930144333_add_chat_enabled_to_user_options.rb
20211022151713_create_chat_message_post_connections.rb
20211029145508_add_chat_isolated_to_user_options.rb
20211104141254_add_only_chat_push_notifications_to_user_options.rb
20211119142000_add_cooked_to_chat_messages.rb
20211129171229_create_chat_uploads.rb
20211201171813_create_chat_reactions.rb
20211210191830_create_chat_mentions.rb
20211213150607_add_chat_sound_to_user_options.rb
20211217221026_add_name_to_chat_channel.rb
20211222153716_add_description_to_chat_channels.rb
20220104051326_change_chat_channels_timestamp_columns_to_timestamp_type.rb DEV: Promote historic post_deploy migrations (#19496) 2022-12-19 20:45:37 +02:00
20220119170535_add_chat_retention_fields_to_user_options.rb
20220203204002_create_chat_drafts_table.rb
20220203204003_migrate_drafts_to_chat_drafts.rb
20220218023859_add_status_to_chat_channel.rb
20220228051724_create_chat_channel_archive_table.rb
20220308165620_add_user_count_to_chat_channel.rb
20220309174820_add_last_message_created_at_to_chat_channels.rb
20220324062937_ignore_channel_wide_mention_to_user_options.rb
20220328142120_create_user_chat_message_statuses.rb
20220518140004_track_last_unread_mention_when_emailed.rb
20220629190633_auto_join_users_to_channels.rb
20220706114835_add_join_mode_to_channel_memberships.rb
20220729032237_add_index_to_chat_message_created_at.rb
20220802014549_disable_chat_uploads_if_secure_media_enabled.rb
20220901034107_add_user_count_stale_to_channel.rb
20221005143622_add_type_to_chat_channel.rb
20221014005208_add_slug_column_to_chat_channel.rb
20221101061319_add_last_editor_id_to_chat_messages.rb
20221107034541_make_chat_editor_ids_not_null.rb FIX: Migrate values before adding a not null constraint. (#18940) 2022-11-08 16:11:03 -03:00
20221118104708_add_allow_channel_wide_mentions_to_chat_channels.rb FEATURE: channels can allow/disallow @all/@here mentions (#19317) 2022-12-05 17:03:51 +01:00
20221122070108_save_chat_allowed_groups_site_setting.rb DEV: Fix chat_allowed_groups migration (#19163) 2022-11-23 15:28:12 +10:00
20221201024458_make_channel_slugs_unique_with_index.rb FIX: Ensure chat channel slug uniqueness at DB level (#19277) 2022-12-02 11:15:43 +10:00
20221201035918_add_slug_unique_index_for_chat_channels.rb FIX: Ensure chat channel slug uniqueness at DB level (#19277) 2022-12-02 11:15:43 +10:00
20221202032006_add_chat_message_count_to_chat_channels.rb DEV: Add messages_count to ChatChannel table (#19295) 2022-12-06 08:40:46 +10:00
20221219082223_add_index_to_chat_messages.rb PERF: Add index for chat unread counts query (#19516) 2022-12-20 05:10:53 +08:00