11 索引优化

作者: 笑Skr人啊 | 来源:发表于2017-12-27 10:03 被阅读1次

创建表

/*
Navicat MySQL Data Transfer

Source Server         : CentOS6.7--Clone
Source Server Version : 50554
Source Host           : 192.168.1.179:3306
Source Database       : db01

Target Server Type    : MYSQL
Target Server Version : 50554
File Encoding         : 65001

Date: 2017-12-18 11:32:01
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `staffs`
-- ----------------------------
DROP TABLE IF EXISTS `staffs`;
CREATE TABLE `staffs` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(24) NOT NULL COMMENT '姓名',
  `age` int(3) NOT NULL COMMENT '年龄',
  `pos` varchar(20) NOT NULL COMMENT '职位',
  `create_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_staffs_nameAgePos` (`name`,`age`,`pos`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of staffs
-- ----------------------------
INSERT INTO `staffs` VALUES ('1', '张三', '22', '经理', '2017-12-18 11:25:18');
INSERT INTO `staffs` VALUES ('2', '李四', '23', '员工', '2017-12-18 11:25:48');
INSERT INTO `staffs` VALUES ('3', '王二', '23', '员工', '2017-12-18 11:25:49');

索引失效的情况

如果索引多列,查询时最左边的列不能缺失
#可以使用索引的三种情况
mysql> explain select * from staffs where name = "李四";
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------+------+-------------+
| id | select_type | table  | type | possible_keys         | key                   | key_len | ref   | rows | Extra       |
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------+------+-------------+
|  1 | SIMPLE      | staffs | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 74      | const |    1 | Using where |
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from staffs where name = "李四" and age = 23;
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------------+------+-------------+
| id | select_type | table  | type | possible_keys         | key                   | key_len | ref         | rows | Extra       |
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------------+------+-------------+
|  1 | SIMPLE      | staffs | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 78      | const,const |    1 | Using where |
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from staffs where name = "李四" and age = 23 and pos = "员工";
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
| id | select_type | table  | type | possible_keys         | key                   | key_len | ref               | rows | Extra       |
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
|  1 | SIMPLE      | staffs | ref  | idx_staffs_nameAgePos | idx_staffs_nameAgePos | 140     | const,const,const |    1 | Using where |
+----+-------------+--------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
1 row in set (0.00 sec)

#下面SQL用不到索引
mysql> explain select * from staffs where age = 23 and pos = "员工";
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | staffs | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from staffs where pos = "员工";
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | staffs | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

相关文章

  • 11 索引优化

    创建表 索引失效的情况 如果索引多列,查询时最左边的列不能缺失

  • MySQL,必须掌握的6个知识点

    目录 一、索引B+ Tree 原理 MySQL 索引 索引优化 索引的优点 索引的使用条件 二、查询性能优化使用 ...

  • sql优化的一般策略

    sql 优化的一般策略:索引优化,sql改写,参数优化,优化器 索引优化 以select * from vvsho...

  • 搜索引擎优化

    搜索引擎优化 SEO(Search Engine Optimization):汉译为搜索引擎优化。搜索引擎优化是一...

  • 17.MySQL优化

    《高性能MySQL》——这本书都有的 “字段”优化总结 “索引”优化总结 索引的优化 “查询SQL”优化总结 “引...

  • MySQL深入理解_SQL优化

    索引优化1[https://juejin.im/post/6844903954392825869] 索引优化2[h...

  • 增长黑客第三章(三)

    关键词:捆绑下载,搜索引擎优化, 3.6 搜索引擎和应用商店的优化营销 搜索引擎优化:利用搜索引擎的排序规则,通过...

  • 小白入门 | 做好SEO优化,只需八步

    SEO中文解释就是“搜索引擎优化”的意思,也可以理解为“百度搜索引擎优化”、“谷歌搜索引擎优化”、“360搜索引擎...

  • Mysql索引

    提起优化 SQL,可能会把它理解为优化索引。简单来说这也不算错,索引在 SQL 优化中占了很大的比重。索引用得好,...

  • mysql 索引优化

    索引的存储分类 索引的创建与删除 索引查看 mysql常用语句优化技巧定期优化表 常用优化 2.应尽量避免在whe...

网友评论

    本文标题:11 索引优化

    本文链接:https://www.haomeiwen.com/subject/ygbygxtx.html