mirror of
https://mau.dev/maunium/synapse.git
synced 2024-08-26 20:22:22 +00:00
Make get_events_around more efficient on sqlite3
This commit is contained in:
parent
d44d11d864
commit
b6b0132ac7
@ -40,7 +40,7 @@ from synapse.util.caches.descriptors import cached
|
|||||||
from synapse.api.constants import EventTypes
|
from synapse.api.constants import EventTypes
|
||||||
from synapse.types import RoomStreamToken
|
from synapse.types import RoomStreamToken
|
||||||
from synapse.util.logcontext import preserve_fn
|
from synapse.util.logcontext import preserve_fn
|
||||||
from synapse.storage.engines import PostgresEngine
|
from synapse.storage.engines import PostgresEngine, Sqlite3Engine
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -614,19 +614,55 @@ class StreamStore(SQLBaseStore):
|
|||||||
results["stream_ordering"],
|
results["stream_ordering"],
|
||||||
)
|
)
|
||||||
|
|
||||||
query_before = (
|
if isinstance(self.database_engine, Sqlite3Engine):
|
||||||
"SELECT topological_ordering, stream_ordering, event_id FROM events"
|
# SQLite3 doesn't optimise ``(x < a) OR (x = a AND y < b)``
|
||||||
" WHERE room_id = ? AND %s"
|
# So we give pass it to SQLite3 as the UNION ALL of the two queries.
|
||||||
" ORDER BY topological_ordering DESC, stream_ordering DESC LIMIT ?"
|
|
||||||
) % (upper_bound(token, self.database_engine, inclusive=False),)
|
|
||||||
|
|
||||||
query_after = (
|
query_before = (
|
||||||
"SELECT topological_ordering, stream_ordering, event_id FROM events"
|
"SELECT topological_ordering, stream_ordering, event_id FROM events"
|
||||||
" WHERE room_id = ? AND %s"
|
" WHERE room_id = ? AND topological_ordering < ?"
|
||||||
" ORDER BY topological_ordering ASC, stream_ordering ASC LIMIT ?"
|
" UNION ALL"
|
||||||
) % (lower_bound(token, self.database_engine, inclusive=False),)
|
" SELECT topological_ordering, stream_ordering, event_id FROM events"
|
||||||
|
" WHERE room_id = ? AND topological_ordering = ? AND stream_ordering < ?"
|
||||||
|
" ORDER BY topological_ordering DESC, stream_ordering DESC LIMIT ?"
|
||||||
|
)
|
||||||
|
before_args = (
|
||||||
|
room_id, token.topological,
|
||||||
|
room_id, token.topological, token.stream,
|
||||||
|
before_limit,
|
||||||
|
)
|
||||||
|
|
||||||
txn.execute(query_before, (room_id, before_limit))
|
query_after = (
|
||||||
|
"SELECT topological_ordering, stream_ordering, event_id FROM events"
|
||||||
|
" WHERE room_id = ? AND topological_ordering > ?"
|
||||||
|
" UNION ALL"
|
||||||
|
" SELECT topological_ordering, stream_ordering, event_id FROM events"
|
||||||
|
" WHERE room_id = ? AND topological_ordering = ? AND stream_ordering > ?"
|
||||||
|
" ORDER BY topological_ordering ASC, stream_ordering ASC LIMIT ?"
|
||||||
|
)
|
||||||
|
after_args = (
|
||||||
|
room_id, token.topological,
|
||||||
|
room_id, token.topological, token.stream,
|
||||||
|
after_limit,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
query_before = (
|
||||||
|
"SELECT topological_ordering, stream_ordering, event_id FROM events"
|
||||||
|
" WHERE room_id = ? AND %s"
|
||||||
|
" ORDER BY topological_ordering DESC, stream_ordering DESC LIMIT ?"
|
||||||
|
) % (upper_bound(token, self.database_engine, inclusive=False),)
|
||||||
|
|
||||||
|
before_args = (room_id, before_limit),
|
||||||
|
|
||||||
|
query_after = (
|
||||||
|
"SELECT topological_ordering, stream_ordering, event_id FROM events"
|
||||||
|
" WHERE room_id = ? AND %s"
|
||||||
|
" ORDER BY topological_ordering ASC, stream_ordering ASC LIMIT ?"
|
||||||
|
) % (lower_bound(token, self.database_engine, inclusive=False),)
|
||||||
|
|
||||||
|
after_args = (room_id, after_limit)
|
||||||
|
|
||||||
|
txn.execute(query_before, before_args)
|
||||||
|
|
||||||
rows = self.cursor_to_dict(txn)
|
rows = self.cursor_to_dict(txn)
|
||||||
events_before = [r["event_id"] for r in rows]
|
events_before = [r["event_id"] for r in rows]
|
||||||
@ -642,7 +678,7 @@ class StreamStore(SQLBaseStore):
|
|||||||
token.stream - 1,
|
token.stream - 1,
|
||||||
))
|
))
|
||||||
|
|
||||||
txn.execute(query_after, (room_id, after_limit))
|
txn.execute(query_after, after_args)
|
||||||
|
|
||||||
rows = self.cursor_to_dict(txn)
|
rows = self.cursor_to_dict(txn)
|
||||||
events_after = [r["event_id"] for r in rows]
|
events_after = [r["event_id"] for r in rows]
|
||||||
|
Loading…
Reference in New Issue
Block a user