1

if I have an int column of id, is it bad for performance to have a query written like

SELECT * FROM products where id = '1'?

Or does MySQL handle this the same as

SELECT * FROM products where id = 1
3
  • Performancewise, the difference is going to be negligible. But they are capable of returning different result. Commented Nov 11, 2020 at 22:00
  • MySQL uses the leading digits of a text field when converting to int, so 123 = '123foo' is true. Commented Nov 11, 2020 at 22:12
  • There's an implicit assumption in your question I think that id is an int type. On general principles of Postel's Law, be strict in what you provide, so provide a comparison with the type that you are comparing to. Commented Nov 11, 2020 at 23:24

3 Answers 3

2

In general: don't do this. Compare values against a literal of the correct datatype, so you don't have to worry about what is going on under the hood. If you have an integer column, then:

SELECT * FROM products WHERE id = 1

In this specific situation: when asked to compare a number to a string, MySQL converts the string to a number. So there would be (almost) no performance penalty if you were to use:

SELECT * FROM products WHERE id = '1'

The literal string is converted once, and then checked against every value in the column.

On the other hand, consider a string column, say str, and a predicate like:

WHERE str = 1

Now it goes the other way around. MySQL needs to convert all string values in str to numbers before they can be compared. The expression becomes inefficient - it cannot take advantage of an index (it is non-SARGable). If there are many rows, the performance impact may be important.

So, back to the initial recommendation: know your datatypes; use the proper literal when comparing.

Sign up to request clarification or add additional context in comments.

4 Comments

Why would MySQL not turn the 1 to '1' in your "WHERE str = 1 example?
@Ben: MySQL follows a set of predefined rules when asked to compare values of different datatypes. And the rule says that a string and an integer should be compared as numbers. See: dev.mysql.com/doc/refman/8.0/en/type-conversion.html. If you don't want to worry about this, use the proper datatype from the start, as recommended in the answer...
Thanks, just curious
Also mysql will produce Truncated incorrect DOUBLE value warning if str is not a number
0

If performance is your concern, numbers are smaller than text in this example--1 is faster than "1".

HOWEVER... if you're looking for performance never start with SELECT *

Comments

0

The cases:

WHERE int_col = 2      -- Good
WHERE int_col = "2"    -- Good; the "2" is turned into INT at start of query
WHERE int_col = "2foo" -- Probably not what you wanted

WHERE char_col = 2     -- Slow; `char_col` is turned into INT for each row
WHERE char_col = "2"   -- Good

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.