Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index to speed up Message.find_recent query #862

Merged
merged 3 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mautrix_telegram/db/upgrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
v14_puppet_custom_mxid_index,
v15_backfill_anchor_id,
v16_backfill_type,
v17_message_find_recent,
)
3 changes: 2 additions & 1 deletion mautrix_telegram/db/upgrade/v00_latest_revision.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from mautrix.util.async_db import Connection, Scheme

latest_version = 16
latest_version = 17


async def create_latest_tables(conn: Connection, scheme: Scheme) -> int:
Expand Down Expand Up @@ -73,6 +73,7 @@ async def create_latest_tables(conn: Connection, scheme: Scheme) -> int:
UNIQUE (mxid, mx_room, tg_space)
)"""
)
await conn.execute("CREATE INDEX message_mx_room_and_tgid_idx ON message(mx_room, tgid DESC)")
await conn.execute(
"""CREATE TABLE reaction (
mxid TEXT NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@

@upgrade_table.register(description="Add index to puppet custom_mxid column")
async def upgrade_v14(conn: Connection) -> None:
await conn.execute("CREATE INDEX puppet_custom_mxid_idx ON puppet(custom_mxid)")
await conn.execute("CREATE INDEX IF NOT EXISTS puppet_custom_mxid_idx ON puppet(custom_mxid)")
25 changes: 25 additions & 0 deletions mautrix_telegram/db/upgrade/v17_message_find_recent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# mautrix-telegram - A Matrix-Telegram puppeting bridge
# Copyright (C) 2022 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from mautrix.util.async_db import Connection

from . import upgrade_table


@upgrade_table.register(description="Add index for Message.find_recent")
async def upgrade_v17(conn: Connection) -> None:
await conn.execute(
"CREATE INDEX IF NOT EXISTS message_mx_room_and_tgid_idx ON message(mx_room, tgid DESC)"
)