Skip to content

Commit

Permalink
Add index to reservations.uuid column
Browse files Browse the repository at this point in the history
Committing or rolling back reservations gets slow once a large number of
reservations have built up in the db.  Since they're being queried by
uuid this adds an index to speed up that operation.

Note that this can be slow with a large number of rows.

bug 1203872

Change-Id: Ida503206194a567a5c6811a304154d067c5faa83
  • Loading branch information
Andrew Laski committed Aug 1, 2013
1 parent 9ce78a9 commit 577d2ae
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
@@ -0,0 +1,41 @@
# 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 _quota_reservations_query
# from: nova/db/sqlalchemy/api.py
index = Index('reservations_uuid_idx', reservations.c.uuid)
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_uuid_idx', reservations.c.uuid)
index.drop(migrate_engine)
1 change: 1 addition & 0 deletions nova/db/sqlalchemy/models.py
Expand Up @@ -470,6 +470,7 @@ class Reservation(BASE, NovaBase):
__tablename__ = 'reservations'
__table_args__ = (
Index('ix_reservations_project_id', 'project_id'),
Index('reservations_uuid_idx', 'uuid'),
)
id = Column(Integer, primary_key=True, nullable=False)
uuid = Column(String(36), nullable=False)
Expand Down
23 changes: 23 additions & 0 deletions nova/tests/db/test_migrations.py
Expand Up @@ -2410,6 +2410,29 @@ def _post_downgrade_203(self, engine):
self.assertFalse('user_id' in reservation)
self.assertFalse(table_exist)

def _check_204(self, engine, data):
if engine.name != 'sqlite':
return

meta = sqlalchemy.MetaData()
meta.bind = engine
reservations = sqlalchemy.Table('reservations', meta, autoload=True)

index_data = [(idx.name, idx.columns.keys())
for idx in reservations.indexes]

if engine.name == "postgresql":
# we can not get correct order of columns in index
# definition to postgresql using sqlalchemy. So we sort
# columns list before compare
# bug http://www.sqlalchemy.org/trac/ticket/2767
self.assertIn(
('reservations_uuid_idx', sorted(['uuid'])),
([(idx[0], sorted(idx[1])) for idx in index_data])
)
else:
self.assertIn(('reservations_uuid_idx', ['uuid']), index_data)


class TestBaremetalMigrations(BaseMigrationTestCase, CommonTestsMixIn):
"""Test sqlalchemy-migrate migrations."""
Expand Down

0 comments on commit 577d2ae

Please sign in to comment.