MySQL——索引结构

索引:用于快速查找数据。

索引是将数据的一些关键信息通过特定的数据结构存储到一片新的空间中,这样在文件查找的时候能快速找到。

mysql索引类型:
  • B+TREE、HASH、R-TREE、FULL TEXT
B+Tree:B+树,MySQL常用的一种索引类型。

哈希索引:基于哈希表实现,取数据的哈希值,把这个哈希值来作为索引。

R-Tree:和地理位置相关信息的索引,例如查询附近的人等

全文索引:例如在一个文本中查找关键词出现的次数等
  • 聚簇(集)索引、非聚簇索引:数据和索引是否存储在一起
innodb这个存储引擎就是聚集索引,因为索引和数据是放在一起的。

而myisam就是非聚集索引,索引和数据分开存放的。
  • 主键索引、二级(辅助)索引
主键索引:主键自带索引,在表中建立主键的同时,会按照主键的次序来将数据进行排序。

二级索引:一张表原来有索引,又添加了一个新的索引,这个新添加的索引就是二级索引
  • 稠密索引、稀疏索引:是否索引了每一个数据项
  • 简单索引、组合索引: 是否是多个字段的索引
  • 左前缀索引:取前面的字符做索引
  • 覆盖索引:从索引中即可取出要查询的数据,性能高

MySQL管理索引:

创建索引:
create index index_name on tb_name(col_name[(length)]) #如果选择作为索引的字段是一个字符串,可以只取这个字符串的一部分

alter table tb_name add index index_name(col_name)
mysql> create index index_name on students(name(5));
Query OK, 0 rows affected (0.52 sec)
Records: 0  Duplicates: 0  Warnings: 0

#name(5)表示使用name这个字段来作为索引值,但是只取name这个字符串的前五个字符
删除索引:
drop index index_name on tb_name

alter table tb_name drop index index_name(col_name)
mysql> drop index index_name on students;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from students;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| students |          0 | PRIMARY  |            1 | StuID       | A         |          25 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
查看索引:
show index from tb_name
mysql> show index from students;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| students |          0 | PRIMARY  |            1 | StuID       | A         |          25 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

table:当前执行操作的表名

Non_unique:是否是唯一键索引

Key_name:索引名,说明是主键索引

Column_name:建立索引的字段

Index_type:索引类型,B+树

