Changed pending email query structure to improve speed #5405
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This fixes a performance bottleneck when loading the Channels -> Emails page with a large number of contacts in Mautic. The old code uses an inefficient NOT EXISTS query, and the new code uses a more efficient NOT IN query.
See an analysis of the different types of queries in MySQL here
To summarise, the query is going from the form:
to
It should be noted that the NOT EXISTS and NOT IN queries are slightly different in how they handle NULL values. However, this is irrelevant in our scenario, as the
lead_list_leads.lead_id
field to which we are comparing the subqueries IDs are set to NOT NULL, so the results will always be equivalent.Tests with 500,000 contacts show a 200x speed improvement for this query, from 200.5s down to 1.04s.
Steps to reproduce the bug:
Steps to test this PR: