Tech
Database Indexing and Query Optimization: Stop the Production Bottleneck
Learn how database indexes work, the trade-offs between read and write speeds, and practical strategies to optimize SQL queries using EXPLAIN to eliminate full table scans.
June 2026 · 6 min read · 2 views · 0 hearts
Advertisement
Stop letting your database be the bottleneck of your application. It starts with a simple query that runs in milliseconds during development, only to crawl to a halt once you hit 100,000 rows in production.
The difference between a sluggish app and a high-performance system usually comes down to how you handle database indexes and query optimization.
What Exactly is a Database Index?
Think of a database index like the index at the back of a massive technical book. If you want to find every mention of "Decorators" in a 1,000-page Python manual, you don't flip through every single page from the start (that's a Full Table Scan). Instead, you flip to the index, find the word "Decorators," grab the page numbers, and jump straight to the relevant content.
In technical terms, an index is a separate data structure (usually a B-Tree) that stores a sorted version of a specific column and a pointer to the actual row in the table.
The Trade-off: Read Speed vs. Write Speed
Indexes aren't free. While they make SELECT queries lightning fast, they slow down INSERT, UPDATE, and DELETE operations. This is because every time you change data in the table, the database must also update the corresponding index to keep it sorted.
Rule of thumb: Index columns that appear frequently in WHERE clauses, JOIN conditions, and ORDER BY statements, but avoid indexing columns with very low cardinality (like a "Gender" column with only two options).
Types of Indexes You Should Know
1. Primary Index (Clustered)
The primary key is automatically indexed. In most engines (like MySQL InnoDB), the table is physically stored on disk in the order of the primary key. You can only have one clustered index per table.
2. Secondary Index (Non-Clustered)
These are indexes you create manually on other columns. They don't change how the data is stored; they just create a "lookup table" that points back to the primary key.
3. Composite Index
A composite index covers multiple columns. For example, if you frequently query WHERE first_name = 'Jane' AND last_name = 'Doe', a composite index on (last_name, first_name) is significantly faster than two separate indexes.
Important: The order of columns in a composite index matters. An index on (last_name, first_name) helps queries for "Last Name" or "Last Name + First Name," but it generally won't help a query looking for only "First Name."
How to Optimize Your Queries
Indexing is the foundation, but how you write your SQL (or ORM code) determines if the database actually uses those indexes.
Stop Using SELECT *
Fetching every column when you only need the username forces the database to go back to the main table to retrieve data. If your index contains all the columns requested in the SELECT clause, the database can perform a Covering Index scan, returning the result directly from the index without ever touching the actual table.
Avoid Functions on Indexed Columns
This is a common performance killer. Consider this query:
SELECT * FROM users WHERE YEAR(created_at) = 2023;
Because you wrapped created_at in a function, the database cannot use the index on that column; it must calculate the year for every single row. Instead, use a range:
SELECT * FROM users WHERE created_at >= '2023-01-01' AND created_at <= '2023-12-31';
Be Wary of Wildcards
Using LIKE '%term' (a leading wildcard) forces a full table scan because the database doesn't know where the string starts. However, LIKE 'term%' (a trailing wildcard) can still leverage an index.
The Secret Weapon: EXPLAIN
You don't have to guess why a query is slow. Every major database provides an EXPLAIN command. By prefixing your query with EXPLAIN (or EXPLAIN ANALYZE in PostgreSQL/MySQL), the database tells you its execution plan:
- Scan Type: Is it doing an
Index Scan(fast) or aFull Table Scan(slow)? - Rows: How many rows did the database have to examine to find the result?
- Key: Which index did it actually choose to use?
Optimization Checklist for Developers
When a page feels slow, follow this workflow:
1. Identify the slow query using slow-query logs or an APM tool.
2. Run EXPLAIN to see if the database is performing a full table scan.
3. Check for missing indexes on columns used in JOIN or WHERE clauses.
4. Refactor the query to remove functions on columns or unnecessary SELECT * calls.
5. Test the fix by running EXPLAIN again to confirm the "type" has changed from ALL to ref or range.
Advertisement
Comments
Questions, corrections, and tips stay visible for everyone reading this page.
Join the discussion
No comments yet
Be the first to leave a note — it helps the next reader.