mirror of
https://github.com/matrix-org/sliding-sync.git
synced 2025-03-10 13:37:11 +00:00
Remove materialized view and use a recursive CTE instead to get unique
event_types
This commit is contained in:
parent
9eee30b152
commit
ce15a2800c
@ -141,11 +141,6 @@ func NewEventTable(db *sqlx.DB) *EventTable {
|
|||||||
CREATE INDEX IF NOT EXISTS syncv3_nid_room_state_idx ON syncv3_events(room_id, event_nid, is_state);
|
CREATE INDEX IF NOT EXISTS syncv3_nid_room_state_idx ON syncv3_events(room_id, event_nid, is_state);
|
||||||
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS syncv3_events_room_event_nid_type_skey_idx ON syncv3_events(event_nid, event_type, state_key);
|
CREATE UNIQUE INDEX IF NOT EXISTS syncv3_events_room_event_nid_type_skey_idx ON syncv3_events(event_nid, event_type, state_key);
|
||||||
|
|
||||||
-- Create a materialized view for event_types (used on startup to get the latest events in each room)
|
|
||||||
CREATE MATERIALIZED VIEW IF NOT EXISTS event_types as
|
|
||||||
SELECT DISTINCT event_type
|
|
||||||
FROM syncv3_events;
|
|
||||||
`)
|
`)
|
||||||
return &EventTable{db}
|
return &EventTable{db}
|
||||||
}
|
}
|
||||||
@ -447,13 +442,20 @@ func (t *EventTable) SelectLatestEventsBetween(txn *sqlx.Tx, roomID string, lowe
|
|||||||
func (t *EventTable) selectLatestEventByTypeInAllRooms(txn *sqlx.Tx) ([]Event, error) {
|
func (t *EventTable) selectLatestEventByTypeInAllRooms(txn *sqlx.Tx) ([]Event, error) {
|
||||||
result := []Event{}
|
result := []Event{}
|
||||||
// What the following query does:
|
// What the following query does:
|
||||||
// 1. Gets all event types from a materialized view (updated on startup in `PrepareSnapshot`) as the `event_types` CTE
|
// 1. Gets all event types from a recursive CTE as the `event_types` CTE
|
||||||
// 2. Gets all rooms as the `room_ids` CTE
|
// 2. Gets all rooms as the `room_ids` CTE
|
||||||
// 3. Gets the latest event_nid for each event_type and room as the `max_by_ev_type` CTE
|
// 3. Gets the latest event_nid for each event_type and room as the `max_by_ev_type` CTE
|
||||||
// 4. Queries the required data using the event_nids provided by the `max_by_ev_type` CTE
|
// 4. Queries the required data using the event_nids provided by the `max_by_ev_type` CTE
|
||||||
rows, err := txn.Query(`
|
rows, err := txn.Query(`
|
||||||
WITH event_types AS (
|
WITH event_types AS (
|
||||||
SELECT * FROM event_types
|
WITH RECURSIVE t AS (
|
||||||
|
(SELECT event_type FROM syncv3_events ORDER BY event_type LIMIT 1) -- parentheses required
|
||||||
|
UNION ALL
|
||||||
|
SELECT (SELECT event_type FROM syncv3_events WHERE event_type > t.event_type ORDER BY event_type LIMIT 1)
|
||||||
|
FROM t
|
||||||
|
WHERE t.event_type IS NOT NULL
|
||||||
|
)
|
||||||
|
SELECT event_type FROM t WHERE event_type IS NOT NULL
|
||||||
), room_ids AS (
|
), room_ids AS (
|
||||||
SELECT DISTINCT room_id FROM syncv3_rooms
|
SELECT DISTINCT room_id FROM syncv3_rooms
|
||||||
), max_by_ev_type AS (
|
), max_by_ev_type AS (
|
||||||
|
@ -171,15 +171,6 @@ func (s *Storage) PrepareSnapshot(txn *sqlx.Tx) (tableName string, err error) {
|
|||||||
`SELECT UNNEST(membership_events) AS membership_nid INTO TEMP ` + tempTableName + ` FROM syncv3_snapshots
|
`SELECT UNNEST(membership_events) AS membership_nid INTO TEMP ` + tempTableName + ` FROM syncv3_snapshots
|
||||||
JOIN syncv3_rooms ON syncv3_snapshots.snapshot_id = syncv3_rooms.current_snapshot_id`,
|
JOIN syncv3_rooms ON syncv3_snapshots.snapshot_id = syncv3_rooms.current_snapshot_id`,
|
||||||
)
|
)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// Refresh the materialized view, so getting latest events by type per room
|
|
||||||
// can use a fresh view of the event_types.
|
|
||||||
_, err = txn.Exec("REFRESH MATERIALIZED VIEW event_types;")
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return tempTableName, nil
|
return tempTableName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user