How to Time a Query Fairly: A Beginner's Method
Sooner or later you will want to know if one query is faster than another, or if an index helped. It is easy to fool yourself with one quick test. This page gives a simple method you can follow on a laptop with SQLite. It contains no results, because the only results that matter are yours.
Step 1: write down the question
One sentence is enough. For example: "Is finding books by author faster with an index on author_id?" Writing it down keeps the test focused.
Step 2: make enough data
Speed differences only show up with enough rows. A table with ten rows is fast whatever you do. You can generate practice rows with a small script:
import sqlite3, random
conn = sqlite3.connect("practice.db")
conn.execute("CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY, title TEXT, author_id INTEGER)")
rows = [(f"Book {i}", random.randint(1, 5000)) for i in range(200000)]
conn.executemany("INSERT INTO books (title, author_id) VALUES (?, ?)", rows)
conn.commit()
This creates a made-up table that is big enough to notice a difference.
Step 3: check both versions give the same answer
A faster query that returns different rows is just a wrong query. Run both versions and compare the row counts before you time anything.
Step 4: warm up
The first run of a query is often slower, because the computer is reading the database file from disk for the first time. Run each version once or twice before you start measuring.
Step 5: run each version several times
Use the sqlite3 shell with .timer on, or time it in Python:
import time
def run(sql, args):
start = time.perf_counter()
conn.execute(sql, args).fetchall()
return time.perf_counter() - start
times = [run("SELECT * FROM books WHERE author_id = ?", (42,)) for _ in range(20)]
times.sort()
print("middle value:", times[len(times)//2])
Taking the middle value (the median) of many runs is fairer than taking one run or the average, because one unusually slow run will not distort it.
Step 6: change one thing, then repeat
Add the index:
CREATE INDEX idx_books_author ON books(author_id);
Then repeat steps 4 and 5 exactly as before.
Step 7: write a short note
| Field | What to write |
|---|---|
| Question | Your sentence from step 1 |
| Data | How many rows, how they were made |
| Version A | The query and its median time |
| Version B | The change and its median time |
| Same results? | Yes or no |
| Conclusion | Faster, slower, or no clear difference |
Common ways to fool yourself
- Comparing a first (cold) run with a later (warm) run.
- Testing on a tiny table.
- Changing two things at once, so you do not know which one helped.
- Forgetting that an index makes inserts a little slower, as well as some reads faster.
When you want to go further, our sister site for working developers, databaseintegrate.com, covers benchmarking on larger systems.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
General commentary only: this page does not report specific news events and does not cite a survey or data source. Treat any trends described as unverified.