Use received_ts to find uncensored redacted events

Joining against `events` and ordering by `stream_ordering` is
inefficient as it forced scanning the entirety of the redactions table.

This isn't the case if we use `redactions.received_ts` column as we can
then use an index.
This commit is contained in:
Erik Johnston 2019-10-01 13:28:41 +01:00
parent 898dde981b
commit 5e8387af9e

View File

@ -1589,36 +1589,29 @@ class EventsStore(
if self.hs.config.redaction_retention_period is None: if self.hs.config.redaction_retention_period is None:
return return
max_pos = yield self.find_first_stream_ordering_after_ts( before_ts = self._clock.time_msec() - self.hs.config.redaction_retention_period
self._clock.time_msec() - self.hs.config.redaction_retention_period
)
# We fetch all redactions that: # We fetch all redactions that:
# 1. point to an event we have, # 1. point to an event we have,
# 2. has a stream ordering from before the cut off, and # 2. has a received_ts from before the cut off, and
# 3. we haven't yet censored. # 3. we haven't yet censored.
# #
# This is limited to 100 events to ensure that we don't try and do too # This is limited to 100 events to ensure that we don't try and do too
# much at once. We'll get called again so this should eventually catch # much at once. We'll get called again so this should eventually catch
# up. # up.
#
# We use the range [-max_pos, max_pos] to handle backfilled events,
# which are given negative stream ordering.
sql = """ sql = """
SELECT redact_event.event_id, redacts FROM redactions SELECT redactions.event_id, redacts FROM redactions
INNER JOIN events AS redact_event USING (event_id)
LEFT JOIN events AS original_event ON ( LEFT JOIN events AS original_event ON (
redact_event.room_id = original_event.room_id redacts = original_event.event_id
AND redacts = original_event.event_id
) )
WHERE NOT have_censored WHERE NOT have_censored
AND ? <= redact_event.stream_ordering AND redact_event.stream_ordering <= ? AND redactions.received_ts <= ?
ORDER BY redact_event.stream_ordering ASC ORDER BY redactions.received_ts ASC
LIMIT ? LIMIT ?
""" """
rows = yield self._execute( rows = yield self._execute(
"_censor_redactions_fetch", None, sql, -max_pos, max_pos, 100 "_censor_redactions_fetch", None, sql, before_ts, 100
) )
updates = [] updates = []