#### 记一次隐式转换引起的sql慢查询

实在很无语呀，遇到一个mysql隐式转换问题，问了周边的dba大拿该问题，他们居然反问我，你连这个也不知道？白白跟他们混了那么长   尼玛，我还真不知道。罪过罪过….

该文章后续仍在不断的更新修改中， 请移步到原文地址  http://xiaorui.cc/?p=5147

通过explain分析出了结果，当使用整型来查询字符串的字段会出现无法走索引的情况，看下面可以知道，key为NULL，没走索引，Rows是很大的数值，基本是全表扫描了。  当正常的用字符串查询字符串就很正常了，索引没问题，rows的值为1，这里说的是扫描聚簇索引的rows，而不是索引二级索引。

# xiaorui.cc

If both arguments in a comparison operation are strings, they are compared as strings.
If both arguments are integers, they are compared as integers.
Hexadecimal values are treated as binary strings if not compared to a number.
If one of the arguments is a TIMESTAMP or DATETIME column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed. This is done to be more ODBC-friendly. Note that this is not done for the arguments to IN()! To be safe, always use complete datetime, date, or time strings when doing comparisons. For example, to achieve best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.
If one of the arguments is a decimal value, comparison depends on the other argument. The arguments are compared as decimal values if the other argument is a decimal or integer value, or as floating-point values if the other argument is a floating-point value.
In all other cases, the arguments are compared as floating-point (real) numbers.

sql查询的时候，字段的类型要保持一致，不然会数据字段的隐式转换，继而出现慢查询。 还是那句废话，多看mysql的慢查询日志，有你想要的.

END.