Adding an index on token.user_id and token.trust_id

This is a performance update to ensure that we are scanning the fewest
number of rows on a user delete (causing token revocations). Without
these indexes it is possible to scan all valid tokens, causing
significant overhead, to find the user or trust matching tokens.

Due to selecting the extra column (needed for other matches in
some cases) this can also cause issues with buffer pool sizes.

Change-Id: I202b5c87a221d8dba99d16b0a1baa7546fef093b
Closes-Bug: 1332666
This commit is contained in:
galstrom21 2014-06-23 17:30:03 -05:00 committed by Morgan Fainberg
parent 08416ac2b7
commit 4a462a47fc
3 changed files with 54 additions and 1 deletions

View File

@ -0,0 +1,35 @@
# 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 indexes to `user_id` and `trust_id` columns for the `token` table."""
import sqlalchemy as sql
def upgrade(migrate_engine):
meta = sql.MetaData()
meta.bind = migrate_engine
token = sql.Table('token', meta, autoload=True)
sql.Index('ix_token_user_id', token.c.user_id).create()
sql.Index('ix_token_trust_id', token.c.trust_id).create()
def downgrade(migrate_engine):
meta = sql.MetaData()
meta.bind = migrate_engine
token = sql.Table('token', meta, autoload=True)
sql.Index('ix_token_user_id', token.c.user_id).drop()
sql.Index('ix_token_trust_id', token.c.trust_id).drop()

View File

@ -1393,6 +1393,22 @@ class SqlUpgradeTests(SqlMigrateBase):
index_data = [(idx.name, idx.columns.keys()) for idx in table.indexes]
self.assertIn(('ix_actor_id', ['actor_id']), index_data)
def test_token_user_id_and_trust_id_index_upgrade(self):
self.upgrade(54)
self.upgrade(55)
table = sqlalchemy.Table('token', self.metadata, autoload=True)
index_data = [(idx.name, idx.columns.keys()) for idx in table.indexes]
self.assertIn(('ix_token_user_id', ['user_id']), index_data)
self.assertIn(('ix_token_trust_id', ['trust_id']), index_data)
def test_token_user_id_and_trust_id_index_downgrade(self):
self.upgrade(55)
self.downgrade(54)
table = sqlalchemy.Table('token', self.metadata, autoload=True)
index_data = [(idx.name, idx.columns.keys()) for idx in table.indexes]
self.assertNotIn(('ix_token_user_id', ['user_id']), index_data)
self.assertNotIn(('ix_token_trust_id', ['trust_id']), index_data)
def test_remove_actor_id_index(self):
self.upgrade(54)
self.downgrade(53)

View File

@ -41,7 +41,9 @@ class TokenModel(sql.ModelBase, sql.DictBase):
trust_id = sql.Column(sql.String(64))
__table_args__ = (
sql.Index('ix_token_expires', 'expires'),
sql.Index('ix_token_expires_valid', 'expires', 'valid')
sql.Index('ix_token_expires_valid', 'expires', 'valid'),
sql.Index('ix_token_user_id', 'user_id'),
sql.Index('ix_token_trust_id', 'trust_id')
)