Introduction
When databases grow large, finding data quickly becomes a challenge. Indexes help speed up searches, but choosing the right index is important to keep the database fast and efficient.
Imagine a large library where you want to find books quickly. If you know which shelves hold books on your favorite topics and the books are well organized by author and title, you find your book faster. But if the shelves are messy or you have to check every book, it takes longer.
┌───────────────────────────────┐ │ Query Patterns │ ├───────────────┬───────────────┤ │ High Selectivity Columns │ ├───────────────┼───────────────┤ │ Composite Indexes │ ├───────────────┼───────────────┤ │ Covering Indexes │ ├───────────────┼───────────────┤ │ Avoid Low Selectivity Columns │ ├───────────────┼───────────────┤ │ Balance Read/Write Performance │ └───────────────────────────────┘
import sqlite3 conn = sqlite3.connect(':memory:') cur = conn.cursor() # Create a sample table cur.execute('CREATE TABLE employees (id INTEGER, name TEXT, department TEXT, salary INTEGER)') # Insert sample data cur.executemany('INSERT INTO employees VALUES (?, ?, ?, ?)', [ (1, 'Alice', 'Sales', 70000), (2, 'Bob', 'HR', 50000), (3, 'Charlie', 'Sales', 60000), (4, 'Diana', 'IT', 80000), (5, 'Eve', 'IT', 75000) ]) # Create an index on department (low selectivity) and salary cur.execute('CREATE INDEX idx_dept_salary ON employees(department, salary)') # Query using the index cur.execute('SELECT name FROM employees WHERE department = ? AND salary > ?', ('IT', 70000)) for row in cur.fetchall(): print(row[0])