Add index for reservations on (deleted, expire)

the query for expire_reservations currently does a full table scan.
This adds an index so frequent invocations of expire does not bog
down the database.

Change-Id: Ic6f6e4262746753d869ad37b8aaaa5ffc1c4efef
Resolves-bug: 1348720
This commit is contained in:
Vishvananda Ishaya 2014-07-25 10:15:27 -07:00
parent 2f09c3031e
commit 4d56b91628
2 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,43 @@
# All Rights Reserved.
#
# 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.
from sqlalchemy import Index, MetaData, Table
from sqlalchemy.exc import IntegrityError
def upgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
reservations = Table('reservations', meta, autoload=True)
# Based on expire_reservations query
# from: cinder/db/sqlalchemy/api.py
index = Index('reservations_deleted_expire_idx',
reservations.c.deleted, reservations.c.expire)
try:
index.create(migrate_engine)
except IntegrityError:
pass
def downgrade(migrate_engine):
meta = MetaData()
meta.bind = migrate_engine
reservations = Table('reservations', meta, autoload=True)
index = Index('reservations_deleted_expire_idx',
reservations.c.deleted, reservations.c.expire)
index.drop(migrate_engine)

View File

@ -1071,3 +1071,36 @@ class TestMigrations(test.TestCase):
metadata,
autoload=True)
self.assertNotIn('disabled_reason', services.c)
def test_migration_023(self):
"""Test that adding reservations index works correctly."""
for (key, engine) in self.engines.items():
migration_api.version_control(engine,
TestMigrations.REPOSITORY,
migration.db_initial_version())
migration_api.upgrade(engine, TestMigrations.REPOSITORY, 22)
metadata = sqlalchemy.schema.MetaData()
metadata.bind = engine
migration_api.upgrade(engine, TestMigrations.REPOSITORY, 23)
reservations = sqlalchemy.Table('reservations',
metadata,
autoload=True)
index_colums = []
for idx in reservations.indexes:
if idx.name == 'reservations_deleted_expire_idx':
index_columns = idx.columns.keys()
break
self.assertEqual(sorted(['deleted', 'expire']),
sorted(index_columns))
migration_api.downgrade(engine, TestMigrations.REPOSITORY, 22)
metadata = sqlalchemy.schema.MetaData()
metadata.bind = engine
reservations = sqlalchemy.Table('reservations',
metadata,
autoload=True)
index_names = [idx.name for idx in reservations.indexes]
self.assertNotIn('reservations_deleted_expire_idx', index_names)