GitLab has an optimization for deep pages of audit events. On the group and project APIs, from page 100 on, it was never used. The order the helper needs was computed, then thrown away.
This is the story of gitlab-org/gitlab!269899 . The first version put the order back, but ordered by id alone, and a page that should have been cheap still took about 1.8 seconds. A database review changed the order to match the index and the partition key. I'll cover the discarded order, the query plans, and the spec review that made the page contents explicit.
The issue: an optimization that could not see its ORDER BY
The issue was gitlab-org/gitlab#639234 , opened by Leaminn Ma. It is a short report, and the bug is in three lines:
audit_events . order ( id : :desc ) # rubocop:disable CodeReuse/ActiveRecord -- needed for offset optimization
Gitlab :: Pagination :: Offset :: PaginationWithIndexOnlyScan . new ( scope : audit_events , . . .
Enter fullscreen mode
Exit fullscreen mode
.order returns a new relation.…