侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

简单了解添加mysql索引的3条原则

数据库  /  管理员 发布于 6年前   321

一,索引的重要性

索引用于快速找出在某个列中有一特定值的行。不使用索引,MySQL必须从第1条记录开始然后读完整个表直到找出相关的行。表越大,花费的时间越多。如果表中查询的列有一个索引,MySQL能快速到达一个位置去搜寻到数据文件的中间,没有必要看所有数据。注意如果你需要访问大部分行,顺序读取要快得多,因为此时我们避免磁盘搜索。

假如你用新华字典来查找“张”这个汉字,不使用目录的话,你可能要从新华字典的第一页找到最后一页,可能要花二个小时。字典越厚呢,你花的时间就越多。现在你使用目录来查找“张”这个汉字,张的首字母是z,z开头的汉字从900多页开始,有了这条线索,你查找一个汉字可能只要一分钟,由此可见索引的重要性。但是索引建的是不是越多越好呢,当然不是,如果一本书的目录分成好几级的话,我想你也会晕的。

二,准备工作

//准备二张测试表 mysql> CREATE TABLE `test_t` ( -> `id` int(11) NOT NULL auto_increment, -> `num` int(11) NOT NULL default '0', -> `d_num` varchar(30) NOT NULL default '0', -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; Query OK, 0 rows affected (0.05 sec) mysql> CREATE TABLE `test_test` ( -> `id` int(11) NOT NULL auto_increment, -> `num` int(11) NOT NULL default '0', -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; Query OK, 0 rows affected (0.05 sec) //创建一个存储过程,为插数据方便 mysql> delimiter | mysql> create procedure i_test(pa int(11),tab varchar(30)) -> begin -> declare max_num int(11) default 100000; -> declare i int default 0; -> declare rand_num int; -> declare double_num char; -> -> if tab != 'test_test' then -> select count(id) into max_num from test_t; -> while i < pa do -> if max_num < 100000 then -> select cast(rand()*100 as unsigned) into rand_num; -> select concat(rand_num,rand_num) into double_num; -> insert into test_t(num,d_num)values(rand_num,double_num); -> end if; -> set i = i +1; -> end while; -> else -> select count(id) into max_num from test_test; -> while i < pa do -> if max_num < 100000 then -> select cast(rand()*100 as unsigned) into rand_num; -> insert into test_test(num)values(rand_num); -> end if; -> set i = i +1; -> end while; -> end if; -> end| Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> show variables like "%pro%"; //查看一下,记录执行的profiling是不是开启动了,默认是不开启的 +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | profiling | OFF | | profiling_history_size | 15 | | protocol_version | 10 | | slave_compressed_protocol | OFF | +---------------------------+-------+ 4 rows in set (0.00 sec) mysql> set profiling=1; //开启后,是为了对比加了索引后的执行时间 Query OK, 0 rows affected (0.00 sec) 

三,实例

1,单表数据太少,索引反而会影响速度

mysql> call i_test(10,'test_t'); //向test_t表插入10条件 Query OK, 1 row affected (0.02 sec) mysql> select num from test_t where num!=0; mysql> explain select num from test_t where num!=0\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test_t type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 10 Extra: Using where 1 row in set (0.00 sec) ERROR: No query specified mysql> create index num_2 on test_t (num); Query OK, 10 rows affected (0.19 sec) Records: 10 Duplicates: 0 Warnings: 0 mysql> select num from test_t where num!=0; mysql> explain select num from test_t where num!=0\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test_t type: index possible_keys: num_2 key: num_2 key_len: 4 ref: NULL rows: 10 Extra: Using where; Using index 1 row in set (0.00 sec) ERROR: No query specified mysql> show profiles; +----------+------------+---------------------------------------------+ | Query_ID | Duration | Query | +----------+------------+---------------------------------------------+ | 1 | 0.00286325 | call i_test(10,'test_t') | //插入十条数据 | 2 | 0.00026350 | select num from test_t where num!=0 | | 3 | 0.00022250 | explain select num from test_t where num!=0 | | 4 | 0.18385400 | create index num_2 on test_t (num) | //创建索引 | 5 | 0.00127525 | select num from test_t where num!=0 | //使用索引后,差不多是没有使用索引的0.2倍 | 6 | 0.00024375 | explain select num from test_t where num!=0 | +----------+------------+---------------------------------------------+ 6 rows in set (0.00 sec) 

解释:

  • id:表示sql执行的顺序
  • select_type:SIMPLE,PRIMARY,UNION,DEPENDENT UNION,UNION RESULT,SUBQUERY,DEPENDENT SUBQUERY,DERIVED不同的查询语句会有不同的select_type
  • table:表示查找的表名
  • type:表示使用索引类型,或者有无使用索引.效率从高到低const、eq_reg、ref、range、index和ALL,其实这个根你sql的写法有直接关系,例如:能用主键就用主键,where后面的条件加上索引,如果是唯一加上唯一索引等
  • possible_keys:可能存在的索引
  • key:使用索引
  • key_len:使用索引的长度
  • ref:使用哪个列或常数与key一起从表中选择行,一般在多表联合查询时会有。
  • rows:查找出的行数
  • Extra:额外说明

前段时间写过一篇博文mysql distinct和group by谁更好,里面有朋友留言,说测试结果根我当时做的测试结果不一样,当时我打比方解释了一下,今天有时间,以例子的形势,更直观的表达出索引的工作原理。

2,where后的条件,order by ,group by 等这样过滤时,后面的字段最好加上索引。根据实际情况,选择PRIMARY KEY、UNIQUE、INDEX等索引,但是不是越多越好,要适度。

3,联合查询,子查询等多表操作时关连字段要加索引

mysql> call i_test(10,'test_test'); //向test_test表插入10条数据 Query OK, 1 row affected (0.02 sec) mysql> explain select a.num as num1,b.num as num2 from test_t as a left join tes t_test as b on a.num=b.num\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: a type: index possible_keys: NULL key: num_2 key_len: 4 ref: NULL rows: 10 Extra: Using index *************************** 2. row *************************** id: 1 select_type: SIMPLE table: b type: ref possible_keys: num_1 key: num_1 key_len: 4 ref: bak_test.a.num //bak_test是数据库名,a.num是test_t的一个字段 rows: 1080 Extra: Using index 2 rows in set (0.01 sec) ERROR: No query specified 

数据量特别大的时候,最好不要用联合查询,即使你做了索引。

上面只是个人的一点小结,抛砖引玉一下。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    简单了解mysql mycat 中间件
    下一条:
    MySQL分表和分区的具体实现方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客