Fix #894 - index User.username field
authorJessica Tallon <jessica@megworld.co.uk>
Thu, 17 Jul 2014 13:58:24 +0000 (14:58 +0100)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Thu, 17 Jul 2014 15:21:29 +0000 (10:21 -0500)
This commit sponsored by Emily O'Leary.  Thank you!

mediagoblin/db/migrations.py
mediagoblin/db/models.py

index 8e0b5096c9c58cbe84db1236d0694ea399579ca6..59aec4d2f1f6df989da9c7be73eee4d1f6587e62 100644 (file)
@@ -19,7 +19,7 @@ import uuid
 
 from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger,
                         Integer, Unicode, UnicodeText, DateTime,
-                        ForeignKey, Date)
+                        ForeignKey, Date, Index)
 from sqlalchemy.exc import ProgrammingError
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.sql import and_
@@ -789,3 +789,17 @@ def fix_privilege_user_association_table(db):
         privilege_user_assoc.c.core__privilege_id.alter(name="user")
 
     db.commit()
+
+@RegisterMigration(22, MIGRATIONS)
+def add_index_username_field(db):
+    """
+    This indexes the User.username field which is frequently queried
+    for example a user logging in. This solves the issue #894
+    """
+    metadata = MetaData(bind=db.bind)
+    user_table = inspect_table(metadata, "core__users")
+
+    new_index = Index("ix_core__users_uploader", user_table.c.username)
+    new_index.create()
+
+    db.commit()
index e388bd5b5f137cb6e6e9a54f15524442c2a9f9d5..643d5d41ba3e7aa5068cf2dc0c05d5b0ea72358a 100644 (file)
@@ -57,7 +57,7 @@ class User(Base, UserMixin):
     __tablename__ = "core__users"
 
     id = Column(Integer, primary_key=True)
-    username = Column(Unicode, nullable=False, unique=True)
+    username = Column(Unicode, nullable=False, unique=True, index=True)
     # Note: no db uniqueness constraint on email because it's not
     # reliable (many email systems case insensitive despite against
     # the RFC) and because it would be a mess to implement at this