Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(sql): optimize post search query on the join with messages #3877

Merged
merged 2 commits into from
Mar 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/App/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,31 @@ protected function selectQuery(array $where = [])
$query = parent::selectQuery($where);

// Join to messages and load message id
$query->join('messages', 'LEFT')->on('posts.id', '=', 'messages.post_id')
$query
->join('messages', 'LEFT')
->on('posts.id', '=', 'messages.post_id')
->select(
['messages.id', 'message_id'],
['messages.type', 'source'],
['messages.contact_id', 'contact_id'],
['messages.data_source_message_id', 'data_source_message_id']
['messages.type', 'source']
);

/*
* The above join is optimized by the (post_id,type) index on messages.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the most useful comment block I've seen in a while, you rock

*
* Add now a separate join into same table, to retrieve more message details.
*
* Compared to having all the details come from a single join, this will speed
* things up *very* considerably, *IF* there are many messages, *BUT* rather
* few matches of posts to messages.
* (This easily happens when twitter searches have few usable results,
* spam filled e-mail inboxes are being imported, etc)
*/
$query
->join(['messages', 'msgs2'], 'LEFT')
->on('msgs2.id', '=', 'messages.id')
->select(
['msgs2.contact_id', 'contact_id'],
['msgs2.data_source_message_id', 'data_source_message_id']
);

// Join to form
Expand Down