What an index does
An index stores a sorted structure that lets MySQL find rows without scanning the whole table. Primary keys and unique constraints create indexes automatically. Secondary indexes are added for the columns used in filtering, joining and sorting.
The order of columns in a composite index matters. A query that filters on the first column can use the index; a query that filters only on the second may not. Design the index around the query pattern, not around the table columns in isolation.
- Indexes speed up reads but add write cost
- Composite index order follows the query
- Primary and unique keys are indexed automatically
- Indexes consume storage and memory
Read the query plan
Use EXPLAIN to see how MySQL intends to execute a query. Look at the access type, the keys used, the estimated rows and whether a filesort or temporary table appears. A full table scan is not always wrong, especially on small tables, but it should be understood.
Measure with realistic data. A query that is fast on a development database with a hundred rows may behave very differently with a million. The query plan and the data volume together tell the real story.
- Use EXPLAIN before adding indexes
- Check access type, keys and estimated rows
- Watch for filesort and temporary tables
- Test with realistic data volumes
When indexes hurt
Every index must be maintained on insert, update and delete. A table with many unused indexes pays a write cost for no read benefit. Low-selectivity columns, such as a boolean flag, are often poor standalone index candidates.
Indexes also consume memory. A large number of indexes can push useful data out of the buffer pool. Review index usage and remove the ones that no query relies on.
- Avoid unused and duplicate indexes
- Low-selectivity columns rarely help alone
- Index maintenance affects write performance
- Review index usage periodically
Maintenance and measurement
Change one thing at a time and measure the query before and after. Check the effect on write performance and on other queries, not only the one you were fixing. Keep a record of the query, the plan and the change.
Statistics and table growth change over time. A query that was fine last year may need a new plan or index now. Monitoring slow queries is more useful than a one-off optimisation.
- Change one variable and measure
- Check read and write impact
- Record the query and the reason for the index
- Monitor slow queries as data grows
Key takeaways
- Design indexes around real queries, not assumptions.
- Read the query plan before adding an index.
- Every index has a write and storage cost.
- Measure the effect and revisit as data grows.
Need help
Related services.
MySQL Database Development
Relational database design, queries, indexes and migrations for applications that depend on dependable data.
Backend Development
Server-side systems for authentication, data, business rules, integrations and background work.
PHP & Laravel Development
PHP and Laravel back-ends for content platforms, portals, integrations and business systems.
Questions
About this guide.
Should every foreign key have an index?
Often, yes, because joins and lookups frequently use them. The final decision depends on the queries and the write load, so it should be checked rather than applied blindly.
What is a covering index?
A covering index contains all the columns a query needs, so MySQL can answer from the index without reading the table. It can improve read performance but adds storage and write cost.
How do I find slow queries?
Enable the slow query log, review the application’s most important queries, and use EXPLAIN with realistic data. Prioritise the queries that affect users and business operations.