KC SoftwareSUPPORT
← All articles

Troubleshooting · Oct 2026 · 2 min read

Finding out why a database query is slow

Guessing at database performance rarely works. Measure, read the query plan, then change one thing at a time.

First find the slow query. Most databases can log queries that take longer than a set time, and most frameworks can show the queries behind a page. Measure before you change anything, so you know whether a change helped.

Ask the database how it runs the query. Put EXPLAIN in front of the query in MySQL or PostgreSQL to see the plan. PostgreSQL's EXPLAIN ANALYZE, and MySQL 8's EXPLAIN ANALYZE, also run the query and show where the time goes.

Look for full table scans on large tables. They often mean a missing index on a column used in a WHERE condition, a JOIN or an ORDER BY.

Check how many queries a page runs, not only how slow each one is. Code that loads a list and then runs one more query for each item, often called the N+1 problem, can run hundreds of fast queries that add up to a slow page.

Select only the columns you need, and test with a realistic amount of data. A query that is instant with a hundred rows can be slow with a million.