mysql用explain查索引_MYSQL 用 explain 语句判断select查询是否使用了索引

我有一个 zje 表

我先用一个普通的 select语句,用explain解析,看看有什么显示:

explain select * from zje;

d4df41061b62b09e025dc1e3992e3b19.png

主要关注的是,type 和 key:

type = ALL :表示全表扫描

type = const :表示通过索引一次就找到了

key = NULL:表示没有使用索引

key = primary :表示使用了主键

key一般=使用了主键/索引的名字

下面的图是使用了索引的:

select * from zje where math<60;

47d4433c1b0a08f1cc05ba69206098cd.png

其中 key = math的 math就是索引名

假设有一张表,里面有一个复合索引:CREATE TABLE `user` (

`id` int(11) unsigned NOT NULL AUTO_INCREMENT,

`name` varchar(255) DEFAULT NULL,

`gongzi` int(10) unsigned DEFAULT NULL,

`class_id` int(11) DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `iname` (`name`),

KEY `classname` (`class_id`,`name`)

) ENGINE=MyISAM AUTO_INCREMENT=41 DEFAULT CHARSET=utf8 |

查询里面数据有

b80e5feb566f352997238a5c940a0e8a.png

复合索引,遵循从左到有依次的原则。

explain select * from user where class_id=1 \G

ce25982f58a7070e7f4263a5518f6bc3.png

explain select * from user where class_id>1 \G

0a44d8a01c784f29bff083c4481c8a05.png

第一个搜索条件必须是等于,不能使用区间范围  大小啊,否则索引失效。

复合索引不能使用区间,也不能使用不等于

mysql> explain select * from user where class_id!=1 \G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: user

type: ALL

possible_keys: classname

key: NULL

key_len: NULL

ref: NULL

rows: 40

Extra: Using where

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值