Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Speed up pruning of user_ips table (#16667)
Browse files Browse the repository at this point in the history
Silly query planner
  • Loading branch information
erikjohnston authored Nov 29, 2023
1 parent 6f2be77 commit df36696
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog.d/16667.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduce database load of pruning old `user_ips`.
17 changes: 7 additions & 10 deletions synapse/storage/databases/main/client_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,18 +465,15 @@ async def _prune_old_user_ips(self) -> None:
#
# 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
# a lesser last_seen time. (We COALESCE so that the sub-SELECT always
# returns exactly one row).
# a lesser last_seen time. (We use an `IN` clause to force postgres to
# use the index, otherwise it tends to do a seq scan).
sql = """
DELETE FROM user_ips
WHERE last_seen <= (
SELECT COALESCE(MAX(last_seen), -1)
FROM (
SELECT last_seen FROM user_ips
WHERE last_seen <= ?
ORDER BY last_seen ASC
LIMIT 5000
) AS u
WHERE last_seen IN (
SELECT last_seen FROM user_ips
WHERE last_seen <= ?
ORDER BY last_seen ASC
LIMIT 5000
)
"""

Expand Down

0 comments on commit df36696

Please sign in to comment.