Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

An index exists on orders(customer_id) where customer_id is VARCHAR. The Java code runs SELECT * FROM orders WHERE customer_id = ? with setLong(1, 42) on MySQL and the query does a full table scan. Why?

customer_id is a VARCHAR, but setLong binds the number 42, and when MySQL compares a string column with a number it converts the column value of every row to a number before comparing. That conversion is a function on the column, so the index on customer_id cannot be searched and MySQL scans the whole table. Binding the value with the column's own type, setString(1, "42"), or making the JPA entity field a String, lets the index be used again; EXPLAIN shows the problem as type ALL with key NULL.

Why this question is asked

Type mismatches between application code and the schema are a classic source of full table scans that nobody notices until the table is large. The SQL looks perfect and the index exists, so developers look everywhere except at the parameter type. Interviewers ask this to see whether we understand comparison rules and how the JDBC driver binds parameters. In this answer, we will recap what happens to the parameter between Java and MySQL, see MySQL's rule for comparing strings with numbers, reproduce the scan with EXPLAIN, fix it in JDBC and JPA, and look at the same problem in PostgreSQL and in joins.

1What Actually Reaches MySQL

A PreparedStatement sends SQL with ? placeholders and binds a value for each. With MySQL Connector/J, by default the driver does the substitution itself (client-side prepared statements) and sends the final SQL text to the server. The setter we choose decides how the value is written into that text: setString writes a quoted string literal, setLong writes a bare number.

Java
PreparedStatement ps = con.prepareStatement("SELECT * FROM orders WHERE customer_id = ?");
ps.setLong(1, 42);
// sent to MySQL: SELECT * FROM orders WHERE customer_id = 42 <- a number
ps.setString(1, "42");
// sent to MySQL: SELECT * FROM orders WHERE customer_id = '42' <- a string

With server-side prepared statements the value travels separately with a type code instead, but the effect is the same: the server receives an integer parameter for a VARCHAR column. Notice that nothing fails. The query is valid SQL and returns the correct rows; it is just slow.

2Why the Comparison Defeats the Index

When MySQL compares a string with a number, its type conversion rules say both sides are compared as numbers (as floating point values). So for every row MySQL must take the stored string, convert it to a number, and compare it with 42. The index on customer_id stores the strings in string order, and a numeric comparison cannot be turned into a range of that order.

There is a good reason MySQL cannot simply convert 42 into '42' and use the index. Many different strings convert to the number 42: '42', '042', ' 42', '42abc' and '42.0' all compare equal to 42 numerically. They sit at different places in the string-sorted index, so a single index lookup for '42' would miss rows that the numeric comparison must return. The only correct way is to check every row, which is a full scan.

SQL
SELECT '042' = 42, '42abc' = 42, '42' = 42;
-- 1, 1, 1 (each converted to the number 42; '42abc' also raises a warning)
Column type versus bound value in MySQL
ColumnBound valueIndex used?
VARCHAR customer_id'42' via setStringyes, string compared with string
VARCHAR customer_id42 via setLongno, every row converted to a number
INT customer_id42 via setLongyes
INT customer_id'42' via setStringyes, the constant is converted once

The opposite direction is harmless. If customer_id were an INT column and we bound the string '42', MySQL converts the constant once to a number and can still use the index. The problem appears specifically when the column side needs converting.

3Confirming It With EXPLAIN

MySQL's EXPLAIN shows the access type and the index chosen. A full table scan appears as type ALL with key NULL, even though possible_keys may mention the index.

SQL
EXPLAIN SELECT * FROM orders WHERE customer_id = 42;
-- type: ALL possible_keys: idx_customer_id key: NULL rows: 9800000 Extra: Using where
EXPLAIN SELECT * FROM orders WHERE customer_id = '42';
-- type: ref key: idx_customer_id rows: 37

The difference is 9.8 million rows examined against 37. To find these cases in a running system, the slow query log or performance_schema digests show statements with a high rows examined count compared with rows sent, and MySQL may also report a warning saying it cannot use the index because of a type or collation conversion; check your version for the exact wording.

