mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: optimise original query instead of huge rewrite
better perf for all cases
This commit is contained in:
@@ -130,75 +130,44 @@ class TopicTrackingState
|
|||||||
new = TopicQuery.new_filter(Topic, "xxx").where_values.join(" AND ").gsub!("'xxx'", treat_as_new_topic_clause)
|
new = TopicQuery.new_filter(Topic, "xxx").where_values.join(" AND ").gsub!("'xxx'", treat_as_new_topic_clause)
|
||||||
|
|
||||||
sql = <<SQL
|
sql = <<SQL
|
||||||
|
WITH x AS (
|
||||||
WITH allowed_categories AS (
|
SELECT u.id AS user_id,
|
||||||
SELECT c.id FROM categories c
|
topics.id AS topic_id,
|
||||||
JOIN users u on u.id = :user_id
|
|
||||||
WHERE
|
|
||||||
( NOT c.read_restricted OR u.admin OR c.id IN (
|
|
||||||
SELECT c2.id FROM categories c2
|
|
||||||
JOIN category_groups cg ON cg.category_id = c2.id
|
|
||||||
JOIN group_users gu ON gu.user_id = :user_id AND cg.group_id = gu.group_id
|
|
||||||
WHERE c2.read_restricted )
|
|
||||||
) AND NOT EXISTS( SELECT 1 FROM category_users cu
|
|
||||||
WHERE
|
|
||||||
cu.user_id = :user_id AND
|
|
||||||
cu.category_id = c.id AND
|
|
||||||
cu.notification_level = #{CategoryUser.notification_levels[:muted]})
|
|
||||||
)
|
|
||||||
|
|
||||||
SELECT * FROM (
|
|
||||||
SELECT :user_id user_id,
|
|
||||||
topics.id topic_id,
|
|
||||||
topics.created_at,
|
topics.created_at,
|
||||||
highest_post_number,
|
highest_post_number,
|
||||||
last_read_post_number,
|
last_read_post_number,
|
||||||
topics.category_id,
|
c.id AS category_id,
|
||||||
tu.notification_level
|
tu.notification_level
|
||||||
FROM topics
|
FROM users u
|
||||||
JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = :user_id AND tu.last_read_post_number IS NOT NULL
|
INNER JOIN user_stats AS us ON us.user_id = u.id
|
||||||
JOIN allowed_categories c ON c.id = topics.category_id
|
FULL OUTER JOIN topics ON 1=1
|
||||||
JOIN users u on u.id = :user_id
|
LEFT JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = u.id
|
||||||
|
LEFT JOIN categories c ON c.id = topics.category_id
|
||||||
WHERE topics.archetype <> 'private_message' AND
|
WHERE u.id = :user_id AND
|
||||||
(#{unread}) AND
|
topics.archetype <> 'private_message' AND
|
||||||
|
((#{unread}) OR (#{new})) AND
|
||||||
(topics.visible OR u.admin OR u.moderator) AND
|
(topics.visible OR u.admin OR u.moderator) AND
|
||||||
topics.deleted_at IS NULL
|
topics.deleted_at IS NULL AND
|
||||||
/*topic_filter*/
|
( category_id IS NULL OR NOT c.read_restricted OR u.admin OR category_id IN (
|
||||||
ORDER BY topics.bumped_at DESC
|
SELECT c2.id FROM categories c2
|
||||||
LIMIT 200
|
JOIN category_groups cg ON cg.category_id = c2.id
|
||||||
) X
|
JOIN group_users gu ON gu.user_id = u.id AND cg.group_id = gu.group_id
|
||||||
|
WHERE c2.read_restricted )
|
||||||
|
)
|
||||||
|
AND NOT EXISTS( SELECT 1 FROM category_users cu
|
||||||
|
WHERE last_read_post_number IS NULL AND
|
||||||
|
cu.user_id = :user_id AND
|
||||||
|
cu.category_id = topics.category_id AND
|
||||||
|
cu.notification_level = #{CategoryUser.notification_levels[:muted]})
|
||||||
|
|
||||||
UNION ALL
|
|
||||||
|
|
||||||
SELECT * FROM (
|
|
||||||
SELECT :user_id user_id,
|
|
||||||
topics.id topic_id,
|
|
||||||
topics.created_at,
|
|
||||||
highest_post_number,
|
|
||||||
NULL::int last_read_post_number,
|
|
||||||
topics.category_id,
|
|
||||||
tu.notification_level
|
|
||||||
FROM topics
|
|
||||||
JOIN users u on u.id = :user_id
|
|
||||||
JOIN user_stats AS us ON us.user_id = u.id
|
|
||||||
JOIN allowed_categories c ON c.id = topics.category_id
|
|
||||||
LEFT JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = :user_id AND tu.last_read_post_number IS NOT NULL
|
|
||||||
|
|
||||||
WHERE tu.id IS NULL AND
|
|
||||||
(#{new}) AND
|
|
||||||
(topics.visible OR u.admin OR u.moderator) AND
|
|
||||||
topics.deleted_at IS NULL
|
|
||||||
/*topic_filter*/
|
|
||||||
ORDER BY topics.bumped_at DESC
|
|
||||||
LIMIT 200
|
|
||||||
) Y
|
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
if topic_id
|
if topic_id
|
||||||
sql.gsub! "/*topic_filter*/", " AND topics.id = :topic_id"
|
sql << " AND topics.id = :topic_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sql << " ORDER BY topics.bumped_at DESC ) SELECT * FROM x LIMIT 500"
|
||||||
|
|
||||||
SqlBuilder.new(sql)
|
SqlBuilder.new(sql)
|
||||||
.map_exec(TopicTrackingState, user_id: user_id, topic_id: topic_id)
|
.map_exec(TopicTrackingState, user_id: user_id, topic_id: topic_id)
|
||||||
|
|
||||||
|
|||||||
@@ -36,8 +36,7 @@ describe TopicTrackingState do
|
|||||||
TopicUser.create!(user_id: user.id, topic_id: post.topic_id, last_read_post_number: 1, notification_level: 3)
|
TopicUser.create!(user_id: user.id, topic_id: post.topic_id, last_read_post_number: 1, notification_level: 3)
|
||||||
|
|
||||||
report = TopicTrackingState.report(user.id)
|
report = TopicTrackingState.report(user.id)
|
||||||
# no read state for muted categories, query is faster
|
expect(report.length).to eq(1)
|
||||||
expect(report.length).to eq(0)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "correctly gets the tracking state" do
|
it "correctly gets the tracking state" do
|
||||||
|
|||||||
Reference in New Issue
Block a user