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中使用索引的更多相关文章

  1. MySQL(五) MySQL中的索引详讲

    序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...

  2. 一、MySQL中的索引 二、MySQL中的函数 三、MySQL数据库的备份和恢复 四、数据库设计和优化(重点)

    一.MySQL中的索引###<1>索引的概念 索引就是一种数据结构(高效获取数据),在mysql中以文件的方式存在.存储建立了索引列的地址或者指向. 文件 :(以某种数据 结构存放) 存放 ...

  3. MySQL中的索引详讲

    一.什么是索引?为什么要建立索引? 索引用于快速找出在某个列中有一特定值的行,不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行,表越大,查询数据所花费的时间就越多,如果表中查询的 ...

  4. mysql 中添加索引的三种方法

    原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...

  5. (转)MySQL中的索引详讲

    序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...

  6. MySQL中是索引

    MySQL中是索引: --.唯一索引: 一行中的内容不能一样, create t2( id int , num int, unique weiyisuiyin (id,num) ) --唯一; --约 ...

  7. 一步一步带你入门MySQL中的索引和锁 (转)

    出处: 一步一步带你入门MySQL中的索引和锁 索引 索引常见的几种类型 索引常见的类型有哈希索引,有序数组索引,二叉树索引,跳表等等.本文主要探讨 MySQL 的默认存储引擎 InnoDB 的索引结 ...

  8. MySQL中的索引优化

    MySQL中的SQL的常见优化策略 MySQL中的索引优化 MySQL中的索引简介 过多的使用索引将会造成滥用.因此索引也会有它的缺点.虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行 ...

  9. MySQL中的索引简介

    MySQL中的SQL的常见优化策略 MySQL中的索引优化 MySQL中的索引简介 一. 索引的优点 为什么要创建索引?这是因为,创建索引可以大大提高系统的查询性能. 第一.通过创建唯一性索引,可以保 ...

  10. java面试一日一题:讲下mysql中的索引

    问题:请讲下mysql中的索引 分析:mysql中有很多索引,要对对这些索引有所掌握,还要弄清楚每种索引的本质? 回答要点: 主要从以下几点去考虑 1.索引的本质是什么 2.mysql的索引分类: 3 ...

随机推荐

  1. [CDH] New project for ML pipeline

    启动后台服务: [CDH] Cloudera's Distribution including Apache Hadoop 这里只介绍一些基本的流程,具体操作还是需要实践代码. 一.开发环境配置 JD ...

  2. The working copy is locked due to a previous error.

    SVN报错: The working copy is locked due to a previous error. 不能更新项目代码... 解决方式: 解决:右键你的左侧管理目录中的相关目录,然后点 ...

  3. redis单机版无法启动java程序解决

    1.注释掉 bind 127.0.0.1 2.kill-9 杀死进行 3.redis-server + 配置文件进行重启,

  4. python-Web-django-商城-session存入数据库

    utils: '''用户session''' import datetime from app01.models import Sessions ''' iii = request.session.s ...

  5. 安装ceilometer

    在控制节点上执行 #!/bin/bash MYSQL_ROOT_PASSWD='m4r!adbOP' GNOCCHI_PASSWD='gnocchi1234!' CEILOMETER_PASSWD=' ...

  6. Windows Server 2019远程桌面服务配置和授权激活

    参考Windows Server 2016远程桌面服务配置和授权激活方法可适用于Windows Server 2019     Server 2016默认远程桌面连接数是2个用户,如果多余两个用户进行 ...

  7. 【Matlab开发】MATLAB编译C/C++代码

    在使用MATLAB编译C/C++代码时,C/C++代码中要使用一个mexFunction函数,那么这个函数是如何定义,在编译时又是如何实现的呢?下面我将使用实例进行说明. 如一个简单的函数: doub ...

  8. PHP学习(9)——错误和异常处理

    1.Exception类 这个类是PHP为异常处理提供的内置类.构造函数的两个参数分别是错误消息和错误代码. 除了构造函数之外,该类还提供了如下的内置方法: · getCode() 返回传递给构造函数 ...

  9. prometheus 监控 zookeeper

    1.zookeeper的规则 [root@do1cloud01 prometheus]# cat zookeeper.yml rules: - pattern: "org.apache.Zo ...

  10. MySQL中的聚集索引和辅助索引

    MySQL中的聚集索引和辅助索引 当你定义一个主键时,innodb存储引擎就把他当做聚集索引 如果你没有定义一个主键,则innodb定位到第一个唯一索引,且改索引的所有列值均为非空,就将其当做聚集索引 ...