Optimize snippet fetching by ID in Snippets API

What does this MR do and why?

Contributes to #525656 (closed)

Problem

The Snippets API experiences timeout errors due to slow SQL queries. The current implementation applies complex filtering before using the snippet ID, causing unnecessary performance overhead.

Solution

Apply the snippet ID filter first in the query chain to eliminate irrelevant records early, reducing query execution time and preventing timeouts.

References

Snippets API: request timeout error (#525656 - closed)

Database

Before optimization

Time: 1.205 min
  - planning: 21.360 ms
  - execution: 1.205 min
    - I/O read: 21.054 s
    - I/O write: 0.000 ms

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/42552/commands/130243

After optimization

Time: 20.113 ms
  - planning: 19.516 ms
  - execution: 0.597 ms
    - I/O read: 0.000 ms
    - I/O write: 0.000 ms

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/42552/commands/130242

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #525656 (closed)

Merge request reports

Loading