Skip to content
Snippets Groups Projects
Unverified Commit df366966 authored by Erik Johnston's avatar Erik Johnston Committed by GitHub
Browse files

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

Silly query planner
parent 6f2be779
No related branches found
No related tags found
No related merge requests found
Reduce database load of pruning old `user_ips`.
...@@ -465,18 +465,15 @@ class ClientIpWorkerStore(ClientIpBackgroundUpdateStore, MonthlyActiveUsersWorke ...@@ -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) SELECT last_seen FROM user_ips
FROM ( WHERE last_seen <= ?
SELECT last_seen FROM user_ips ORDER BY last_seen ASC
WHERE last_seen <= ? LIMIT 5000
ORDER BY last_seen ASC
LIMIT 5000
) AS u
) )
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment