Improve high-frequency database query
This is to track the gitlab-ee code change to improve the high-frequency database query found in https://gitlab.com/gitlab-org/gitlab-ce/issues/60524.
The query takes more than 50% of total database time on the primary and is expected to generate significant load on the database.