-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract post context join out into temporary table
Upgrading to MySQL 8 has enabled better performance analysis, which I've immediately applied to the longest-running query. The big JOIN used to gather context for posts (their user, their parent, and their thread) forms the start of the notifiable users subquery. As it's in the subquery, it is executed each time the subquery is run. While it's not the whole query, it is a significant part of it. Extracting this JOIN out to a temporary table which is only created once will make the query more efficient overall. The temporary table will be destroyed at the end of the connection session, so I don't need to worry about that, or about the table becoming out of date when new posts are downloaded as it won't exist until after that. The query results in a table scan on this temporary table, which may not be the most efficient approach. That being said, I didn't find any indexes that make it any faster - all I tried slowed it down. The query could be hugely sped up by reducing the rows in this table i.e. the number of posts - e.g. by limiting it by date - but that is out of scope for now.
- Loading branch information
Showing
3 changed files
with
34 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
CREATE TEMPORARY TABLE IF NOT EXISTS post_with_context | ||
WITH cte AS ( | ||
SELECT | ||
post.user_id AS post_user_id, | ||
post.posted_timestamp AS post_posted_timestamp, | ||
parent_post.id AS parent_post_id, | ||
parent_post.user_id AS parent_post_user_id, | ||
thread.id AS thread_id, | ||
first_post_in_thread.user_id AS first_post_in_thread_user_id | ||
FROM | ||
post | ||
INNER JOIN | ||
thread ON thread.id = post.thread_id | ||
INNER JOIN | ||
thread_first_post ON thread_first_post.thread_id = thread.id | ||
INNER JOIN | ||
post AS first_post_in_thread ON first_post_in_thread.id = thread_first_post.post_id | ||
LEFT JOIN | ||
post AS parent_post ON parent_post.id = post.parent_post_id | ||
WHERE | ||
-- Remove deleted threads/posts | ||
thread.is_deleted = 0 AND post.is_deleted = 0 | ||
) | ||
SELECT * FROM cte |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters