synapse/changelog.d
reivilibre bfb3a6e700
Improve performance of device deletion by adding missing index. (#18582)
<ol>
<li>

Reorder columns in `event_txn_id_device_id_txn_id` index \
This now satisfies the foreign key on `(user_id, device_id)` making
reverse lookups, as needed for device deletions, more efficient.

This improves device deletion performance by on the order of 8 to 10×
on matrix.org.


</li>
</ol>


Rationale:

## On the `event_txn_id_device_id` table:

We currently have this index:
```sql
-- This ensures that there is only one mapping per (room_id, user_id, device_id, txn_id) tuple.
CREATE UNIQUE INDEX IF NOT EXISTS event_txn_id_device_id_txn_id 
    ON event_txn_id_device_id(room_id, user_id, device_id, txn_id);
```

The main way we use this table is
```python
        return await self.db_pool.simple_select_one_onecol(
            table="event_txn_id_device_id",
            keyvalues={
                "room_id": room_id,
                "user_id": user_id,
                "device_id": device_id,
                "txn_id": txn_id,
            },
            retcol="event_id",
            allow_none=True,
            desc="get_event_id_from_transaction_id_and_device_id",
        )
```

But this foreign key is relatively unsupported, making deletions in
the devices table inefficient (full index scan on the above index):
```sql
    FOREIGN KEY (user_id, device_id)
        REFERENCES devices (user_id, device_id) ON DELETE CASCADE
```

I propose re-ordering the columns in that index to: `(user_id,
device_id, room_id, txn_id)` (by replacing it).

That way the foreign key back-check can rely on the prefix of this
index, but it's still useful for the original purpose it was made for.

It doesn't take any extra disk space and does not harm write performance
(because the same amount of writing work needs to be performed).

---------

Signed-off-by: Olivier 'reivilibre <oliverw@matrix.org>
2025-06-30 10:36:12 +01:00
..
.gitignore
18196.feature Add forget_forced_upon_leave capability as per MSC4267 (#18196) 2025-06-27 15:07:24 -05:00
18241.feature Add federated_user_may_invite spam checker callback (#18241) 2025-06-26 12:27:21 +01:00
18509.bugfix Fix registering of background updates for split main/state db (#18509) 2025-06-25 13:59:18 +01:00
18573.misc Improve docstring on simple_upsert_many. (#18573) 2025-06-30 10:35:23 +01:00
18582.bugfix Improve performance of device deletion by adding missing index. (#18582) 2025-06-30 10:36:12 +01:00
18595.misc Lift pausing on ratelimited requests to http layer (#18595) 2025-06-25 14:32:55 +00:00
18600.misc Fix backwards compat for DirectServeJsonResource (#18600) 2025-06-26 14:05:48 +00:00