Why MySQL Sometimes Skips Your Index on Range Queries (and How to Fix It)
This article explains why MySQL may ignore an index on range queries, shows example queries with EXPLAIN output, describes the optimizer's row‑count threshold that triggers a full table scan, and offers guidance on keeping indexes effective.
Indexes dramatically speed up database queries, especially when you create them on frequently filtered columns such as trans_date. An equality query like SELECT * FROM A WHERE trans_date = '20220222' will reliably use the union_idx_query index.
However, when the same column is used in a range condition (e.g., WHERE trans_date > '20220122'), MySQL does not always use the index. An EXPLAIN of that query shows a full‑table scan, indicating that the optimizer decided the index would not be efficient.
Changing the constant value can alter the optimizer's decision. For example, SELECT * FROM t_trans_log_info WHERE trans_date > '20220222' results in an EXPLAIN that shows the index is used. The difference stems from the optimizer estimating how many rows the range will return.
The optimizer prefers a full scan when the estimated rows accessed via the index exceed roughly 10‑30% of the total table size, because scanning the index and then fetching rows would be slower than reading the table sequentially. This threshold varies with data distribution and query context, and even forcing an index may be ignored if the optimizer deems it inefficient.
Range queries ( >, <, >=, <=, IN, etc.) often trigger this behavior. To ensure the index remains effective, you may need to add additional limiting conditions, rewrite the query, or create more selective composite indexes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
