Composite index leftmost prefix in Python
Simulate a composite index in SQLite and check whether query columns match the leftmost prefix rule for index usage.
Python code
37 linesimport sqlite3
def get_indexed_columns(table_name):
"""Simulate a composite index by reading column names that start with 'idx_'."""
conn = sqlite3.connect(":memory:")
conn.execute(f"CREATE TABLE {table_name} (id INTEGER, idx_col1 TEXT, idx_col2 INTEGER, other TEXT)")
conn.execute(f"CREATE INDEX idx_composite ON {table_name} (idx_col1, idx_col2)")
cursor = conn.execute("SELECT sql FROM sqlite_master WHERE name='idx_composite'")
index_sql = cursor.fetchone()[0]
columns_part = index_sql.split("(")[1].rstrip(")")
columns = [c.strip() for c in columns_part.split(",")]
conn.close()
return columns
def matches_leftmost_prefix(available_columns, query_columns):
"""Return True if query_columns form a leftmost prefix of the index columns."""
if not query_columns:
return False
return query_columns == available_columns[: len(query_columns)]
if __name__ == "__main__":
index_cols = get_indexed_columns("sample")
print("Index columns:", index_cols)
test_queries = [
["idx_col1"],
["idx_col1", "idx_col2"],
["idx_col2"],
["idx_col2", "idx_col1"],
["idx_col1", "other"],
]
for q in test_queries:
print(f"{q}: {matches_leftmost_prefix(index_cols, q)}")
Output
Index columns: ['idx_col1', 'idx_col2']
['idx_col1']: True
['idx_col1', 'idx_col2']: True
['idx_col2']: False
['idx_col2', 'idx_col1']: False
['idx_col1', 'other']: False
How it works
The get_indexed_columns function creates an in-memory SQLite table with a composite index and parses the index definition from sqlite_master to extract column names. The matches_leftmost_prefix function compares the query columns against the first N columns of the index, where N is the number of query columns. This mirrors how databases use composite indexes: queries can only exploit the index when they start with the leftmost column and follow the index order. The code demonstrates that ['idx_col2'] and ['idx_col2', 'idx_col1'] fail because they skip the leading column, while ['idx_col1', 'other'] fails because other is not part of the index.
Common mistakes
- Forgetting that the leftmost prefix must include the first column of the index to be useful
- Assuming any subset of composite index columns can be used independently
- Confusing column order in the query with index order — the query must match the index prefix exactly
Variations
- Use a real database like PostgreSQL and query `pg_indexes` or `information_schema` to inspect actual index definitions
- Implement a more generic parser that handles multi-column indexes with various SQL dialects
Real-world use cases
- Analyzing query performance by verifying which indexed columns a given WHERE clause actually uses
- Building a database schema validation tool that flags queries that miss the leftmost prefix and force full scans
- Automating index review in a migration pipeline by testing candidate query patterns against composite indexes
Sponsored
More from Database scaling & optimization
- Approximate Count with HyperLogLog in Python medium
- B-Tree Insert and In-Order Traversal in Python hard
- Broadcast a Small Reference Table in Python easy
- Build a Full Text Search Index in Python medium
- Build a Partial Index Mock in Python for Database Filtering easy
- Consistent Hashing with Virtual Buckets in Python medium
Keep learning
Related tutorials and quizzes for this topic.