From 97a963b4bf2d992639f8b29fbb1df1dd2b9f6566 Mon Sep 17 00:00:00 2001 From: rkuo-danswer Date: Wed, 8 Jan 2025 16:56:55 -0800 Subject: [PATCH] add index to speed up get last attempt (#3636) * add index to speed up get last attempt * use descending order * put back unique param * how did this not get formatted? --------- Co-authored-by: Richard Kuo (Danswer) --- ..._add_composite_index_for_index_attempt_.py | 35 +++++++++++++++++++ backend/onyx/db/models.py | 8 +++++ 2 files changed, 43 insertions(+) create mode 100644 backend/alembic/versions/369644546676_add_composite_index_for_index_attempt_.py diff --git a/backend/alembic/versions/369644546676_add_composite_index_for_index_attempt_.py b/backend/alembic/versions/369644546676_add_composite_index_for_index_attempt_.py new file mode 100644 index 0000000000..4e0384fe48 --- /dev/null +++ b/backend/alembic/versions/369644546676_add_composite_index_for_index_attempt_.py @@ -0,0 +1,35 @@ +"""add composite index for index attempt time updated + +Revision ID: 369644546676 +Revises: 2955778aa44c +Create Date: 2025-01-08 15:38:17.224380 + +""" +from alembic import op +from sqlalchemy import text + +# revision identifiers, used by Alembic. +revision = "369644546676" +down_revision = "2955778aa44c" +branch_labels: None = None +depends_on: None = None + + +def upgrade() -> None: + op.create_index( + "ix_index_attempt_ccpair_search_settings_time_updated", + "index_attempt", + [ + "connector_credential_pair_id", + "search_settings_id", + text("time_updated DESC"), + ], + unique=False, + ) + + +def downgrade() -> None: + op.drop_index( + "ix_index_attempt_ccpair_search_settings_time_updated", + table_name="index_attempt", + ) diff --git a/backend/onyx/db/models.py b/backend/onyx/db/models.py index 87370e91f6..ff1c98d13d 100644 --- a/backend/onyx/db/models.py +++ b/backend/onyx/db/models.py @@ -18,6 +18,7 @@ from fastapi_users_db_sqlalchemy.access_token import SQLAlchemyBaseAccessTokenTa from fastapi_users_db_sqlalchemy.generics import TIMESTAMPAware from sqlalchemy import Boolean from sqlalchemy import DateTime +from sqlalchemy import desc from sqlalchemy import Enum from sqlalchemy import Float from sqlalchemy import ForeignKey @@ -813,6 +814,13 @@ class IndexAttempt(Base): "connector_credential_pair_id", "time_created", ), + Index( + "ix_index_attempt_ccpair_search_settings_time_updated", + "connector_credential_pair_id", + "search_settings_id", + desc("time_updated"), + unique=False, + ), ) def __repr__(self) -> str: