mirror of
https://github.com/discourse/discourse.git
synced 2024-12-05 02:03:40 +08:00
bd301c3f08
Optimize `UserStatpost_read_count` calculation. In addition, tests were updated to fail when code is not evaluated. Creation of PostTiming was updating `post_read_count`. Count it has to be reset to ensure that ensure_consitency correctly calculates result. Extracting users seen in the last hour to separate Common Table Expression reduces the amount of processed rows. Before ``` Update on user_stats (cost=267492.07..270822.95 rows=2900 width=174) (actual time=12606.121..12606.127 rows=0 loops=1) -> Hash Join (cost=267492.07..270822.95 rows=2900 width=174) (actual time=12561.814..12603.689 rows=10 loops=1) Hash Cond: (user_stats.user_id = x.user_id) Join Filter: (x.c <> user_stats.posts_read_count) Rows Removed by Join Filter: 67 -> Seq Scan on user_stats (cost=0.00..3125.34 rows=75534 width=134) (actual time=0.014..39.173 rows=75534 loops=1) -> Hash (cost=267455.80..267455.80 rows=2901 width=48) (actual time=12558.613..12558.617 rows=77 loops=1) Buckets: 4096 Batches: 1 Memory Usage: 39kB -> Subquery Scan on x (cost=267376.03..267455.80 rows=2901 width=48) (actual time=12168.601..12558.572 rows=77 loops=1) -> GroupAggregate (cost=267376.03..267426.79 rows=2901 width=12) (actual time=12168.595..12558.525 rows=77 loops=1) Group Key: pt.user_id -> Sort (cost=267376.03..267383.28 rows=2901 width=4) (actual time=12100.490..12352.106 rows=2072830 loops=1) Sort Key: pt.user_id Sort Method: external merge Disk: 28488kB -> Nested Loop (cost=1.28..267209.18 rows=2901 width=4) (actual time=0.040..11528.680 rows=2072830 loops=1) -> Nested Loop (cost=0.86..261390.02 rows=13159 width=8) (actual time=0.030..3492.887 rows=3581648 loops=1) -> Index Scan using index_users_on_last_seen_at on users u (cost=0.42..89.71 rows=28 width=4) (actual time=0.010..0.201 rows=78 loops=1) Index Cond: (last_seen_at > '2023-04-11 00:22:49.555537'::timestamp without time zone) -> Index Scan using index_post_timings_on_user_id on post_timings pt (cost=0.44..9287.60 rows=4455 width=8) (actual time=0.081..38.542 rows=45919 loops=78) Index Cond: (user_id = u.id) -> Index Scan using forum_threads_pkey on topics t (cost=0.42..0.44 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=3581648) Index Cond: (id = pt.topic_id) Filter: ((deleted_at IS NULL) AND ((archetype)::text = 'regular'::text)) Rows Removed by Filter: 0 Planning Time: 0.692 ms Execution Time: 12612.587 ms ``` After ``` Update on user_stats (cost=9473.60..12804.30 rows=2828 width=174) (actual time=677.724..677.729 rows=0 loops=1) -> Hash Join (cost=9473.60..12804.30 rows=2828 width=174) (actual time=672.536..677.706 rows=1 loops=1) Hash Cond: (user_stats.user_id = x.user_id) Join Filter: (x.c <> user_stats.posts_read_count) Rows Removed by Join Filter: 54 -> Seq Scan on user_stats (cost=0.00..3125.34 rows=75534 width=134) (actual time=0.012..23.977 rows=75534 loops=1) -> Hash (cost=9438.24..9438.24 rows=2829 width=48) (actual time=647.818..647.822 rows=55 loops=1) Buckets: 4096 Batches: 1 Memory Usage: 37kB -> Subquery Scan on x (cost=9381.66..9438.24 rows=2829 width=48) (actual time=647.409..647.805 rows=55 loops=1) -> HashAggregate (cost=9381.66..9409.95 rows=2829 width=12) (actual time=647.403..647.786 rows=55 loops=1) Group Key: pt.user_id Batches: 1 Memory Usage: 121kB -> Nested Loop (cost=1.86..9367.51 rows=2829 width=4) (actual time=0.056..625.245 rows=120022 loops=1) -> Nested Loop (cost=1.44..3692.96 rows=12832 width=8) (actual time=0.047..171.754 rows=217440 loops=1) -> Nested Loop (cost=1.00..254.63 rows=25 width=12) (actual time=0.030..1.407 rows=56 loops=1) Join Filter: (u.id = user_stats_1.user_id) -> Nested Loop (cost=0.71..243.08 rows=25 width=8) (actual time=0.018..1.207 rows=87 loops=1) -> Index Scan using index_users_on_last_seen_at on users u (cost=0.42..86.71 rows=27 width=4) (actual time=0.009..0.156 rows=87 loops=1) Index Cond: (last_seen_at > '2023-04-11 00:47:07.437568'::timestamp without time zone) -> Index Only Scan using user_stats_pkey on user_stats us (cost=0.29..5.79 rows=1 width=4) (actual time=0.011..0.011 rows=1 loops=87) Index Cond: (user_id = u.id) Heap Fetches: 87 -> Index Scan using user_stats_pkey on user_stats user_stats_1 (cost=0.29..0.45 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=87) Index Cond: (user_id = us.user_id) Filter: (posts_read_count < 10000) Rows Removed by Filter: 0 -> Index Scan using index_post_timings_on_user_id on post_timings pt (cost=0.44..92.98 rows=4455 width=8) (actual time=0.036..2.492 rows=3883 loops=56) Index Cond: (user_id = user_stats_1.user_id) -> Index Scan using forum_threads_pkey on topics t (cost=0.42..0.44 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=217440) Index Cond: (id = pt.topic_id) Filter: ((deleted_at IS NULL) AND ((archetype)::text = 'regular'::text)) Rows Removed by Filter: 0 Planning Time: 1.406 ms Execution Time: 677.817 ms ``` |
||
---|---|---|
.. | ||
about_spec.rb | ||
admin_dashboard_data_spec.rb | ||
api_key_scope_spec.rb | ||
api_key_spec.rb | ||
application_request_spec.rb | ||
associated_group_spec.rb | ||
badge_spec.rb | ||
badge_type_spec.rb | ||
bookmark_spec.rb | ||
category_featured_topic_spec.rb | ||
category_group_spec.rb | ||
category_list_spec.rb | ||
category_spec.rb | ||
category_user_spec.rb | ||
child_theme_spec.rb | ||
color_scheme_color_spec.rb | ||
color_scheme_spec.rb | ||
developer_spec.rb | ||
digest_email_site_setting_spec.rb | ||
directory_item_spec.rb | ||
discourse_connect_spec.rb | ||
do_not_disturb_timing_spec.rb | ||
draft_sequence_spec.rb | ||
draft_spec.rb | ||
email_change_request_spec.rb | ||
email_log_spec.rb | ||
email_token_spec.rb | ||
embeddable_host_spec.rb | ||
emoji_spec.rb | ||
given_daily_like_spec.rb | ||
global_setting_spec.rb | ||
group_archived_message_spec.rb | ||
group_associated_group_spec.rb | ||
group_history_spec.rb | ||
group_request_spec.rb | ||
group_spec.rb | ||
group_user_spec.rb | ||
incoming_link_spec.rb | ||
incoming_links_report_spec.rb | ||
invite_redeemer_spec.rb | ||
invite_spec.rb | ||
javascript_cache_spec.rb | ||
locale_site_setting_spec.rb | ||
mailing_list_mode_site_setting_spec.rb | ||
notification_spec.rb | ||
optimized_image_spec.rb | ||
permalink_spec.rb | ||
plugin_store_spec.rb | ||
post_action_spec.rb | ||
post_action_type_spec.rb | ||
post_analyzer_spec.rb | ||
post_detail_spec.rb | ||
post_mover_spec.rb | ||
post_reply_key_spec.rb | ||
post_reply_spec.rb | ||
post_revision_spec.rb | ||
post_spec.rb | ||
post_timing_spec.rb | ||
private_message_topic_tracking_state_spec.rb | ||
published_page_spec.rb | ||
quoted_post_spec.rb | ||
remote_theme_spec.rb | ||
report_spec.rb | ||
reviewable_claimed_topic_spec.rb | ||
reviewable_flagged_post_spec.rb | ||
reviewable_history_spec.rb | ||
reviewable_post_spec.rb | ||
reviewable_queued_post_spec.rb | ||
reviewable_score_spec.rb | ||
reviewable_spec.rb | ||
reviewable_user_spec.rb | ||
s3_region_site_setting_spec.rb | ||
screened_email_spec.rb | ||
screened_ip_address_spec.rb | ||
screened_url_spec.rb | ||
search_log_spec.rb | ||
sidebar_section_link_spec.rb | ||
site_setting_spec.rb | ||
site_spec.rb | ||
sitemap_spec.rb | ||
skipped_email_log_spec.rb | ||
stylesheet_cache_spec.rb | ||
tag_group_spec.rb | ||
tag_spec.rb | ||
tag_user_spec.rb | ||
theme_field_spec.rb | ||
theme_modifier_set_spec.rb | ||
theme_spec.rb | ||
top_menu_item_spec.rb | ||
top_topic_spec.rb | ||
topic_allowed_user_spec.rb | ||
topic_converter_spec.rb | ||
topic_embed_spec.rb | ||
topic_featured_users_spec.rb | ||
topic_group_spec.rb | ||
topic_invite_spec.rb | ||
topic_link_click_spec.rb | ||
topic_link_spec.rb | ||
topic_list_spec.rb | ||
topic_participants_summary_spec.rb | ||
topic_posters_summary_spec.rb | ||
topic_spec.rb | ||
topic_tag_spec.rb | ||
topic_thumbnail_spec.rb | ||
topic_timer_spec.rb | ||
topic_tracking_state_spec.rb | ||
topic_user_spec.rb | ||
topic_view_item_spec.rb | ||
translation_override_spec.rb | ||
trust_level_and_staff_setting_spec.rb | ||
trust_level_setting_spec.rb | ||
trust_level3_requirements_spec.rb | ||
unsubscribe_key_spec.rb | ||
upload_reference_spec.rb | ||
upload_spec.rb | ||
user_action_spec.rb | ||
user_api_key_spec.rb | ||
user_archived_message_spec.rb | ||
user_associated_group_spec.rb | ||
user_auth_token_spec.rb | ||
user_avatar_spec.rb | ||
user_badge_spec.rb | ||
user_bookmark_list_spec.rb | ||
user_email_spec.rb | ||
user_export_spec.rb | ||
user_field_spec.rb | ||
user_history_spec.rb | ||
user_notification_schedule_spec.rb | ||
user_option_spec.rb | ||
user_profile_spec.rb | ||
user_profile_view_spec.rb | ||
user_search_spec.rb | ||
user_second_factor_spec.rb | ||
user_spec.rb | ||
user_stat_spec.rb | ||
user_status_spec.rb | ||
user_summary_spec.rb | ||
user_visit_spec.rb | ||
username_validator_spec.rb | ||
watched_word_spec.rb | ||
web_crawler_request_spec.rb | ||
web_hook_event_spec.rb | ||
web_hook_spec.rb |