关于mysql中where条件的类型自动转换
来源:互联网 发布:汽车行业制图软件 编辑:程序博客网 时间:2024/05/17 08:10
CREATE
TABLE
`appstat_day_prototype_201305` (
`day_key`
date
NOT
NULL
DEFAULT
'1900-01-01'
,
`appkey`
varchar
(20)
NOT
NULL
DEFAULT
''
,
`user_total`
bigint
(20)
NOT
NULL
DEFAULT
'0'
,
`user_activity`
bigint
(20)
NOT
NULL
DEFAULT
'0'
,
`times_total`
bigint
(20)
NOT
NULL
DEFAULT
'0'
,
`times_activity`
bigint
(20)
NOT
NULL
DEFAULT
'0'
,
`incr_login_daily`
bigint
(20)
NOT
NULL
DEFAULT
'0'
,
`unbind_total`
bigint
(20)
NOT
NULL
DEFAULT
'0'
,
`unbind_activitys`
bigint
(20)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(`appkey`,`day_key`)
) ENGINE=InnoDB
DEFAULT
CHARSET=utf8
mysql> explain
SELECT
*
from
appstat_day_prototype_201305
where
appkey = xxxxx
and
day_key
between
'2013-05-23'
and
'2013-05-30'
;
+
----+-------------+------------------------------+------+---------------+------+---------+------+----------+-------------+
| id | select_type |
table
| type | possible_keys |
key
| key_len | ref |
rows
| Extra |
+
----+-------------+------------------------------+------+---------------+------+---------+------+----------+-------------+
| 1 | SIMPLE | appstat_day_prototype_201305 |
ALL
|
PRIMARY
|
NULL
|
NULL
|
NULL
| 19285787 | Using
where
|
+
----+-------------+------------------------------+------+---------------+------+---------+------+----------+-------------+
1 row
in
set
(0.00 sec)
mysql> explain
SELECT
*
from
appstat_day_prototype_201305
where
appkey =
'xxxxx'
and
day_key
between
'2013-05-23'
and
'2013-05-30'
;
+
----+-------------+------------------------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type |
table
| type | possible_keys |
key
| key_len | ref |
rows
| Extra |
+
----+-------------+------------------------------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | appstat_day_prototype_201305 | range |
PRIMARY
|
PRIMARY
| 65 |
NULL
| 1 | Using
where
|
+
----+-------------+------------------------------+-------+---------------+---------+---------+------+------+-------------+
1 row
in
set
(0.00 sec)
从上面可以很明显的看到由于appkey是varchar,而在where条件中不加'',会引发全表查询,加了就可以用到索引,这扫描的行数可是天差地别,对于服务器的压力和响应时间自然也是天差地别的。
我们再看另外一个例子:
从上面的例子看,虽然poll_id的类型为bigint,但是SQL中添加了'',但是这个语句仍然用到了索引,虽然扫描行数也不少,但是能用到索引就是好SQL。
那么一个小小的''为什么会有这么大的影响呢?根本原因是因为MySQL在对文本类型和数字类型进行比较的时候会进行隐式的类型转换。
以下是5.5官方手册的说明:
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.
有一个参数是
TIMESTAMP
或 DATETIME,并且另外一个参数是常量,常量会被转换为
timestamp
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.
有一个参数是
decimal
类型,如果另外一个参数是
decimal
或者整数,会将整数转换为
decimal
后进行比较,如果另外一个参数是浮点数,则会把
decimal
转换为浮点数进行比较
In
all
other cases, the arguments are compared
as
floating-point (
real
) numbers.所有其他情况下,两个参数都会被转换为浮点数再进行比较
根据以上的说明,当where条件之后的值的类型和表结构不一致的时候,MySQL会做隐式的类型转换,都将其转换为浮点数在比较。
对于第一种情况:
比如where string = 1;
需要将索引中的字符串转换成浮点数,但是由于'1',' 1','1a'都会比转化成1,故MySQL无法使用索引只能进行全表扫描,故造成了慢查询的产生。
mysql>
SELECT
CAST
(
' 1'
AS
SIGNED)=1;
+
-------------------------+
|
CAST
(
' 1'
AS
SIGNED)=1 |
+
-------------------------+
| 1 |
+
-------------------------+
1 row
in
set
(0.00 sec)
mysql>
SELECT
CAST
(
' 1a'
AS
SIGNED)=1;
+
--------------------------+
|
CAST
(
' 1a'
AS
SIGNED)=1 |
+
--------------------------+
| 1 |
+
--------------------------+
1 row
in
set
, 1 warning (0.00 sec)
mysql>
SELECT
CAST
(
'1'
AS
SIGNED)=1;
+
-----------------------+
|
CAST
(
'1'
AS
SIGNED)=1 |
+
-----------------------+
| 1 |
+
-----------------------+
1 row
in
set
(0.00 sec)
同时需要注意一点,由于都会转换成浮点数进行比较,而浮点数只有53bit,故当超过最大值的时候,比较会出现问题。
对于第二种情况:
由于索引建立在int的基础上,而将纯数字的字符串可以百分百转换成数字,故可以使用到索引,虽然也会进行一定的转换,消耗一定的资源,但是最终仍然使用了索引,不会产生慢查询。
以上所述是小编给大家介绍的MySQL数据库中把int转化varchar引发的慢查询 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
原文链接:http://www.cnblogs.com/billyxp/archive/2013/05/31/3110016.html?utm_source=tuicool&utm_medium=referral
- 关于mysql中where条件的类型自动转换
- 验证where条件中类型隐式转换问题
- MySQL中where条件的执行分析
- mysql中Where条件中字段顺序的重要性
- mysql中Where条件中字段顺序的重要性
- mysql中Where条件中字段顺序的重要性
- mysql的5中查询子句之一where条件查询
- mysql where 条件中加 if 判断
- 关于Java变量类型的自动转换
- 关于sql条件语句where id in (@参数)执行报错问题(转换成数据类型 int 时失败)(
- 关于where的条件查询in
- MySQL是如何优化WHERE条件的
- 关于C#中关于List.where中过滤条件的使用注意事项
- 关于C#中关于List.where中过滤条件的使用注意事项--笔记
- 浅析Mysql Where条件
- 关于MYSQL查询时WHERE条件值为数字的问题
- Where条件中字段顺序的重要性
- oracle where 后面的条件中|| 是什么意思
- 不错的链接--积累ing
- svn+apache服务搭建
- 自用-动画管理
- Arrays.sort()用的是什么排序算法?怎么优化?
- 51nod1008N的阶乘
- 关于mysql中where条件的类型自动转换
- ZYNQ XC7Z030平台Linux+裸机AMP实现(官方文档1078、1079)
- 协程
- WebStrom 自动补全快捷键
- Mac显示隐藏文件命令
- This is usually caused by another repository pushing:refusing to merge unrelated histories
- c#笔记(二)
- Reactor模式软文
- js动态改变地址栏url,不刷新页面