mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 03:23:38 +08:00
cd64e88711
* PERF: Improve database query perf when loading topics for a category. Instead of left joining the `topics` table against `categories` by filtering with `categories.id`, we can improve the query plan by filtering against `topics.category_id` first before joining which helps to reduce the number of rows in the topics table that has to be joined against the other tables and also make better use of our existing index. The following is a before and after of the query plan for a category with many subcategories. Before: ``` QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------- Limit (cost=1.28..747.09 rows=30 width=12) (actual time=85.502..2453.727 rows=30 loops=1) -> Nested Loop Left Join (cost=1.28..566518.36 rows=22788 width=12) (actual time=85.501..2453.722 rows=30 loops=1) Join Filter: (category_users.category_id = topics.category_id) Filter: ((topics.category_id = 11) OR (COALESCE(category_users.notification_level, 1) <> 0) OR (tu.notification_level > 1)) -> Nested Loop Left Join (cost=1.00..566001.58 rows=22866 width=20) (actual time=85.494..2453.702 rows=30 loops=1) Filter: ((COALESCE(tu.notification_level, 1) > 0) AND ((topics.category_id <> 11) OR (topics.pinned_at IS NULL) OR ((t opics.pinned_at <= tu.cleared_pinned_at) AND (tu.cleared_pinned_at IS NOT NULL)))) Rows Removed by Filter: 1 -> Nested Loop (cost=0.57..528561.75 rows=68606 width=24) (actual time=85.472..2453.562 rows=31 loops=1) Join Filter: ((topics.category_id = categories.id) AND ((categories.topic_id <> topics.id) OR (categories.id = 1 1))) Rows Removed by Join Filter: 13938306 -> Index Scan using index_topics_on_bumped_at on topics (cost=0.42..100480.05 rows=715549 width=24) (actual ti me=0.010..633.015 rows=464623 loops=1) Filter: ((deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text)) Rows Removed by Filter: 105321 -> Materialize (cost=0.14..36.04 rows=30 width=8) (actual time=0.000..0.002 rows=30 loops=464623) -> Index Scan using categories_pkey on categories (cost=0.14..35.89 rows=30 width=8) (actual time=0.006. .0.040 rows=30 loops=1) Index Cond: (id = ANY ('{11,53,57,55,54,56,112,94,107,115,116,117,97,95,102,103,101,105,99,114,106,1 13,104,98,100,96,108,109,110,111}'::integer[])) -> Index Scan using index_topic_users_on_topic_id_and_user_id on topic_users tu (cost=0.43..0.53 rows=1 width=16) (a ctual time=0.004..0.004 rows=0 loops=31) Index Cond: ((topic_id = topics.id) AND (user_id = 1103877)) -> Materialize (cost=0.28..2.30 rows=1 width=8) (actual time=0.000..0.000 rows=0 loops=30) -> Index Scan using index_category_users_on_user_id_and_last_seen_at on category_users (cost=0.28..2.29 rows=1 width =8) (actual time=0.004..0.004 rows=0 loops=1) Index Cond: (user_id = 1103877) Planning Time: 1.359 ms Execution Time: 2453.765 ms (23 rows) ``` After: ``` QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=1.28..438.55 rows=30 width=12) (actual time=38.297..657.215 rows=30 loops=1) -> Nested Loop Left Join (cost=1.28..195944.68 rows=13443 width=12) (actual time=38.296..657.211 rows=30 loops=1) Filter: ((categories.topic_id <> topics.id) OR (topics.category_id = 11)) Rows Removed by Filter: 29 -> Nested Loop Left Join (cost=1.13..193462.59 rows=13443 width=16) (actual time=38.289..657.092 rows=59 loops=1) Join Filter: (category_users.category_id = topics.category_id) Filter: ((topics.category_id = 11) OR (COALESCE(category_users.notification_level, 1) <> 0) OR (tu.notification_level > 1)) -> Nested Loop Left Join (cost=0.85..193156.79 rows=13489 width=20) (actual time=38.282..657.059 rows=59 loops=1) Filter: ((COALESCE(tu.notification_level, 1) > 0) AND ((topics.category_id <> 11) OR (topics.pinned_at IS NULL) OR ((topics.pinned_at <= tu.cleared_pinned_at) AND (tu.cleared_pinned_at IS NOT NULL)))) Rows Removed by Filter: 1 -> Index Scan using index_topics_on_bumped_at on topics (cost=0.42..134521.06 rows=40470 width=24) (actual time=38.267..656.850 rows=60 loops=1) Filter: ((deleted_at IS NULL) AND ((archetype)::text <> 'private_message'::text) AND (category_id = ANY ('{11,53,57,55,54,56,112,94,107,115,116,117,97,95,102,103,101,105,99,114,106,113,104,98,100,96,108,109,110,111}'::integer[]))) Rows Removed by Filter: 569895 -> Index Scan using index_topic_users_on_topic_id_and_user_id on topic_users tu (cost=0.43..1.43 rows=1 width=16) (actual time=0.003..0.003 rows=0 loops=60) Index Cond: ((topic_id = topics.id) AND (user_id = 1103877)) -> Materialize (cost=0.28..2.30 rows=1 width=8) (actual time=0.000..0.000 rows=0 loops=59) -> Index Scan using index_category_users_on_user_id_and_last_seen_at on category_users (cost=0.28..2.29 rows=1 width=8) (actual time=0.004..0.004 rows=0 loops=1) Index Cond: (user_id = 1103877) -> Index Scan using categories_pkey on categories (cost=0.14..0.17 rows=1 width=8) (actual time=0.001..0.001 rows=1 loops=59) Index Cond: (id = topics.category_id) Planning Time: 1.633 ms Execution Time: 657.255 ms (22 rows) ``` * PERF: Optimize index on topics bumped_at. Replace `index_topics_on_bumped_at` index with a partial index on `Topic#bumped_at` filtered by archetype since there is already another index that covers private topics. |
||
---|---|---|
.. | ||
auth | ||
common_passwords | ||
concern | ||
file_store | ||
freedom_patches | ||
guardian | ||
highlight_js | ||
imap | ||
import | ||
middleware | ||
migration | ||
plugin | ||
pretty_text | ||
rate_limiter | ||
scheduler | ||
site_settings | ||
stylesheet | ||
svg_sprite | ||
theme_store | ||
validators | ||
wizard | ||
admin_confirmation_spec.rb | ||
admin_user_index_query_spec.rb | ||
archetype_spec.rb | ||
cache_spec.rb | ||
category_badge_spec.rb | ||
composer_messages_finder_spec.rb | ||
content_buffer_spec.rb | ||
cooked_post_processor_spec.rb | ||
crawler_detection_spec.rb | ||
current_user_spec.rb | ||
directory_helper_spec.rb | ||
discourse_diff_spec.rb | ||
discourse_event_spec.rb | ||
discourse_hub_spec.rb | ||
discourse_plugin_registry_spec.rb | ||
discourse_redis_spec.rb | ||
discourse_spec.rb | ||
discourse_tagging_spec.rb | ||
discourse_updates_spec.rb | ||
distributed_memoizer_spec.rb | ||
distributed_mutex_spec.rb | ||
email_cook_spec.rb | ||
email_updater_spec.rb | ||
enum_spec.rb | ||
excerpt_parser_spec.rb | ||
feed_element_installer_spec.rb | ||
feed_item_accessor_spec.rb | ||
file_helper_spec.rb | ||
filter_best_posts_spec.rb | ||
final_destination_spec.rb | ||
flag_settings_spec.rb | ||
gaps_spec.rb | ||
global_path_spec.rb | ||
guardian_spec.rb | ||
has_errors_spec.rb | ||
hijack_spec.rb | ||
html_prettify_spec.rb | ||
html_to_markdown_spec.rb | ||
image_sizer_spec.rb | ||
inline_oneboxer_spec.rb | ||
js_locale_helper_spec.rb | ||
json_error_spec.rb | ||
letter_avatar_spec.rb | ||
method_profiler_spec.rb | ||
new_post_manager_spec.rb | ||
new_post_result_spec.rb | ||
oneboxer_spec.rb | ||
onpdiff_spec.rb | ||
pbkdf2_spec.rb | ||
pinned_check_spec.rb | ||
plain_text_to_markdown_spec.rb | ||
post_action_creator_spec.rb | ||
post_creator_spec.rb | ||
post_destroyer_spec.rb | ||
post_locker_spec.rb | ||
post_merger_spec.rb | ||
post_revisor_spec.rb | ||
presence_channel_spec.rb | ||
pretty_text_spec.rb | ||
promotion_spec.rb | ||
quote_comparer_spec.rb | ||
rate_limiter_spec.rb | ||
redis_store_spec.rb | ||
retrieve_title_spec.rb | ||
rtl_spec.rb | ||
s3_helper_spec.rb | ||
s3_inventory_multisite_spec.rb | ||
s3_inventory_spec.rb | ||
score_calculator_spec.rb | ||
scss_checker_spec.rb | ||
search_spec.rb | ||
secure_session_spec.rb | ||
site_icon_manager_spec.rb | ||
site_setting_extension_multisite_spec.rb | ||
site_setting_extension_spec.rb | ||
slug_spec.rb | ||
spam_handler_spec.rb | ||
suggested_topics_builder_spec.rb | ||
system_message_spec.rb | ||
text_cleaner_spec.rb | ||
text_sentinel_spec.rb | ||
theme_settings_manager_spec.rb | ||
theme_settings_parser_spec.rb | ||
timeline_lookup_spec.rb | ||
topic_creator_spec.rb | ||
topic_publisher_spec.rb | ||
topic_query_spec.rb | ||
topic_retriever_spec.rb | ||
topic_view_spec.rb | ||
topics_bulk_action_spec.rb | ||
trashable_spec.rb | ||
trust_level_spec.rb | ||
unread_spec.rb | ||
url_helper_spec.rb | ||
user_lookup_spec.rb | ||
user_name_suggester_spec.rb | ||
version_spec.rb |