0

For example, I have this query:

SELECT * FROM a WHERE field IN (-1, "1")

The above query takes 20 times longer than this one to execute. Why?

SELECT * FROM a WHERE field IN (-1, 1)

Also, in the first case the index is not used, even with USE INDEX.

AdamMc331
  • 16,492
  • 10
  • 71
  • 133
newpdv
  • 412
  • 1
  • 6
  • 15

1 Answers1

2

Most likely because MySQL is converting field to CHAR, and not "1" to INT.

See how long this runs:

SELECT * FROM a WHERE field IN (-1, CAST("1" AS INT));
mike.k
  • 3,277
  • 1
  • 12
  • 18
  • 1
    See [this](http://stackoverflow.com/a/6782019/3131147) answer for more info. It sounds like MySQL is actually converting "1" (a char) to an int(because that matches the type of field), but still, this conversion process takes time, and that's why it runs longer. – AdamMc331 May 20 '15 at 17:38