From 6d8d00fea1021cb50087bebf051b7795cda1978d Mon Sep 17 00:00:00 2001 From: Carl Baldwin Date: Mon, 6 Jun 2016 15:00:23 -0600 Subject: [PATCH] Add index on trunk_id in the subports model An index on trunk_id will make queries that return subports from a given trunk_id efficient. When I first approved the trunk and subports model, I thought that having trunk_id in the primary key would be sufficient. However, thinking about it some more, I realize that isn't the case because trunk_id is listed as the second column in the primary key. Think about it like searching a string index for something that matches in the last half of a string. The index doesn't make that efficient. The DB must still search all entries for matches. It is the same with the index on (port_id, trunk_id) that the primary key provides. You need to look through every row to find matching trunk_ids. Hence the addition of an index. Change-Id: I566863fd44aa402230d2eb83e99bc66e7fed8d80 Partially-Implements: blueprint vlan-aware-vms --- .../alembic_migrations/versions/EXPAND_HEAD | 2 +- ...a3_add_index_to_trunk_model_and_adjust_.py | 25 +++++++++++++++++++ neutron/services/trunk/models.py | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 neutron/db/migration/alembic_migrations/versions/newton/expand/0e877ec3cba3_add_index_to_trunk_model_and_adjust_.py diff --git a/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD b/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD index cf26eb1f33e..11bc08fbd7c 100644 --- a/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD +++ b/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD @@ -1 +1 @@ -30107ab6a3ee +0e877ec3cba3 diff --git a/neutron/db/migration/alembic_migrations/versions/newton/expand/0e877ec3cba3_add_index_to_trunk_model_and_adjust_.py b/neutron/db/migration/alembic_migrations/versions/newton/expand/0e877ec3cba3_add_index_to_trunk_model_and_adjust_.py new file mode 100644 index 00000000000..c278d27f261 --- /dev/null +++ b/neutron/db/migration/alembic_migrations/versions/newton/expand/0e877ec3cba3_add_index_to_trunk_model_and_adjust_.py @@ -0,0 +1,25 @@ +# 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 index on trunk_id to subports model """ + +revision = '0e877ec3cba3' +down_revision = '30107ab6a3ee' + +from alembic import op + + +def upgrade(): + op.create_index('ix_subports_trunk_id', + 'subports', + ['trunk_id'], + unique=False) diff --git a/neutron/services/trunk/models.py b/neutron/services/trunk/models.py index 89b45acaa0f..dc7c562db2f 100644 --- a/neutron/services/trunk/models.py +++ b/neutron/services/trunk/models.py @@ -56,6 +56,7 @@ class SubPort(model_base.BASEV2): segmentation_id = sa.Column(sa.Integer, nullable=False) __table_args__ = ( + sa.Index('ix_subports_trunk_id', 'trunk_id'), sa.UniqueConstraint( 'trunk_id', 'segmentation_type',