Why a MySQL Query Took 30 Seconds and How a Hidden Index Cut It to 0.2 s
A production MySQL query on a table with five million rows took over 30 seconds, and after reproducing the issue, examining execution plans, trying various index and query tweaks, discovering that SQLyog adds a hidden LIMIT, and finally forcing the correct index, the runtime dropped to under a second, illustrating the importance of proper index usage and tool‑specific quirks.
1. Problem Background
In the live system a query on a table with about five million rows required more than 30 seconds; the query selects users under certain conditions using
GROUP BY. In a test environment with the same data volume the query took 37 seconds.
The
app_accountcolumn contains 5,000 distinct random values, each appearing roughly 1,000 times.
2. Execution Plan
The execution plan shows that the
GROUP BYcolumn has an index and that the index is being used.
3. Optimization Attempts
Several ideas were tried but did not noticeably improve performance:
Add
ORDER BY NULLto avoid unnecessary sorting.
Add indexes to the fields used in the
WHEREclause.
Reorder the
WHEREconditions so indexed columns appear first.
Create composite indexes covering all
WHEREfields.
Rewrite the query using sub‑queries.
Replacing
GROUP BYwith
DISTINCTdramatically reduced the runtime on the test machine (0.8 s vs 37 s), but the same query on other computers still took more than 30 seconds.
4. Unexpected Behavior
It was later discovered that SQLyog automatically appends
LIMIT 1000to the query, which makes the query appear fast in that client. Running the same statement from the command line or on the server shows the real 30‑second execution time.
5. Final Solution
Forcing the use of the
idx_end_timeindex (e.g.,
USE INDEX (idx_end_time)) reduced the query time to 0.19 seconds.
Before and after execution plans illustrate the improvement.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.