38 / 65 · 15 SQL & Database Testing · Migration Testing← prev⊞ allnext →☰ Read as one page
5.6Performance Considerations
Index Creation
Adding an index to a table with millions of rows can lock the table for minutes:
-- BAD: locks the table during index creation
CREATE INDEX idx_users_email ON users (email);
-- GOOD: creates the index without locking (PostgreSQL)
CREATE INDEX CONCURRENTLY idx_users_email ON users (email);
Testing Migration Performance
def test_migration_completes_in_time(migration_db):
"""Migration should complete within acceptable time."""
import time
conn, db_name = migration_db
# Load realistic data volume
cursor = conn.cursor()
cursor.execute("""
INSERT INTO users (name, email)
SELECT 'User ' || i, 'user' || i || '@test.com'
FROM generate_series(1, 100000) AS i
""")
conn.commit()
start = time.time()
subprocess.run(
f"psql {db_name} < migrations/0042_add_phone_column.sql",
shell=True, check=True
)
duration = time.time() - start
assert duration < 30, f"Migration took {duration:.1f}s (limit: 30s)"