-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat(db): index tables to improve study search and sorting performance (
#1902) Merge pull request #1902 from AntaresSimulatorTeam/feature/1035-index-study-table-search-engine
Showing
5 changed files
with
161 additions
and
25 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
alembic/versions/1f5db5dfad80_add_indexes_to_study_tables.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# noinspection SpellCheckingInspection | ||
""" | ||
Add indexes to Study tables | ||
The goal of this migration is to add indexes on the `study`, `rawstudy` and `study_additional_data` tables, | ||
in order to speed up data search queries for the search engine. | ||
Revision ID: 1f5db5dfad80 | ||
Revises: 782a481f3414 | ||
Create Date: 2024-01-19 18:37:34.155199 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa # type: ignore | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
# noinspection SpellCheckingInspection | ||
revision = "1f5db5dfad80" | ||
down_revision = "782a481f3414" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
# noinspection SpellCheckingInspection | ||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table("rawstudy", schema=None) as batch_op: | ||
batch_op.alter_column("workspace", existing_type=sa.VARCHAR(length=255), nullable=False) | ||
batch_op.create_index(batch_op.f("ix_rawstudy_missing"), ["missing"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_rawstudy_workspace"), ["workspace"], unique=False) | ||
|
||
with op.batch_alter_table("study", schema=None) as batch_op: | ||
batch_op.create_index(batch_op.f("ix_study_archived"), ["archived"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_study_created_at"), ["created_at"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_study_folder"), ["folder"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_study_name"), ["name"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_study_owner_id"), ["owner_id"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_study_parent_id"), ["parent_id"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_study_type"), ["type"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_study_updated_at"), ["updated_at"], unique=False) | ||
batch_op.create_index(batch_op.f("ix_study_version"), ["version"], unique=False) | ||
|
||
with op.batch_alter_table("study_additional_data", schema=None) as batch_op: | ||
batch_op.create_index(batch_op.f("ix_study_additional_data_patch"), ["patch"], unique=False) | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
# noinspection SpellCheckingInspection | ||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table("study_additional_data", schema=None) as batch_op: | ||
batch_op.drop_index(batch_op.f("ix_study_additional_data_patch")) | ||
|
||
with op.batch_alter_table("study", schema=None) as batch_op: | ||
batch_op.drop_index(batch_op.f("ix_study_version")) | ||
batch_op.drop_index(batch_op.f("ix_study_updated_at")) | ||
batch_op.drop_index(batch_op.f("ix_study_type")) | ||
batch_op.drop_index(batch_op.f("ix_study_parent_id")) | ||
batch_op.drop_index(batch_op.f("ix_study_owner_id")) | ||
batch_op.drop_index(batch_op.f("ix_study_name")) | ||
batch_op.drop_index(batch_op.f("ix_study_folder")) | ||
batch_op.drop_index(batch_op.f("ix_study_created_at")) | ||
batch_op.drop_index(batch_op.f("ix_study_archived")) | ||
|
||
with op.batch_alter_table("rawstudy", schema=None) as batch_op: | ||
batch_op.drop_index(batch_op.f("ix_rawstudy_workspace")) | ||
batch_op.drop_index(batch_op.f("ix_rawstudy_missing")) | ||
batch_op.alter_column("workspace", existing_type=sa.VARCHAR(length=255), nullable=True) | ||
|
||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Test the database model. | ||
""" | ||
import uuid | ||
|
||
from sqlalchemy import inspect # type: ignore | ||
from sqlalchemy.engine import Engine # type: ignore | ||
from sqlalchemy.orm import Session # type: ignore | ||
|
||
from antarest.study.model import Study | ||
|
||
|
||
# noinspection SpellCheckingInspection | ||
class TestStudy: | ||
""" | ||
Test the study model. | ||
""" | ||
|
||
def test_study(self, db_session: Session) -> None: | ||
""" | ||
Basic test of the `study` table. | ||
""" | ||
study_id = uuid.uuid4() | ||
|
||
with db_session: | ||
db_session.add(Study(id=str(study_id), name="Study 1")) | ||
db_session.commit() | ||
|
||
with db_session: | ||
study = db_session.query(Study).first() | ||
assert study.id == str(study_id) | ||
assert study.name == "Study 1" | ||
|
||
def test_index_on_study(self, db_engine: Engine) -> None: | ||
inspector = inspect(db_engine) | ||
indexes = inspector.get_indexes("study") | ||
index_names = {index["name"] for index in indexes} | ||
assert index_names == { | ||
"ix_study_archived", | ||
"ix_study_created_at", | ||
"ix_study_folder", | ||
"ix_study_name", | ||
"ix_study_owner_id", | ||
"ix_study_parent_id", | ||
"ix_study_type", | ||
"ix_study_updated_at", | ||
"ix_study_version", | ||
} | ||
|
||
def test_index_on_rawstudy(self, db_engine: Engine) -> None: | ||
inspector = inspect(db_engine) | ||
indexes = inspector.get_indexes("rawstudy") | ||
index_names = {index["name"] for index in indexes} | ||
assert index_names == {"ix_rawstudy_workspace", "ix_rawstudy_missing"} | ||
|
||
def test_index_on_variantstudy(self, db_engine: Engine) -> None: | ||
inspector = inspect(db_engine) | ||
indexes = inspector.get_indexes("variantstudy") | ||
index_names = {index["name"] for index in indexes} | ||
assert not index_names | ||
|
||
def test_index_on_study_additional_data(self, db_engine: Engine) -> None: | ||
inspector = inspect(db_engine) | ||
indexes = inspector.get_indexes("study_additional_data") | ||
index_names = {index["name"] for index in indexes} | ||
assert index_names == {"ix_study_additional_data_patch"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters