import sqlite3
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute('CREATE TABLE employees (id INTEGER, name TEXT, dept TEXT)')
cur.executemany('INSERT INTO employees VALUES (?, ?, ?)', [
(1, 'Alice', 'Sales'),
(2, 'Bob', 'HR'),
(3, 'Charlie', 'Sales'),
(4, 'Diana', 'IT')
])
cur.execute('CREATE INDEX idx_dept ON employees(dept)')
# Query without optimization hint
cur.execute('SELECT name FROM employees WHERE dept = "Sales"')
print("Without optimization:")
for row in cur.fetchall():
print(row[0])
# Query with an explicit index usage (SQLite uses index automatically here)
cur.execute('SELECT name FROM employees WHERE dept = "Sales"')
print("\nWith index optimization:")
for row in cur.fetchall():
print(row[0])