I am trying to improve an old system (written ages ago) where every mysql queries are glued from string. So example of that query looks like
"SELECT * FROM User WHERE id > '3'"
Id column is of course bigint and PK.
What does mysql do with ‘3’ in query where id should be a int value? I assume it is treated as a string (due to ”) so this value is casted into int during analyze/optimize process by mysql. Am I right?
//UPDATE
I probably asked wrong way. There are two way to handle it.
-
(Fast) Mysql automatically detects that id should be int and rewrite/cast a query to
SELECT * FROM User WHERE id > 3before send it to DB engine
-
(Unbelievable) Mysql does
SELECT * FROMthen in loop apply condition WHERE id > ‘3’ and cast it for EVERY row
I just want to be sure that second option is impossible.
MySQL will always cast the string to a number for comparing, which in this case this is the right thing to do (it will use the index on the column to find the values).
If your column is a string and you compare it to an integer constant MySQL will cast the column to an integer and not use the index.