iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
MySQL can fail to use a string index for a quick lookup when a query compares a string column with a number. The comparison may treat differently formatted strings as the same numeric value, so a single string-key lookup cannot reliably find every match. That is a credible explanation for a broad scan, but the cited MySQL documentation does not verify that a particular incident scanned seven million rows; confirm that count in the query plan or incident telemetry.
How a type mismatch changes an indexed comparison
When comparison operands have different types, MySQL may convert them to make the comparison. For string-versus-number comparisons, it can compare values numerically rather than as text. The MySQL 8.0 Reference Manual explains the consequence: “For comparisons of a string column with a number, MySQL cannot use an index on the column to look up the value quickly.”
The reason is that numeric equality can match multiple distinct strings. For example, strings such as '1', ' 1', and '1a' can convert to the same numeric value. A string index is ordered by string keys, but the numeric predicate may consider several different keys equal. MySQL therefore cannot assume that looking up just one textual key will satisfy the predicate.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsThe MySQL 8.4 index documentation describes the broader principle: comparing dissimilar types, including string and numeric values, may prevent index use. It gives string representations such as '1', ' 1', '00001', and '01.e1' as values that can compare equal numerically. See How MySQL Uses Indexes.
#1 Best Overall
What “a 7M-row scan” does—and does not—establish
The seven-million-row figure belongs to the specific incident named in the headline; the cited manuals do not independently confirm it. A slow query alone does not prove that MySQL examined seven million rows. Check plan output and, where safe and supported, runtime row counts before attributing that number to a scan.
Also distinguish a table scan from an index scan. MySQL may scan an index for reasons unrelated to this conversion issue, and documentation that index use may be prevented does not prove what happened in any one execution. A historical MySQL Bug #61778 discusses a separate mismatch example from 2011; its row-count example is not evidence for this incident.
How to diagnose the query
- Check the column definition. Inspect the schema for the exact column used in the predicate. Determine whether it is a character type such as
VARCHARor a numeric type, and whether its values are identifiers or quantities. - Check the value MySQL receives. Review the SQL literal and the application’s bound parameter type. A parameter that looks numeric in application code may still be sent with a type that triggers a conversion, or a numeric literal may be compared against a string column.
- Inspect the plan. Run
EXPLAINand review the access type, chosen key, and estimated rows. Where your MySQL version supports it and running the query is safe, useEXPLAIN ANALYZEto inspect actual execution and row counts. Compare this evidence with the incident’s observed behavior rather than inferring examined rows from elapsed time. - Check whether conversion affects the predicate. Confirm whether the compared expressions have compatible types and whether MySQL is applying conversion. Use the documentation for the version deployed: conversion and optimizer behavior should be assessed against that version and the actual query plan.
Choose a fix that preserves the data’s meaning
| Approach | When it fits | Index and semantics considerations |
|---|---|---|
| Send a value with the column’s type | The column’s existing type matches the intended data model, but the query literal or application parameter does not. | Can avoid a mismatch without converting the indexed column; verify the resulting plan and that the comparison still means what the application expects. |
| Keep comparison textual | The column stores identifiers or formatted values for which distinct spellings matter. | Compare as text so values such as '01' and '1' can remain distinct. Confirm that the index is used in the actual plan. |
| Correct the schema and data | The column represents a number, and storing it as text is not an intentional part of the data model. | Assess existing values, conversion safety, constraints, and application dependencies before changing the column. Preserve formatting if it carries meaning. |
| Cast or convert the indexed column | Only where the conversion is required for semantics and the resulting plan has been checked. | Not a general performance fix: MySQL warns that applying CAST, CONVERT, or BINARY to an indexed column may prevent efficient index use. See Cast Functions and Operators. |
Before changing a type or comparison, test values that could collapse under numeric conversion. If '01' and '1' are different identifiers in your system, a numeric comparison changes the result set, not just the query plan. After a change, rerun the relevant plan and validate both matching rows and execution behavior.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Why casting the column is not an automatic remedy
It may seem natural to write a predicate such as CAST(indexed_column AS UNSIGNED) = ? to make the comparison numeric. But this applies a function to the indexed column, which can keep MySQL from using the original index for an efficient lookup. Prefer aligning the supplied value with the column’s intended type when that preserves semantics; if that is not possible, confirm the plan rather than assuming the cast is faster.
Quick Recap
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

