From b606d35c11f5fee022e9341e429fc137def437b8 Mon Sep 17 00:00:00 2001 From: Son Nguyen Kim Date: Fri, 20 Aug 2021 12:14:20 +0200 Subject: [PATCH] add pg_trgm index on Alias.note to speed up LIKE search --- app/models.py | 4 +++ .../versions/2021_082012_424808e1fe49_.py | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 migrations/versions/2021_082012_424808e1fe49_.py diff --git a/app/models.py b/app/models.py index 2512b40f..a14c1b5c 100644 --- a/app/models.py +++ b/app/models.py @@ -1136,6 +1136,10 @@ class Alias(db.Model, ModelMixin): __table_args__ = ( Index("ix_video___ts_vector__", ts_vector, postgresql_using="gin"), + # index on note column using pg_trgm + Index('note_pg_trgm_index', "note", + postgresql_ops={"note": "gin_trgm_ops"}, + postgresql_using='gin'), ) user = db.relationship(User, foreign_keys=[user_id]) diff --git a/migrations/versions/2021_082012_424808e1fe49_.py b/migrations/versions/2021_082012_424808e1fe49_.py new file mode 100644 index 00000000..2038c5e3 --- /dev/null +++ b/migrations/versions/2021_082012_424808e1fe49_.py @@ -0,0 +1,31 @@ +"""empty message + +Revision ID: 424808e1fe49 +Revises: d4392342465f +Create Date: 2021-08-20 12:11:57.901994 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '424808e1fe49' +down_revision = 'd4392342465f' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.execute('CREATE EXTENSION pg_trgm') + op.create_index('note_pg_trgm_index', 'alias', ['note'], unique=False, postgresql_ops={'note': 'gin_trgm_ops'}, postgresql_using='gin') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index('note_pg_trgm_index', table_name='alias') + op.execute('DROP EXTENSION pg_trgm') + # ### end Alembic commands ###