perf(sql): Optimise searchForPipelinesByTrigger LIMIT and OFFSET SQL query (backport #4698) #4700
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.
Searching for the executions of the pipelines of an application by triggerType and eventId (Gate endpoint: applications//executions/search?triggerTypes=&eventId=) query execution is not optimal and creates high response times as the execution history of the application increases and/or the application has a lot of pipelines.
The retrievePipelinesForPipelineConfigIdsBetweenBuildTimeBoundary creates SQL queries with LIMIT 20 and OFFSET that look like this:
With this change the queries are optimised with a deferred join (see https://emmer.dev/blog/the-dangers-of-offset-with-mysql/). The new queries look like:
The above query lets the server to examine only the index (without accessing the rows) and once the desired rows are found is joined against the full table to retrieve the desired columns for the rows.
Explain plan of a single query on an App:
Before the change:
After the change:
With this change on simulated traffic for the above Application we have seen drop in response time from ~5.6sec to ~4.5sec.
This is an automatic backport of pull request #4698 done by [Mergify](https://mergify.com).