在Mysql中使用索引
MySQL查询的优化是个老生常谈的问题,方法更是多种多样,其中最直接的就是创建索引.
这里通过一个简单的demo来实际用一下索引,看看索引在百万级别查询中速率的提升效果如何
所需数据可以从我前面的一篇博客中获取:https://www.cnblogs.com/wangbaojun/p/11154515.html
有一张salaries,
查看表结构如下:
mysql> desc salaries;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| salary | int(11) | NO | | NULL | |
| from_date | date | NO | PRI | NULL | |
| to_date | date | NO | | NULL | |
+-----------+---------+------+-----+---------+-------+
可以看到emp_no,from_date都是PRI(主键索引),这是在这个表中将这两个字段联合起来设置为主键,一张表中还是只能有一个主键
查看表的创建命令:
mysql> show create table salaries;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| salaries | CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`),
CONSTRAINT `salaries_ibfk_1` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
注意我标红的地方,将emp_no`,`from_date设置为组合主键,组合索引遵从左前缀原则,查询emp_no,或者查询(`emp_no`,`from_date`)会走索引,但是查from_date不会走索引,可以看一下用explain命令查看:
mysql> explain select * from salaries where emp_no=227694;
+----+-------------+----------+------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | salaries | ref | PRIMARY | PRIMARY | 4 | const | 18 | NULL | # key为PRIMARY 走了索引
+----+-------------+----------+------+---------------+---------+---------+-------+------+-------+
1 row in set (0.01 sec) mysql> explain select * from salaries where from_date = '1986-06-26';
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | salaries | ALL | NULL | NULL | NULL | NULL | 2838426 | Using where | key为Null,Extra 使用了where,没走索引
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec) mysql> explain select * from salaries where from_date = '1986-06-26' and emp_no=75047;
+----+-------------+----------+-------+---------------+---------+---------+-------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+---------+---------+-------------+------+-------+
| 1 | SIMPLE | salaries | const | PRIMARY | PRIMARY | 7 | const,const | 1 | NULL | # key为PRIMARY 走了索引
+----+-------------+----------+-------+---------------+---------+---------+-------------+------+-------+
1 row in set (0.00 sec)
to_date字段没有设置索引,我们来测试一下加索引前后,该字段查询效率会不会有提升:
未加索引:
mysql> select * from salaries where to_date = '1986-06-26'; +--------+--------+------------+------------+
| emp_no | salary | from_date | to_date |
+--------+--------+------------+------------+
| 25676 | 40000 | 1985-06-26 | 1986-06-26 |
| 28757 | 40000 | 1985-06-26 | 1986-06-26 |
| 30860 | 64620 | 1985-06-26 | 1986-06-26 |
| 69209 | 40000 | 1985-06-26 | 1986-06-26 |
| 80550 | 45292 | 1985-06-26 | 1986-06-26 |
| 91204 | 47553 | 1985-06-26 | 1986-06-26 |
| 96140 | 52908 | 1985-06-26 | 1986-06-26 |
| 208352 | 42989 | 1985-06-26 | 1986-06-26 |
| 213109 | 90133 | 1985-06-26 | 1986-06-26 |
| 217498 | 80247 | 1985-06-26 | 1986-06-26 |
| 219462 | 83880 | 1985-06-26 | 1986-06-26 |
| 223150 | 40000 | 1985-06-26 | 1986-06-26 |
| 227694 | 73897 | 1985-06-26 | 1986-06-26 |
| 232856 | 73126 | 1985-06-26 | 1986-06-26 |
| 237619 | 56982 | 1985-06-26 | 1986-06-26 |
| 244087 | 40000 | 1985-06-26 | 1986-06-26 |
| 253472 | 72004 | 1985-06-26 | 1986-06-26 |
| 257395 | 40000 | 1985-06-26 | 1986-06-26 |
| 261811 | 40000 | 1985-06-26 | 1986-06-26 |
| 268968 | 40000 | 1985-06-26 | 1986-06-26 |
| 269331 | 40000 | 1985-06-26 | 1986-06-26 |
| 274805 | 40000 | 1985-06-26 | 1986-06-26 |
| 279432 | 74530 | 1985-06-26 | 1986-06-26 |
| 285685 | 83198 | 1985-06-26 | 1986-06-26 |
| 286745 | 44082 | 1985-06-26 | 1986-06-26 |
| 290901 | 49876 | 1985-06-26 | 1986-06-26 |
| 400719 | 79168 | 1985-06-26 | 1986-06-26 |
| 401448 | 49600 | 1985-06-26 | 1986-06-26 |
| 427374 | 40000 | 1985-06-26 | 1986-06-26 |
| 432024 | 40000 | 1985-06-26 | 1986-06-26 |
| 432654 | 40000 | 1985-06-26 | 1986-06-26 |
| 438461 | 44451 | 1985-06-26 | 1986-06-26 |
| 446228 | 42733 | 1985-06-26 | 1986-06-26 |
| 447391 | 62381 | 1985-06-26 | 1986-06-26 |
| 448823 | 40000 | 1985-06-26 | 1986-06-26 |
| 452355 | 40000 | 1985-06-26 | 1986-06-26 |
| 453590 | 61615 | 1985-06-26 | 1986-06-26 |
| 456521 | 40000 | 1985-06-26 | 1986-06-26 |
| 464415 | 48955 | 1985-06-26 | 1986-06-26 |
| 467901 | 52349 | 1985-06-26 | 1986-06-26 |
| 472895 | 40000 | 1985-06-26 | 1986-06-26 |
| 476501 | 40000 | 1985-06-26 | 1986-06-26 |
| 477079 | 40000 | 1985-06-26 | 1986-06-26 |
| 478934 | 55054 | 1985-06-26 | 1986-06-26 |
| 480301 | 44177 | 1985-06-26 | 1986-06-26 |
| 484507 | 40000 | 1985-06-26 | 1986-06-26 |
| 486187 | 40000 | 1985-06-26 | 1986-06-26 |
| 491159 | 46034 | 1985-06-26 | 1986-06-26 |
| 493154 | 40000 | 1985-06-26 | 1986-06-26 |
| 498140 | 81909 | 1985-06-26 | 1986-06-26 |
| 498565 | 72853 | 1985-06-26 | 1986-06-26 |
+--------+--------+------------+------------+
51 rows in set (1.08 sec)
用explain分析一下:
mysql> explain select * from salaries where to_date = '1986-06-26';
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | salaries | ALL | NULL | NULL | NULL | NULL | 2838426 | Using where | # Extra使用where。key为Null,在2838426条数据中找51条记录用时1.08s
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
为to_date字段加索引:
mysql> create index to_date on salaries(to_date);
Query OK, 0 rows affected (5.31 sec)
Records: 0 Duplicates: 0 Warnings: 0 创建索引会耗时,索然提升了查询速率,但是更新添加动作会效率降低
现在看一下表结构:
mysql> desc salaries;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| salary | int(11) | NO | | NULL | |
| from_date | date | NO | PRI | NULL | |
| to_date | date | NO | MUL | NULL | | MUL表示非唯一索引
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec) mysql> show create table salaries;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| salaries | CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`),
KEY `to_date` (`to_date`), # 创建了索引key为to_date
CONSTRAINT `salaries_ibfk_1` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
再次查询:
mysql> select * from salaries where to_date = '1986-06-26';
+--------+--------+------------+------------+
| emp_no | salary | from_date | to_date |
+--------+--------+------------+------------+
| 25676 | 40000 | 1985-06-26 | 1986-06-26 |
| 28757 | 40000 | 1985-06-26 | 1986-06-26 |
| 30860 | 64620 | 1985-06-26 | 1986-06-26 |
| 69209 | 40000 | 1985-06-26 | 1986-06-26 |
| 80550 | 45292 | 1985-06-26 | 1986-06-26 |
| 91204 | 47553 | 1985-06-26 | 1986-06-26 |
| 96140 | 52908 | 1985-06-26 | 1986-06-26 |
| 208352 | 42989 | 1985-06-26 | 1986-06-26 |
| 213109 | 90133 | 1985-06-26 | 1986-06-26 |
| 217498 | 80247 | 1985-06-26 | 1986-06-26 |
| 219462 | 83880 | 1985-06-26 | 1986-06-26 |
| 223150 | 40000 | 1985-06-26 | 1986-06-26 |
| 227694 | 73897 | 1985-06-26 | 1986-06-26 |
| 232856 | 73126 | 1985-06-26 | 1986-06-26 |
| 237619 | 56982 | 1985-06-26 | 1986-06-26 |
| 244087 | 40000 | 1985-06-26 | 1986-06-26 |
| 253472 | 72004 | 1985-06-26 | 1986-06-26 |
| 257395 | 40000 | 1985-06-26 | 1986-06-26 |
| 261811 | 40000 | 1985-06-26 | 1986-06-26 |
| 268968 | 40000 | 1985-06-26 | 1986-06-26 |
| 269331 | 40000 | 1985-06-26 | 1986-06-26 |
| 274805 | 40000 | 1985-06-26 | 1986-06-26 |
| 279432 | 74530 | 1985-06-26 | 1986-06-26 |
| 285685 | 83198 | 1985-06-26 | 1986-06-26 |
| 286745 | 44082 | 1985-06-26 | 1986-06-26 |
| 290901 | 49876 | 1985-06-26 | 1986-06-26 |
| 400719 | 79168 | 1985-06-26 | 1986-06-26 |
| 401448 | 49600 | 1985-06-26 | 1986-06-26 |
| 427374 | 40000 | 1985-06-26 | 1986-06-26 |
| 432024 | 40000 | 1985-06-26 | 1986-06-26 |
| 432654 | 40000 | 1985-06-26 | 1986-06-26 |
| 438461 | 44451 | 1985-06-26 | 1986-06-26 |
| 446228 | 42733 | 1985-06-26 | 1986-06-26 |
| 447391 | 62381 | 1985-06-26 | 1986-06-26 |
| 448823 | 40000 | 1985-06-26 | 1986-06-26 |
| 452355 | 40000 | 1985-06-26 | 1986-06-26 |
| 453590 | 61615 | 1985-06-26 | 1986-06-26 |
| 456521 | 40000 | 1985-06-26 | 1986-06-26 |
| 464415 | 48955 | 1985-06-26 | 1986-06-26 |
| 467901 | 52349 | 1985-06-26 | 1986-06-26 |
| 472895 | 40000 | 1985-06-26 | 1986-06-26 |
| 476501 | 40000 | 1985-06-26 | 1986-06-26 |
| 477079 | 40000 | 1985-06-26 | 1986-06-26 |
| 478934 | 55054 | 1985-06-26 | 1986-06-26 |
| 480301 | 44177 | 1985-06-26 | 1986-06-26 |
| 484507 | 40000 | 1985-06-26 | 1986-06-26 |
| 486187 | 40000 | 1985-06-26 | 1986-06-26 |
| 491159 | 46034 | 1985-06-26 | 1986-06-26 |
| 493154 | 40000 | 1985-06-26 | 1986-06-26 |
| 498140 | 81909 | 1985-06-26 | 1986-06-26 |
| 498565 | 72853 | 1985-06-26 | 1986-06-26 |
+--------+--------+------------+------------+
51 rows in set (0.00 sec) # 创建索引后同样的查询条件从1.08s变为了0.00s,惊讶吧
explain分析:
mysql> explain select * from salaries where to_date = '1986-06-26';
+----+-------------+----------+------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | salaries | ref | to_date | to_date | 3 | const | 51 | NULL | key从Null变味了索引字段to_date,row从两百多万变为了51
+----+-------------+----------+------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
这个demo从数据上直观的体现了索引带来的查询效率提升有多可观,但是索引也是有利必有害,更多索引的底层知识可以参考这位大牛的博客:https://www.cnblogs.com/Aiapple/p/5693239.html
在Mysql中使用索引的更多相关文章
- MySQL(五) MySQL中的索引详讲
序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...
- 一、MySQL中的索引 二、MySQL中的函数 三、MySQL数据库的备份和恢复 四、数据库设计和优化(重点)
一.MySQL中的索引###<1>索引的概念 索引就是一种数据结构(高效获取数据),在mysql中以文件的方式存在.存储建立了索引列的地址或者指向. 文件 :(以某种数据 结构存放) 存放 ...
- MySQL中的索引详讲
一.什么是索引?为什么要建立索引? 索引用于快速找出在某个列中有一特定值的行,不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行,表越大,查询数据所花费的时间就越多,如果表中查询的 ...
- mysql 中添加索引的三种方法
原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...
- (转)MySQL中的索引详讲
序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...
- MySQL中是索引
MySQL中是索引: --.唯一索引: 一行中的内容不能一样, create t2( id int , num int, unique weiyisuiyin (id,num) ) --唯一; --约 ...
- 一步一步带你入门MySQL中的索引和锁 (转)
出处: 一步一步带你入门MySQL中的索引和锁 索引 索引常见的几种类型 索引常见的类型有哈希索引,有序数组索引,二叉树索引,跳表等等.本文主要探讨 MySQL 的默认存储引擎 InnoDB 的索引结 ...
- MySQL中的索引优化
MySQL中的SQL的常见优化策略 MySQL中的索引优化 MySQL中的索引简介 过多的使用索引将会造成滥用.因此索引也会有它的缺点.虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行 ...
- MySQL中的索引简介
MySQL中的SQL的常见优化策略 MySQL中的索引优化 MySQL中的索引简介 一. 索引的优点 为什么要创建索引?这是因为,创建索引可以大大提高系统的查询性能. 第一.通过创建唯一性索引,可以保 ...
- java面试一日一题:讲下mysql中的索引
问题:请讲下mysql中的索引 分析:mysql中有很多索引,要对对这些索引有所掌握,还要弄清楚每种索引的本质? 回答要点: 主要从以下几点去考虑 1.索引的本质是什么 2.mysql的索引分类: 3 ...
随机推荐
- 关于Server2008 R2日志的查看
Server 2008 r2通过 系统事件查看器 分析日志: 查看 系统 事件: 事件ID号: 审计目录服务访问 4934 - Active Directory 对象的属性被复制 4935 -复制失败 ...
- B站动手学深度学习第十八课:seq2seq(编码器和解码器)和注意力机制
from mxnet import nd h_forward = nd.array([1,2]) h_backward = nd.array([3,4]) h_bi = nd.concat(h_for ...
- Vue组件v-if新渲染的组件不更新
Vue组件v-if新渲染的组件不更新:可能原因是Vue识别到是相似组件(高度相似甚至相同)不会更新元素.给原来的组件和新组件分别给不同的key值让Vue识别为不同的组件.
- iOS创建带删除线和价钱符号的Label
效果显示如下: 只需要子类化Label,重写DrawRect()方法即可: #import "MyLabel.h" @implementation MyLabel - (insta ...
- Warning:detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd".
执行kubeadm init集群初始化时遇到: [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker ...
- 一篇文章搞懂android存储目录结构
前言 前两天因为开发一个app更新的功能,我将从服务器下载的apk文件放在了内部存储目录(测试手机为小米,路径为:data/user/0/packagename/files)下面,然后安装的时候一直安 ...
- 奥比中光Astra Pro的使用(1)
在ubuntu上的使用 首先下载SDK以及OpenNI安装包,下载地址: 解压两个安装包 切换目录到AstraSDK-Linux下的install目录,并输入命令:sudo sh ./install. ...
- day17 包与相对路径
""" 今日内容: 1.导入模块的细节 2.包的概念及使用 3.包的相对导入 """ """ 1.导入模块的细 ...
- CentOS学习之NTP服务配置详解
详解centos7下ntp服务配置 一.ntp服务是什么 1.定义 NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议. 2.发展 首次记载在 ...
- [CF544E]Remembering Strings_状压dp
E. Remembering Strings 题目大意: You have multiset of n strings of the same length, consisting of lowerc ...