Speed up pruning of `user_ips` table (#16667)

Silly query planner
This commit is contained in:
Erik Johnston 2023-11-29 11:54:42 +00:00 committed by GitHub
parent 6f2be7794e
commit df366966b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 10 deletions

1
changelog.d/16667.misc Normal file
View File

@ -0,0 +1 @@
Reduce database load of pruning old `user_ips`.

View File

@ -465,18 +465,15 @@ class ClientIpWorkerStore(ClientIpBackgroundUpdateStore, MonthlyActiveUsersWorke
# #
# This works by finding the max last_seen that is less than the given # This works by finding the max last_seen that is less than the given
# time, but has no more than N rows before it, deleting all rows with # time, but has no more than N rows before it, deleting all rows with
# a lesser last_seen time. (We COALESCE so that the sub-SELECT always # a lesser last_seen time. (We use an `IN` clause to force postgres to
# returns exactly one row). # use the index, otherwise it tends to do a seq scan).
sql = """ sql = """
DELETE FROM user_ips DELETE FROM user_ips
WHERE last_seen <= ( WHERE last_seen IN (
SELECT COALESCE(MAX(last_seen), -1)
FROM (
SELECT last_seen FROM user_ips SELECT last_seen FROM user_ips
WHERE last_seen <= ? WHERE last_seen <= ?
ORDER BY last_seen ASC ORDER BY last_seen ASC
LIMIT 5000 LIMIT 5000
) AS u
) )
""" """