4Fixing It in JDBC and JPA

In plain JDBC the fix is to bind the parameter with the column's type.

Java
ps.setString(1, String.valueOf(customerId)); // matches VARCHAR
// JPA: the entity field type decides how Hibernate binds the value
@Entity
class Order {
@Column(name = "customer_id")
private String customerId; // not Long: Hibernate then binds a VARCHAR parameter
}
// Spring Data repository: the parameter type follows the field
List<Order> findByCustomerId(String customerId);

In JPA we rarely call setters ourselves, so the entity field type is what matters. If the field is Long while the column is VARCHAR, Hibernate binds a numeric JDBC type and we get the scan, even though the generated SQL looks perfect in the log. Schema validation (hibernate.hbm2ddl.auto=validate) can catch some of these mismatches at startup. The cleanest long-term fix, if customer IDs are really numbers, is to change the column to BIGINT, and the field to match.

5The Same Trap in PostgreSQL and in Joins

PostgreSQL is stricter and usually raises an error such as operator does not exist: character varying = bigint instead of converting silently, which is arguably better. But the same scan appears when the code adds an explicit cast on the column, such as WHERE customer_id::bigint = 42, which turns the condition into a function on the column.

Joins have the same problem in both databases. If orders.customer_id is VARCHAR and customers.id is BIGINT, the join condition needs a conversion on one side, and the index on that side cannot be used for lookups. Join keys should always have identical types, and a schema review that compares the types of foreign key columns finds these mismatches early.

6Follow-up questions

How do I find other queries with this problem?

In MySQL, look in performance_schema statement digests or the slow log for statements with rows examined far larger than rows sent, then run EXPLAIN on them and look for type ALL with an index listed in possible_keys. In the code, compare entity field types with column types, which a schema validation step at startup or a test can do automatically.

Why does MySQL not just raise an error like PostgreSQL?

MySQL's design has long favoured implicit conversion to be forgiving with input, which is convenient for small applications and dangerous for performance. Strict SQL modes make many conversions stricter, but string to number comparison is still allowed, so we cannot rely on the server to catch it.

Does a collation mismatch cause the same problem?

It can. Comparing two string columns with different character sets or collations, typically in a join, may force a conversion of one side, and then that side's index cannot be used. Keeping the same character set and collation for columns that are compared or joined avoids it.

7Conclusion

In this answer, we saw that setLong sends a number, and when MySQL compares a VARCHAR column with a number it converts every stored string to a number, because many strings like '042' and '42abc' equal 42 numerically, so the index cannot be searched and the table is scanned. EXPLAIN showed type ALL and key NULL. We fixed it by binding with setString or making the JPA field a String, and saw the same trap in PostgreSQL with explicit casts on the column and in joins between columns of different types.

What to say in the interviewAbout 90 seconds aloud

Open with

The query looks fine, but the parameter type is wrong. customer_id is a VARCHAR and setLong binds the number 42, so MySQL ends up comparing a string column with a number.

Then, if they want more

Its rule is that a string compared with a number is compared numerically, so it has to convert the customer_id of every row to a number before it can compare. That conversion is effectively a function on the column, and the index, which stores the strings in string order, cannot be searched.

MySQL also cannot just turn 42 into the string '42', because '042', ' 42' or '42abc' all equal 42 numerically and they sit in different places in the index. So it scans the whole table. EXPLAIN would show type ALL and key NULL, with millions of rows examined, while the same query with '42' shows type ref using the index.

The fix is to bind with the column's type, setString with "42". In JPA the entity field type drives how Hibernate binds, so the field must be a String if the column is a VARCHAR, or better, if the IDs really are numbers, change the column to BIGINT.

The opposite direction, an INT column with a string parameter, is fine because only the constant is converted. PostgreSQL usually throws an error instead, but explicit casts on the column or joins between mismatched types cause the same full scan there.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "An index exists on orders(customer_id) where customer_id is VARCHAR. The Java code runs...": https://scrivio-interview-library.netlify.app/databases/query-optimization/4965499119/