Mysql常见索引介绍
索引是一种特殊的文件,包含了对数据表中所有记录的引用指针。InnoDB引擎的数据库,其上的索引是表空间的一个组成部分。
(1).索引的优缺点
优点:加快搜索速度,减少查询时间
缺点:索引是以文件的形式存储,如果索引过多,会占用磁盘较大的空间。而且影响insert、update、delete的执行时间。
索引中的数据必须与数据表中的人数据同步,如果索引过多,当表中数据更新,索引也要同步更新,这就降低了效率。
(2).普通索引
最基本的索引,不具备唯一性,仅加快查询速度。
1)创建普通索引
create table [表名] ([字段名] [字段类型] [字段约束],...,[ index | key ] [索引名称]([字段名称]));
mysql> create table com_index1(id int,name varchar(20),key (name));
Query OK, 0 rows affected (0.02 sec) mysql> desc com_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> create table com_index2(id int,name varchar(20),index com_name(name));
Query OK, 0 rows affected (0.11 sec) mysql> desc com_index2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.03 sec) mysql> create table com_index(id int,name varchar(20),index (name));
Query OK, 0 rows affected (0.02 sec) mysql> desc com_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
注意1:索引文件是一表一个,所以在没有外键约束的情况下,只要表内没有重名索引即可。
注意2:在Key列值为MUL的一般是普通索引,可以使用show index from [表名]来确认索引类型,里面有索引类型列如果为BTREE那么就是普通索引。
注意3:如果没有指定索引名,那么默认索引名与字段名一致。
2)添加普通索引
alter table [表名] add [ index | key ] [索引名称]([字段名]);
mysql> alter table com_index1 add key (id);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> alter table com_index2 add index com_id(id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> alter table com_index add index (id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
3)删除普通索引
alter table [表名] drop [ key | index ] [索引名];
mysql> alter table com_index1 drop key name;
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> alter table com_index1 drop index id;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc com_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
(3).唯一索引
设置唯一索引的列中所有值都只能出现一次,允许NULL。唯一索引一般只在创建数据表以及数据表没有数据之前创建或添加,如果已经存在数据,必须保证添加的列所有值具有唯一性。
1)创建唯一索引
create table [表名] ([字段名] [字段类型] [字段约束],...,unique [索引名称]([字段名称]));
mysql> create table uni_index(id int,name varchar(20),unique (id));
Query OK, 0 rows affected (0.12 sec) mysql> desc uni_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
注意:在Key列值为UNI的即是唯一索引。
2)添加唯一索引(尽量不要用,用也尽量在存放数据之前)
alter table [表名] add unique [索引名]([字段名]);
mysql> alter table uni_index add unique (name);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc uni_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
3)删除唯一索引
alter table [表名] drop key [索引名];
mysql> alter table uni_index drop key name;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc uni_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
(4).主键索引
查询数据库按主键查询是最快的,每个数据表只允许存在一个主键,主键必须唯一,不允许为NULL。主键和唯一索引一样,一般只在创建数据表以及数据表没有数据之前创建或添加,如果已经存在数据,必须保证添加的列所有值具有唯一性。
1)创建主键
create table [表名] ([字段名] [字段类型] [字段约束],...,primary key [索引名称]([字段名称]));
create table [表名] ([字段名] [字段类型] [字段约束] [ primary key | key ],[字段名] [字段类型] [字段约束]...);
mysql> create table pri_index1 (id int,primary key (id));
Query OK, 0 rows affected (0.01 sec) mysql> desc pri_index1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec) mysql> create table pri_index2 (id int key);
Query OK, 0 rows affected (0.01 sec) mysql> desc pri_index2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec) mysql> create table pri_index3 (id int primary key);
Query OK, 0 rows affected (0.02 sec) mysql> desc pri_index3;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
注意:在Key列值为PRI的即是主键索引。
2)添加主键(尽量不要用,用也尽量在存放数据之前)
alter table [表名] add primary key [索引名]([字段名]);
mysql> create table pri_index4 (id int);
Query OK, 0 rows affected (0.02 sec) mysql> desc pri_index4;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec) mysql> alter table pri_index4 add primary key (id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc pri_index4;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
3)删除主键
alter table [表名] drop primary key;
mysql> alter table pri_index4 drop primary key;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc pri_index4;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)
(5).复合索引
一个索引可以包含一个或一个以上的列,当索引包含两个及以上的列时,此时的索引被称之为复合索引。基本创建、添加、删除方式没有改变,只是字段名部分可以为一个以上,之间由逗号隔开。
创建
mysql> create table comp_index(id int,name varchar(20), primary key (id,name));
Query OK, 0 rows affected (0.01 sec) mysql> desc comp_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
添加
mysql> create table comp_index1(id int,name varchar(20));
Query OK, 0 rows affected (0.25 sec) mysql> alter table comp_index1 add primary key (id,name);
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc comp_index1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
删除
mysql> alter table comp_index drop primary key;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc comp_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
(6).全文索引
全文索引也称全文检索,是目前搜索引擎使用的一种关键技术。它能够利用多种算法智能分析出文本文字中关键词的频率和重要性,然后按照一定的算法规则智能筛选出想要的结果。3.2开始支持全文索引,但无法正确支持中文;5.7.6开始内置ngram全文检索插件,用来支持中文。
旧版的全文索引只能用在MyISAM数据库引擎的表上,但5.6.24上InnoDB也加入了全文索引。不够只支持char、varchar和text的字段类型。
1)创建全文索引
create table [表名] ([字段名] [字段类型] [字段约束],...,fulltext key [索引名称]([字段名称]));
mysql> create table ful_index(id int,name varchar(20),age varchar(20),fulltext key (name));
Query OK, 0 rows affected (0.25 sec) mysql> desc ful_index;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| age | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec) mysql> show index from ful_index;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
注意:这里Key的值虽然是MUL,但使用show index from [表名]可以看到实际索引类型是FULLTEXT。
2)添加全文索引
alter table [表名] add fulltext key [索引名]([字段名]);
mysql> alter table ful_index add fulltext key (age);
Query OK, 0 rows affected (0.26 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from ful_index;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
| ful_index | 1 | age | 1 | age | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.01 sec)
3)删除全文索引
alter table [表名] drop key [索引名];
mysql> alter table ful_index drop key age;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show index from ful_index;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| ful_index | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
(7).索引创建原则
索引并不是越多越好:1.数据量不大时不需要创建索引;
2.列中值变化种类不多不需要创建索引;
3.经常排序(order by)和分组(group by)的列需要建立索引;
4.列中值需要具有唯一性,可以使用唯一索引或主键索引,但每个表中只能存在一个主键索引,并且不能为NULL。
Mysql常见索引介绍的更多相关文章
- MySQL 常见索引类型介绍
GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. MySQL 主要索引类型有如下几种: 1.主键索引 2.唯一索引 3.普通索引 4.空间索引 5.全文索引 假设有如下一张 ...
- MySQL InnoDB索引介绍以及在线添加索引实例分析
引言:MySQL之所以能成为经典,不是没有道理的,B+树足矣! 一.索引概念 InnoDB引擎支持三种常见的索引:B+树索引,全文索引和(自适应)哈希索引.B+树索引是传统意义上的索引,构造类似二叉树 ...
- MySQL数据库索引介绍
一.什么是索引 索引是mysql数据库中的一种数据结构,就是一种数据的组织方式,这种数据结构又称为key 表中的一行行数据按照索引规定的结构组织成了一种树型结构,该树叫B+树 二.为何要用索引 优化查 ...
- Mysql索引介绍及常见索引(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别
Mysql索引概念:说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是要 ...
- Mysql索引结构及常见索引的区别
一.Mysql索引主要有两种结构:B+Tree索引和Hash索引 Hash索引 mysql中,只有Memory(Memory表只存在内存中,断电会消失,适用于临时表)存储引擎显示支持Hash索引,是M ...
- MYSQL的索引类型:PRIMARY, INDEX,UNIQUE,FULLTEXT,SPAIAL 有什么区别?各适用于什么场合?
一.介绍一下索引的类型 Mysql常见索引有:主键索引.唯一索引.普通索引.全文索引.组合索引PRIMARY KEY(主键索引) ALTER TABLE `table_name` ADD PRIMAR ...
- MySQL中索引和优化的用法总结
1.什么是数据库中的索引?索引有什么作用? 引入索引的目的是为了加快查询速度.如果数据量很大,大的查询要从硬盘加载数据到内存当中. 2.InnoDB中的索引原理是怎么样的? InnoDB是Mysql的 ...
- mysql常用索引
1.索引 在关系数据库中,索引是一种单独的.物理的对数据库表中一列或多列的值进行排序的一种存储结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单.索引的作用相当 ...
- Mysql索引介绍及常见索引的区别
关于MySQL索引的好处,如果正确合理设计并且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是一个人力三轮车.对于没有索引的表,单表查询可能几十万数据就是瓶颈,而通常大型 ...
随机推荐
- 【转】Senior Data Structure · 浅谈线段树(Segment Tree)
本文章转自洛谷 原作者: _皎月半洒花 一.简介线段树 ps: _此处以询问区间和为例.实际上线段树可以处理很多符合结合律的操作.(比如说加法,a[1]+a[2]+a[3]+a[4]=(a[1]+a[ ...
- Docker那些事儿之编排工具docker-compose
前面已经讲解过docker的一些基础使用,镜像创建的操作过程,如果大量容器需要同时部署,一个一个容器进行服务器上的部署,估计要疯掉,在使用上我们需要找到更好更便捷的使用方式,今天要讲解的容器编排工具d ...
- Struts2中There is no Action mapped for namespace错误解决方法
1.我的原有配置 jsp表单提交路径 <form class="layui-form" id="form" action="${ctx }/me ...
- Django rest framework的基本用法
今天在网上看到几篇比较不错的drf基本功能介绍,本来想自己写一篇,后来发现脱不开原作者的思路,遂卒,直接扔链接小伙伴自己去看吧.... 1.django rest framework apiview. ...
- [0, 1] 区间内 n 次独立随机事件的一些问题
问题一 证明:一根1米长的绳子,随机切成 $N$ 刀,变成($N+1$)根绳子,则最短的一根绳子长度的期望为 $\displaystyle \frac{1}{(N+1)^2}$. 证: 引理:当分成 ...
- C# TreeView 右键菜单
方法一: 在winform中,添加一个contextMenuStrip1,设置TreeView的属性ContextMenuStrip为contextMenuStrip1,并为这个contextMenu ...
- Git - Q
GIT :实现版本控制的软件 命令: git init # 初始 git (先执行这个才能执行接下来的) git status # 查看当前文件夹状态 git add 文件名 # 对指定文件进行版本控 ...
- PHP流程控制之嵌套if...else...elseif结构
还记得本章开篇我们讲了一个王思总同学的例子: 王同学是生活极度充满娱乐化和享受生活的人.他抵达北京或者大连的时候做的事,他抵达后做的事情,如下:直线电机参数 半夜到达,先去夜店参加假面舞会 早上抵达, ...
- white-space
white-space 语法: white-space:normal | pre | nowrap | pre-wrap | pre-line 默认值:normal 适用于:所有元素 继承性:有 动画 ...
- MyBatis底层实现原理: 动态代理的运用
转载:https://mp.weixin.qq.com/s/_6nyhaWX15mh3mkj8Lb0Zw Mybatis中声明一个interface接口,没有编写任何实现类,Mybatis就能返回接口 ...