An index speeds a lookup. EXPLAIN QUERY PLAN shows whether SQLite uses it. On tiny tables the plan is the lesson, not the milliseconds.
Goal
Print the query plan before and after CREATE INDEX.
import sqlite3
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE t (city TEXT, units INTEGER)")
con.executemany("INSERT INTO t VALUES (?, ?)", [("Nairobi", i) for i in range(20)])
print(con.execute("EXPLAIN QUERY PLAN SELECT * FROM t WHERE city = 'Nairobi'").fetchall())
con.close()import sqlite3
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE t (city TEXT, units INTEGER)")
con.execute("CREATE INDEX idx_city ON t(city)")
con.executemany("INSERT INTO t VALUES (?, ?)", [("Nairobi", i) for i in range(20)])
print(con.execute("EXPLAIN QUERY PLAN SELECT * FROM t WHERE city = 'Nairobi'").fetchall())
con.close()import sqlite3
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE t (city TEXT)")
con.execute("CREATE INDEX idx_city ON t(city)")
print(con.execute("SELECT name FROM sqlite_master WHERE type='index'").fetchall())
con.close()import sqlite3
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE t (city TEXT UNIQUE)")
print(con.execute("SELECT sql FROM sqlite_master WHERE type='table'").fetchone())
con.close()