mysql> create index index_name on students(name(5));
Query OK, 0 rows affected (0.52 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from students;
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| students |          0 | PRIMARY    |            1 | StuID       | A         |          25 |     NULL | NULL   |      | BTREE      |         |               |
| students |          1 | index_name |            1 | Name        | A         |          25 |        5 | NULL   |      | BTREE      |         |               |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

explain工具

explain可以用来判断是否使用了索引,显示执行的细节:
格式:explain Sql语句

mysql> explain select * from students where stuid=20;
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table    | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | students | const | PRIMARY       | PRIMARY | 4       | const |    1 | NULL  |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

id:执行编号,标识select所属的行。如果在语句中没子查询或关联查询,只有唯一的select,每行都将显示1

select_type:查询类型,simple是简单查询。复杂查询:PRIMARY

table:当前正在操作的表

type:表示的是访问类型。 all:全表扫描(表从头到位扫描一遍) ref:表示参考了索引来进行查找

possible_keys:可以使用的索引

key:显示mysql决定采用哪个索引来优化查询

key_len:显示mysql在索引里使用的字节数

ref:当使用索引列等值查询时,与索引列进行等值匹配的对象信息

rows:为了找到所需的行而需要读取的行数,估算值,不精确

extra:附加信息

范例:

mysql> explain select * from students where name like 'S%';
+----+-------------+----------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table    | type  | possible_keys | key        | key_len | ref  | rows | Extra       |
+----+-------------+----------+-------+---------------+------------+---------+------+------+-------------+
|  1 | SIMPLE      | students | range | index_name    | index_name | 17      | NULL |    3 | Using where |
+----+-------------+----------+-------+---------------+------------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from students where name like '%S';
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | students | ALL  | NULL          | NULL | NULL    | NULL |   25 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from students where name like '%S%';
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | students | ALL  | NULL          | NULL | NULL    | NULL |   25 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

查找以某个字母开头的可以利用索引,例如like S%或 %S%就不能使用到索引

MySQL很智能,如果发现利用索引的效率还没有不利用索引的效率高,就会选择不利用索引。
profile工具

显示sql语句执行的详细过程。

set profiling = ON; #设置这个变量的值为ON默认就开启了这个工具

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+
1 row in set, 1 warning (1.60 sec)

开启以后通过:
mysql> show profiles;
可以看到曾今执行过的命令以及花费的时间。

mysql> show profiles;
+----------+------------+------------------------+
| Query_ID | Duration   | Query                  |
+----------+------------+------------------------+
|        1 | 1.63420125 | select @@profiling     |
|        2 | 0.00017425 | select * from students |
+----------+------------+------------------------+
2 rows in set, 1 warning (0.00 sec)

#显示语句的详细执行步骤和时长
Show profile for query num

mysql> Show profile for query 1;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000040 |
| checking permissions | 1.634089 |
| Opening tables       | 0.000010 |
| init                 | 0.000008 |
| optimizing           | 0.000004 |
| executing            | 0.000008 |
| end                  | 0.000003 |
| query end            | 0.000003 |
| closing tables       | 0.000003 |
| freeing items        | 0.000016 |
| cleaning up          | 0.000019 |
+----------------------+----------+
11 rows in set, 1 warning (0.10 sec)

Original: https://www.cnblogs.com/heyongshen/p/16685288.html
Author: 背对背依靠
Title: MySQL——索引结构

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/578596/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Linux 学习笔记

    Linux 学习笔记 作者:Grey 原文地址: 博客园:Linux 学习笔记 CSDN:Linux 学习笔记 说明 本文如果没有特别说明,环境是 CentOS 7, 主要是日常学…

    Linux 2023年5月27日
    0117
  • zabbix5.0报错PHP时区未设置(配置参数”date.timezone”)

    解决办法 : 1、编辑文件/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf,取消注释并设置为所在地时区 vim /etc/opt/rh/rh-p…

    Linux 2023年6月7日
    088
  • LeetCode-1047. 删除字符串中的所有相邻重复项

    题目来源 题目详情 给出由小写字母组成的字符串 S, 重复项删除操作会选择两个相邻且相同的字母,并删除它们。 在 S 上反复执行重复项删除操作,直到无法继续删除。 在完成所有重复项…

    Linux 2023年6月7日
    0125
  • Linux 搭建Apollo

    简介 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用…

    Linux 2023年6月14日
    0122
  • LeetCode 726: 原子的数量-栈和Map的结合以及字符串处理 | Number of Atoms-Combination of stack, map and string processing

    Problem Description Give a chemical formula, return the count of each atom. The count afte…

    Linux 2023年6月13日
    098
  • 配置免密登陆服务器

    前言 原来自己学习的时候在阿里云买自己的学习机,一台主机自己瞎折腾。但是参加工作以后管理的主机越来越多了,上服务器看的频率也越来越频繁,虽然有时候shell管理工具可以很方便的保存…

    Linux 2023年6月14日
    092
  • rsync

    rsync简介 rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。 rsy…

    Linux 2023年6月6日
    088
  • Linux安装mysql8.0

    MySQL 配置MySQL8安装源 sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3….

    Linux 2023年6月6日
    097
  • 每周一个linux命令(ping)

    基础环境 ping命令介绍 ping命令主要用来…

    Linux 2023年6月8日
    092
  • 博客被阮一峰引流后,我对“大数据”的分析与思考

    事情经过 2021年9月3日,一个普通的的早上,我照常打开了我的小博客网站echeverra,看看有没有评论,虽然知道大概率没几个人访问,更鲜有人评论,可还是想打开自己的小窝瞅上那…

    Linux 2023年6月7日
    079
  • chmod 报错 changing permissions of ‘xxx’: Operation not permitted

    chmod的底层实现是 chattr命令,用此命的功能更为强大,甚至可以锁定文件,即使root用户也操作不了此文件。 先使用lsattr查看文件属性 `bashlsattr tem…

    Linux 2023年6月13日
    0104
  • redis的三种集群方式

    博客园 :当前访问的博文已被密码保护 请输入阅读密码: Original: https://www.cnblogs.com/heqiyoujing/p/14494037.htmlA…

    Linux 2023年5月28日
    0102
  • mysql-高可用架构:MHA

    mysql-高可用架构:MHA 1. MHA简介 MHA(Master High Availability)是由日本人yoshinorim开发的一款成熟且开源的MySQL高可用程序…

    Linux 2023年6月13日
    086
  • Linux 下重启 PHP 服务、nginx 服务

    一、重启 PHP 服务 service php-fpm start 开启 service php-fpm stop 停止 service php-fpm restart 重启 二、…

    Linux 2023年6月13日
    083
  • 三种移除list中的元素(可靠)

    /** * 直接使用foreach方法移除list中的元素会抛异常 * Exception in thread "main" java.util.Concurr…

    Linux 2023年6月7日
    090
  • Celery异步任务

    情景: 用户发起request,并等待response返回。在本些views中,可能需要执行一段耗时的程序,那么用户就会等待很长时间,造成不好的用户体验,比如发送邮件、手机验证码等…

    Linux 2023年6月8日
    087
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球