Certain user queries retrieve a single user using an ORDER BY
For example, I'm seeing a lot of queries like this in pg_stat_activity:
SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" DESC LIMIT 1
Since IDs are also unique we can also remove the limit. The difference between such queries is:
gitlabhq_production=# explain analyze select * from users where id = 209240 order by id desc limit 1;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..8.38 rows=1 width=666) (actual time=0.046..0.046 rows=1 loops=1)
-> Index Scan using users_pkey on users (cost=0.00..8.38 rows=1 width=666) (actual time=0.044..0.044 rows=1 loops=1)
Index Cond: (id = 209240)
Total runtime: 0.085 ms
(4 rows)
gitlabhq_production=# explain analyze select * from users where id = 209240;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
Index Scan using users_pkey on users (cost=0.00..8.38 rows=1 width=666) (actual time=0.017..0.018 rows=1 loops=1)
Index Cond: (id = 209240)
Total runtime: 0.051 ms
(3 rows)
So it's not that big of a deal, but the ORDER BY and LIMIT are simply not needed.
Edited by 🤖 GitLab Bot 🤖