From 331a7a640b278cb3f841ed4f19a08f9dc15154f6 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 14 Feb 2019 17:21:01 -0500 Subject: [PATCH] Add some missing indexes that should make queries significantly faster --- data/database.py | 11 +++++-- ...c4de_add_lifetime_indexes_to_tag_tables.py | 32 +++++++++++++++++++ ...42242d_add_missing_index_on_uuid_fields.py | 28 ++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 data/migrations/versions/b9045731c4de_add_lifetime_indexes_to_tag_tables.py create mode 100644 data/migrations/versions/e184af42242d_add_missing_index_on_uuid_fields.py diff --git a/data/database.py b/data/database.py index a673c9a71..40bb53f9b 100644 --- a/data/database.py +++ b/data/database.py @@ -464,7 +464,7 @@ class BaseModel(ReadSlaveModel): class User(BaseModel): - uuid = CharField(default=uuid_generator, max_length=36, null=True) + uuid = CharField(default=uuid_generator, max_length=36, null=True, index=True) username = CharField(unique=True, index=True) password_hash = CharField(null=True) email = CharField(unique=True, index=True, @@ -728,7 +728,7 @@ class RepositoryPermission(BaseModel): class PermissionPrototype(BaseModel): org = QuayUserField(index=True, backref='orgpermissionproto') - uuid = CharField(default=uuid_generator) + uuid = CharField(default=uuid_generator, index=True) activating_user = QuayUserField(allows_robots=True, index=True, null=True, backref='userpermissionproto') delegate_user = QuayUserField(allows_robots=True, backref='receivingpermission', @@ -769,7 +769,7 @@ class DisableReason(BaseModel): class RepositoryBuildTrigger(BaseModel): - uuid = CharField(default=uuid_generator) + uuid = CharField(default=uuid_generator, index=True) service = ForeignKeyField(BuildTriggerService) repository = ForeignKeyField(Repository) connected_user = QuayUserField() @@ -923,6 +923,8 @@ class RepositoryTag(BaseModel): read_slaves = (read_slave,) indexes = ( (('repository', 'name'), False), + (('repository', 'lifetime_start_ts'), False), + (('repository', 'lifetime_end_ts'), False), # This unique index prevents deadlocks when concurrently moving and deleting tags (('repository', 'name', 'lifetime_end_ts'), True), @@ -1462,6 +1464,9 @@ class Tag(BaseModel): (('repository', 'name', 'hidden'), False), (('repository', 'name', 'tag_kind'), False), + (('repository', 'lifetime_start_ms'), False), + (('repository', 'lifetime_end_ms'), False), + # This unique index prevents deadlocks when concurrently moving and deleting tags (('repository', 'name', 'lifetime_end_ms'), True), ) diff --git a/data/migrations/versions/b9045731c4de_add_lifetime_indexes_to_tag_tables.py b/data/migrations/versions/b9045731c4de_add_lifetime_indexes_to_tag_tables.py new file mode 100644 index 000000000..021af750d --- /dev/null +++ b/data/migrations/versions/b9045731c4de_add_lifetime_indexes_to_tag_tables.py @@ -0,0 +1,32 @@ +"""Add lifetime end indexes to tag tables + +Revision ID: b9045731c4de +Revises: e184af42242d +Create Date: 2019-02-14 17:18:40.474310 + +""" + +# revision identifiers, used by Alembic. +revision = 'b9045731c4de' +down_revision = 'e184af42242d' + +from alembic import op + +def upgrade(tables, tester): + # ### commands auto generated by Alembic - please adjust! ### + op.create_index('repositorytag_repository_id_lifetime_end_ts', 'repositorytag', ['repository_id', 'lifetime_end_ts'], unique=False) + op.create_index('tag_repository_id_lifetime_end_ms', 'tag', ['repository_id', 'lifetime_end_ms'], unique=False) + + op.create_index('repositorytag_repository_id_lifetime_start_ts', 'repositorytag', ['repository_id', 'lifetime_start_ts'], unique=False) + op.create_index('tag_repository_id_lifetime_start_ms', 'tag', ['repository_id', 'lifetime_start_ms'], unique=False) + # ### end Alembic commands ### + + +def downgrade(tables, tester): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index('tag_repository_id_lifetime_end_ms', table_name='tag') + op.drop_index('repositorytag_repository_id_lifetime_end_ts', table_name='repositorytag') + + op.drop_index('tag_repository_id_lifetime_start_ms', table_name='tag') + op.drop_index('repositorytag_repository_id_lifetime_start_ts', table_name='repositorytag') + # ### end Alembic commands ### diff --git a/data/migrations/versions/e184af42242d_add_missing_index_on_uuid_fields.py b/data/migrations/versions/e184af42242d_add_missing_index_on_uuid_fields.py new file mode 100644 index 000000000..2dbd17385 --- /dev/null +++ b/data/migrations/versions/e184af42242d_add_missing_index_on_uuid_fields.py @@ -0,0 +1,28 @@ +"""Add missing index on UUID fields + +Revision ID: e184af42242d +Revises: 6ec8726c0ace +Create Date: 2019-02-14 16:35:47.768086 + +""" + +# revision identifiers, used by Alembic. +revision = 'e184af42242d' +down_revision = '6ec8726c0ace' + +from alembic import op + +def upgrade(tables, tester): + # ### commands auto generated by Alembic - please adjust! ### + op.create_index('permissionprototype_uuid', 'permissionprototype', ['uuid'], unique=False) + op.create_index('repositorybuildtrigger_uuid', 'repositorybuildtrigger', ['uuid'], unique=False) + op.create_index('user_uuid', 'user', ['uuid'], unique=False) + # ### end Alembic commands ### + + +def downgrade(tables, tester): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index('user_uuid', table_name='user') + op.drop_index('repositorybuildtrigger_uuid', table_name='repositorybuildtrigger') + op.drop_index('permissionprototype_uuid', table_name='permissionprototype') + # ### end Alembic commands ###