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.

Medium Python 3.9+ Aug 9, 2026 Database scaling & optimization 13 views 0 copies

Python code

37 lines
Python 3.9+
import 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

stdout
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

  1. Use a real database like PostgreSQL and query `pg_indexes` or `information_schema` to inspect actual index definitions
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Database scaling & optimization

Related tutorials and quizzes for this topic.