最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

MySQL中字符串字段,在使用in时,没有加引号时的性能陷阱

来源:动视网 责编:小采 时间:2020-11-09 09:12:50
文档

MySQL中字符串字段,在使用in时,没有加引号时的性能陷阱

MySQL中字符串字段,在使用in时,没有加引号时的性能陷阱:场景和环境redhat6.5 + 64位 + 12核心 + 16G表数量 600wMySQL 5.0问题描述在使用in过程中,同事写了一个简单的in条件查询(字段是普通索引,varchar),由于拼装sql的时候,没有使用引号,导致出现大量慢查询问题SQLselect count(*) tot
推荐度:
导读MySQL中字符串字段,在使用in时,没有加引号时的性能陷阱:场景和环境redhat6.5 + 64位 + 12核心 + 16G表数量 600wMySQL 5.0问题描述在使用in过程中,同事写了一个简单的in条件查询(字段是普通索引,varchar),由于拼装sql的时候,没有使用引号,导致出现大量慢查询问题SQLselect count(*) tot


场景和环境

redhat6.5 + 64位 + 12核心 + 16G

表数量 600w

MySQL 5.0

问题描述

在使用in过程中,同事写了一个简单的in条件查询(字段是普通索引,varchar),由于拼装sql的时候,没有使用引号,导致出现大量慢查询

问题SQL

select count(*) total from member_phone where phone in(1521xxx541,15845xxx412)

问题SQL和纠正过的写法对比

执行时间

mysql> select count(*) total from member_phone where phone in(1521xxx541,15845xxx412);
+-------+
| total |
+-------+
| 1 | 
+-------+
1 row in set (2.76 sec)
mysql> select count(*) total from member_phone where phone in('1521xxx541','15845xxx412');
+-------+
| total |
+-------+
| 1 | 
+-------+
1 row in set (0.01 sec)
mysql> select count(*) total from member_phone where (phone='1521xxx541' or phone='15845xxx412');
+-------+
| total |
+-------+
| 1 | 
+-------+
1 row in set (0.00 sec)

EXPLAIN

mysql> explain select count(*) total from member_phone where phone in(1521xxx541,15845xxx412) \G;
*************************** 1. row ***************************
 id: 1
 select_type: SIMPLE
 table: member_phone
 type: index
possible_keys: phone
 key: phone
 key_len: 18
 ref: NULL
 rows: 6307075
 Extra: Using where; Using index
1 row in set (0.00 sec)
mysql> explain select count(*) total from member_phone where phone in('1521xxx541','15845xxx412') \G;
*************************** 1. row ***************************
 id: 1
 select_type: SIMPLE
 table: member_phone
 type: range
possible_keys: phone
 key: phone
 key_len: 18
 ref: NULL
 rows: 2
 Extra: Using where; Using index
1 row in set (0.00 sec)
mysql> explain select count(*) total from member_phone where (phone='1521xxx541' or phone='15845xxx412') \G;
*************************** 1. row ***************************
 id: 1
 select_type: SIMPLE
 table: member_phone
 type: range
possible_keys: phone
 key: phone
 key_len: 18
 ref: NULL
 rows: 2
 Extra: Using where; Using index
1 row in set (0.01 sec)

总结

在三个类型的sql中,效率从高到低分别是 or,in 添加了引号, in不加引号。在explain中看到不加引号时,显示的用上了索引phone,type 变成了 index ,和全表扫描差不多了,只不过MySQL扫描时按索引的次序进行而不是行。

提醒

在where多个or,in中条件个数比较多,或者多个in 条件时,实际性能都比较差的。以上测试我个人仅在MySQL5.0中测试,高版本官方不知是否优化过。

文档

MySQL中字符串字段,在使用in时,没有加引号时的性能陷阱

MySQL中字符串字段,在使用in时,没有加引号时的性能陷阱:场景和环境redhat6.5 + 64位 + 12核心 + 16G表数量 600wMySQL 5.0问题描述在使用in过程中,同事写了一个简单的in条件查询(字段是普通索引,varchar),由于拼装sql的时候,没有使用引号,导致出现大量慢查询问题SQLselect count(*) tot
推荐度:
标签: in 没有加 时候
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top