FIX: add index to sidebar_section_link (#20234)

Index on linkable_type and linkable_id should increase performance of this subquery https://github.com/discourse/discourse/blob/main/app/services/sidebar_site_settings_backfiller.rb#L86

Also, distinct is removing duplicates which are unnecessary.
This commit is contained in:
Krzysztof Kotlarek 2023-02-10 11:14:22 +11:00 committed by GitHub
parent 6338287e89
commit cbd021db15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -40,5 +40,6 @@ end
#
# Indexes
#
# idx_unique_sidebar_section_links (user_id,linkable_type,linkable_id) UNIQUE
# idx_unique_sidebar_section_links (user_id,linkable_type,linkable_id) UNIQUE
# index_sidebar_section_links_on_linkable_type_and_linkable_id (linkable_type,linkable_id)
#

View File

@ -81,7 +81,7 @@ class SidebarSiteSettingsBackfiller
FROM users
WHERE users.id NOT IN (
SELECT
sidebar_section_links.user_id
DISTINCT(sidebar_section_links.user_id)
FROM sidebar_section_links
WHERE sidebar_section_links.linkable_type = '#{@linkable_klass.to_s}'
AND sidebar_section_links.linkable_id IN (#{@added_ids.join(",")})

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
class AddLinkableIndexToSidebarSectionLinks < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
def up
execute <<~SQL
DROP INDEX CONCURRENTLY IF EXISTS index_sidebar_section_links_on_linkable_type_and_linkable_id
SQL
execute <<~SQL
CREATE INDEX CONCURRENTLY index_sidebar_section_links_on_linkable_type_and_linkable_id
ON sidebar_section_links (linkable_type,linkable_id)
SQL
end
def down
execute <<~SQL
DROP INDEX CONCURRENTLY IF EXISTS index_sidebar_section_links_on_linkable_type_and_linkable_id
SQL
end
end