From f4a1646a8bdf6906d7e49714781b9abe6c6fe22d Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 15 Feb 2019 16:11:25 -0500 Subject: [PATCH] Add an index on the logs_archived boolean on a RepositoryBuild Addresses the slow query: ``` SELECT `candidates` . `id` FROM ( SELECT `t1` . `id` FROM `repositorybuild` AS `t1` WHERE ( ( ( `t1` . `phase` IN (...) ) OR ( `t1` . `started` < ? ) ) AND ( `t1` . `logs_archived` = ? ) ) LIMIT ? ) AS `candidates` ORDER BY `Rand` ( ) LIMIT ? OFFSET ? ``` While the cardinality on `logs_archived` will be low, it should also only be in the `false` state for a very small number of records, so it should make this query very, very fast. --- data/database.py | 2 +- ...23ba00ba_add_index_on_logs_archived_on_.py | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 data/migrations/versions/481623ba00ba_add_index_on_logs_archived_on_.py diff --git a/data/database.py b/data/database.py index 40bb53f9b..a2fb8d632 100644 --- a/data/database.py +++ b/data/database.py @@ -997,7 +997,7 @@ class RepositoryBuild(BaseModel): trigger = ForeignKeyField(RepositoryBuildTrigger, null=True) pull_robot = QuayUserField(null=True, backref='buildpullrobot', allows_robots=True, robot_null_delete=True) - logs_archived = BooleanField(default=False) + logs_archived = BooleanField(default=False, index=True) queue_id = CharField(null=True, index=True) class Meta: diff --git a/data/migrations/versions/481623ba00ba_add_index_on_logs_archived_on_.py b/data/migrations/versions/481623ba00ba_add_index_on_logs_archived_on_.py new file mode 100644 index 000000000..72435b47b --- /dev/null +++ b/data/migrations/versions/481623ba00ba_add_index_on_logs_archived_on_.py @@ -0,0 +1,24 @@ +"""Add index on logs_archived on repositorybuild + +Revision ID: 481623ba00ba +Revises: b9045731c4de +Create Date: 2019-02-15 16:09:47.326805 + +""" + +# revision identifiers, used by Alembic. +revision = '481623ba00ba' +down_revision = 'b9045731c4de' + +from alembic import op + +def upgrade(tables, tester): + # ### commands auto generated by Alembic - please adjust! ### + op.create_index('repositorybuild_logs_archived', 'repositorybuild', ['logs_archived'], unique=False) + # ### end Alembic commands ### + + +def downgrade(tables, tester): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index('repositorybuild_logs_archived', table_name='repositorybuild') + # ### end Alembic commands ###