MariaDB社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3036|回复: 2
打印 上一主题 下一主题

SQL语句中一个符号引发的“惨案”!

[复制链接]
跳转到指定楼层
1#
发表于 2012-12-19 10:54:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 squall 于 2012-12-19 10:56 编辑

表结构和数据如下:
  1. DROP TABLE IF EXISTS `student`;
  2. CREATE TABLE `student` (
  3. `id` int(11) NOT NULL DEFAULT '0',
  4. `name` varchar(6) DEFAULT NULL,
  5. `class` int(11) DEFAULT NULL,
  6. `score` varchar(10) DEFAULT NULL,
  7. PRIMARY KEY (`id`),
  8. KEY `name` (`name`),
  9. KEY `IX_score` (`score`),
  10. KEY `IX_class` (`class`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
复制代码
  1. mysql> select * from student;
  2. +----+------+-------+-------+
  3. | id | name | class | score |
  4. +----+------+-------+-------+
  5. | 1 | a | 1 | 56 |
  6. | 2 | b | 1 | 61 |
  7. | 3 | c | 2 | 78 |
  8. | 4 | d | 2 | 45 |
  9. | 5 | e | 3 | 76 |
  10. | 6 | f | 3 | 89 |
  11. | 7 | g | 4 | 43 |
  12. | 8 | h | 4 | 90 |
  13. +----+------+-------+-------+
  14. 8 rows in set (0.00 sec)
复制代码
执行语句 select * from student where score = 60;

看看执行计划:
  1. mysql> explain select * from student where score = 60;
  2. +----+-------------+---------+------+---------------+------+---------+------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+---------+------+---------------+------+---------+------+------+-------------+
  5. | 1 | SIMPLE | student | ALL | IX_score | NULL | NULL | NULL | 8 | Using where |
  6. +----+-------------+---------+------+---------------+------+---------+------+------+-------------+
  7. 1 row in set (0.00 sec)
复制代码
没有用到索引?

其中的原因,虽然保存的是数字,但是列设置为了varchar,那么在MySQL中就用不到索引了,加个引号会怎么样呢:
  1. mysql> explain select * from student where score = '60' ;
  2. +----+-------------+---------+------+---------------+----------+---------+-------+------+-----------------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+---------+------+---------------+----------+---------+-------+------+-----------------------+
  5. | 1 | SIMPLE | student | ref | IX_score | IX_score | 33 | const | 1 | Using index condition |
  6. +----+-------------+---------+------+---------------+----------+---------+-------+------+-----------------------+
  7. 1 row in set (0.00 sec)
复制代码
另外,要不就把列类型改成integer,也可以用到索引。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖 顶 踩
2#
发表于 2013-3-14 09:59:51 | 只看该作者
楼主,貌似即使是int类型的字段,加上索引也会比较快点
3#
发表于 2016-1-21 09:59:12 | 只看该作者
天津治癫痫病医院排名 http://yyk.familydoctor.com.cn/20336/Content_23134.html
天津最专业癫痫病医院 http://yyk.familydoctor.com.cn/20336/Content_23144.html
北京癫痫病治疗多少钱 http://yyk.familydoctor.com.cn/20336/Content_15957.html
长春市癫痫病专科医院 http://yyk.familydoctor.com.cn/20336/Content_15956.html
癫痫医院排名前十 http://yyk.familydoctor.com.cn/20336/Content_15771.html
北京治疗癫痫的医院在哪里 http://yyk.familydoctor.com.cn/20336/Content_14018.html
北京治疗癫痫最好的医院 http://yyk.familydoctor.com.cn/20336/Content_14612.html
北京癫痫医院排行榜 http://yyk.familydoctor.com.cn/20336/Content_14754.html
北京癫痫医院哪家好 http://yyk.familydoctor.com.cn/20336/Content_14775.html
治癫痫哪个医院好 http://yyk.familydoctor.com.cn/20336/Content_14797.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|MariaDB社区 ( 京ICP备07012489号    |
业务联系: QQ:48474881; 邮箱: 48474881@qq.com; 电话:13911732319
声明:本站部分文章是网友转载,若未经作者同意或署名有误,请联系网站管理员。

GMT+8, 2024-9-20 05:54 , Processed in 0.076379 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表