최근 PostAlerter 작업이 중단되거나 OOM 발생

해당 잡의 성능을 개선하기 위한 또 다른 시도를 병합했습니다:

몇몇 인스턴스에서 테스트를 해보았는데 문제는 없었지만, 그 인스턴스들은 당신의 것보다 규모가 작았습니다.

이 커밋을 적용해도 여전히 실패한다면 EXPLAIN ANALYSE 리포트를 제공해 주시겠어요? 생성을 위한 스크립트는 다음과 같습니다:

topic = Topic.last
user_option_sql_fragment =
  if SiteSetting.watched_precedence_over_muted
    <<~SQL
    INTERSECT
    SELECT user_id FROM user_options WHERE user_options.watched_precedence_over_muted IS false
    SQL
  else
    <<~SQL
    EXCEPT
    SELECT user_id FROM user_options WHERE user_options.watched_precedence_over_muted IS true
    SQL
  end
user_ids_sql = <<~SQL
  (
    SELECT user_id FROM category_users WHERE category_id = #{topic.category_id.to_i} AND notification_level = #{CategoryUser.notification_levels[:muted]}
    UNION
    SELECT user_id FROM tag_users tu JOIN topic_tags tt ON tt.tag_id = tu.tag_id AND tt.topic_id = #{topic.id} WHERE tu.notification_level = #{TagUser.notification_levels[:muted]}
    EXCEPT
    SELECT user_id FROM topic_users tus WHERE tus.topic_id = #{topic.id} AND tus.notification_level = #{TopicUser.notification_levels[:watching]}
  )
  #{user_option_sql_fragment}
SQL
sql = User.where("id IN (#{user_ids_sql})").to_sql

sql_with_index = <<SQL
EXPLAIN ANALYZE #{sql};
SQL
result = ActiveRecord::Base.connection.execute("#{sql_with_index}")
puts sql_with_index
result.each do |r|
  puts r.values
end

누락된 인덱스를 찾거나 이 쿼리의 어떤 부분이 그렇게 느린지를 파악하는 데 도움이 될 것입니다.

3개의 좋아요