Add migration for adding missing indexes

This commit adds indexes which were previously implicitly added by the
use of foreign keys on the id columns in the metadata tables. However,
after the last migration to convert uuid primary keys to integers the
foreign keys were all dropped. This commit adds these indexes back to
fix the performance issues with queries involving the metadata tables.
Additionally, the index on the run_at column in the runs table was
overlooked, so this migration adds that back as well.

Change-Id: I162b6daec1116937cf499bd56fde92c44590f1d4
This commit is contained in:
Matthew Treinish 2015-11-30 15:41:23 -05:00
parent 57a104ae37
commit 03bf76f28a
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
3 changed files with 76 additions and 7 deletions

View File

@ -0,0 +1,8 @@
---
upgrade:
- Contains a DB schema migration to add indexes that were incorrectly
dropped as part of 2822a408bdd0
fixes:
- Fixes performance issue on queries involving the metadata tables by
re-adding indexes that were accidently dropped

View File

@ -60,6 +60,7 @@ class Test(BASE, SubunitBase):
class Run(BASE, SubunitBase): class Run(BASE, SubunitBase):
__tablename__ = 'runs' __tablename__ = 'runs'
__table_args__ = (sa.Index('ix_run_at', 'run_at'),)
uuid = sa.Column(sa.String(36), uuid = sa.Column(sa.String(36),
default=lambda: six.text_type(uuid.uuid4())) default=lambda: six.text_type(uuid.uuid4()))
id = sa.Column(sa.BigInteger, primary_key=True) id = sa.Column(sa.BigInteger, primary_key=True)
@ -93,7 +94,8 @@ class TestRun(BASE, SubunitBase):
class RunMetadata(BASE, SubunitBase): class RunMetadata(BASE, SubunitBase):
__tablename__ = 'run_metadata' __tablename__ = 'run_metadata'
__table_args__ = (sa.Index('ix_run_metadata_run_id', 'run_id'),) __table_args__ = (sa.Index('ix_run_key_value', 'key', 'value'),
sa.Index('ix_run_id', 'run_id'))
id = sa.Column(sa.BigInteger, primary_key=True) id = sa.Column(sa.BigInteger, primary_key=True)
key = sa.Column(sa.String(255)) key = sa.Column(sa.String(255))
@ -103,8 +105,8 @@ class RunMetadata(BASE, SubunitBase):
class TestRunMetadata(BASE, SubunitBase): class TestRunMetadata(BASE, SubunitBase):
__tablename__ = 'test_run_metadata' __tablename__ = 'test_run_metadata'
__table_args__ = (sa.Index('ix_test_run_metadata_test_run_id', __table_args__ = (sa.Index('ix_test_run_key_value', 'key', 'value'),
'test_run_id'),) sa.Index('ix_test_run_id', 'test_run_id'))
id = sa.Column(sa.BigInteger, primary_key=True) id = sa.Column(sa.BigInteger, primary_key=True)
key = sa.Column(sa.String(255)) key = sa.Column(sa.String(255))
@ -114,8 +116,8 @@ class TestRunMetadata(BASE, SubunitBase):
class TestMetadata(BASE, SubunitBase): class TestMetadata(BASE, SubunitBase):
__tablename__ = 'test_metadata' __tablename__ = 'test_metadata'
__table_args__ = (sa.Index('ix_test_metadata_test_id', __table_args__ = (sa.Index('ix_test_key_value', 'key', 'value'),
'test_id'),) sa.Index('ix_test_id', 'test_id'))
id = sa.Column(sa.BigInteger, primary_key=True) id = sa.Column(sa.BigInteger, primary_key=True)
key = sa.Column(sa.String(255)) key = sa.Column(sa.String(255))
@ -125,8 +127,7 @@ class TestMetadata(BASE, SubunitBase):
class Attachments(BASE, SubunitBase): class Attachments(BASE, SubunitBase):
__tablename__ = 'attachments' __tablename__ = 'attachments'
__table_args__ = (sa.Index('ix_attachemnts_id', __table_args__ = (sa.Index('ix_attach_test_run_id', 'test_run_id'),)
'test_run_id'),)
id = sa.Column(sa.BigInteger, primary_key=True) id = sa.Column(sa.BigInteger, primary_key=True)
test_run_id = sa.Column(sa.BigInteger) test_run_id = sa.Column(sa.BigInteger)
label = sa.Column(sa.String(255)) label = sa.Column(sa.String(255))

View File

@ -0,0 +1,60 @@
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Add missing fk indexes
Revision ID: 35cd45895e56
Revises: 2822a408bdd0
Create Date: 2015-11-30 15:32:37.218171
"""
# revision identifiers, used by Alembic.
revision = '35cd45895e56'
down_revision = '2822a408bdd0'
from alembic import context
from alembic import op
from sqlalchemy.engine import reflection
def upgrade():
migration_context = context.get_context()
insp = reflection.Inspector(migration_context.bind)
test_run_meta_indx = insp.get_indexes('test_run_metadata')
run_meta_indx = insp.get_indexes('run_metadata')
test_meta_indx = insp.get_indexes('test_metadata')
runs_indx = insp.get_indexes('runs')
attach_indx = insp.get_indexes('attachments')
if 'run_id' not in [
x['column_names'][0] for x in run_meta_indx if len(x) == 1]:
op.create_index('ix_run_id', 'run_metadata', ['run_id'])
if 'test_id' not in [
x['column_names'][0] for x in test_meta_indx if len(x) == 1]:
op.create_index('ix_test_id', 'test_metadata', ['test_id'])
if 'test_run_id' not in [
x['column_names'][0] for x in test_run_meta_indx if len(x) == 1]:
op.create_index('ix_test_run_id', 'test_run_metadata', ['test_run_id'])
if 'run_at' not in [
x['column_names'][0] for x in runs_indx if len(x) == 1]:
op.create_index('ix_run_at', 'runs', ['run_at'])
if 'test_run_id' not in [
x['column_names'][0] for x in attach_indx if len(x) == 1]:
op.create_index('ix_attach_test_run_id', 'attachments',
['test_run_id'])
def downgrade():
NotImplementedError()