索引是一种特殊的文件,包含了对数据表中所有记录的引用指针。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常见索引介绍的更多相关文章

  1. MySQL 常见索引类型介绍

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. MySQL 主要索引类型有如下几种: 1.主键索引 2.唯一索引 3.普通索引 4.空间索引 5.全文索引 假设有如下一张 ...

  2. MySQL InnoDB索引介绍以及在线添加索引实例分析

    引言:MySQL之所以能成为经典,不是没有道理的,B+树足矣! 一.索引概念 InnoDB引擎支持三种常见的索引:B+树索引,全文索引和(自适应)哈希索引.B+树索引是传统意义上的索引,构造类似二叉树 ...

  3. MySQL数据库索引介绍

    一.什么是索引 索引是mysql数据库中的一种数据结构,就是一种数据的组织方式,这种数据结构又称为key 表中的一行行数据按照索引规定的结构组织成了一种树型结构,该树叫B+树 二.为何要用索引 优化查 ...

  4. Mysql索引介绍及常见索引(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别

    Mysql索引概念:说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是要 ...

  5. Mysql索引结构及常见索引的区别

    一.Mysql索引主要有两种结构:B+Tree索引和Hash索引 Hash索引 mysql中,只有Memory(Memory表只存在内存中,断电会消失,适用于临时表)存储引擎显示支持Hash索引,是M ...

  6. MYSQL的索引类型:PRIMARY, INDEX,UNIQUE,FULLTEXT,SPAIAL 有什么区别?各适用于什么场合?

    一.介绍一下索引的类型 Mysql常见索引有:主键索引.唯一索引.普通索引.全文索引.组合索引PRIMARY KEY(主键索引) ALTER TABLE `table_name` ADD PRIMAR ...

  7. MySQL中索引和优化的用法总结

    1.什么是数据库中的索引?索引有什么作用? 引入索引的目的是为了加快查询速度.如果数据量很大,大的查询要从硬盘加载数据到内存当中. 2.InnoDB中的索引原理是怎么样的? InnoDB是Mysql的 ...

  8. mysql常用索引

    1.索引 在关系数据库中,索引是一种单独的.物理的对数据库表中一列或多列的值进行排序的一种存储结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单.索引的作用相当 ...

  9. Mysql索引介绍及常见索引的区别

    关于MySQL索引的好处,如果正确合理设计并且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是一个人力三轮车.对于没有索引的表,单表查询可能几十万数据就是瓶颈,而通常大型 ...

随机推荐

  1. SpringCloud 学习(5) --- Zuul(一)基本概念、配置

    [TOC] Spring Cloud eureka:注册中心 服务端:提供注册 客户端:进行注册 ribbon:负载均衡(集群) Hystrix:熔断器,执行备选方案 Feign:远程调用 Zuul: ...

  2. Go 语言解释器 Yaegi

    Yaegi 是一个优雅的 Go 语言解释器,可以执行 Go 脚本和插件. 特性 完整支持 Go 语言规范 用 Go 编写,只使用标准库 简单的解释器 API: New(), Eval(), Use() ...

  3. 2019-2020-1 20199312《Linux内核原理与分析》第二周作业

    c语言代码 // main.c int g(int x) { return x + 4; } int f(int x) { return g(x); } int main(void) { return ...

  4. 45、[源码]-Spring容器创建-执行BeanFactoryPostProcessor

    45.[源码]-Spring容器创建-执行BeanFactoryPostProcessor 5.invokeBeanFactoryPostProcessors(beanFactory);执行BeanF ...

  5. 009——C#全局变量定义

    (一)窗体二定义,static静态 public static byte[] waveform_data = { }; // 数据,在串口接收中变化 public static bool wavefo ...

  6. sparkStreaming 读kafka的数据

    目标:sparkStreaming每2s中读取一次kafka中的数据,进行单词计数. topic:topic1 broker list:192.168.1.126:9092,192.168.1.127 ...

  7. RSA公钥私钥原理及作用

    RSA算法广泛应用与加密与认证两个领域 1.加密(保证数据安全性) 使用公钥加密,需使用私钥解密. 这种广泛应用在保证数据的安全性的方面,用户将自己的公钥广播出去,所有人给该用户发数据时使用该公钥加密 ...

  8. Vue.js 中的 v-cloak 指令

    可以使用 v-cloak 指令设置样式,这些样式会在 Vue 实例编译结束时,从绑定的 HTML 元素上被移除. 当网络较慢,网页还在加载 Vue.js ,而导致 Vue 来不及渲染,这时页面就会显示 ...

  9. TensorFlow(十一):递归神经网络(RNN与LSTM)

    RNN RNN(Recurrent Neural Networks,循环神经网络)不仅会学习当前时刻的信息,也会依赖之前的序列信息.由于其特殊的网络模型结构解决了信息保存的问题.所以RNN对处理时间序 ...

  10. 在默认使用apache中央仓库时, 报错 protocol_version

    https://cloud.tencent.com/developer/ask/136221/answer/241408 2018年6月,为了提高安全性和符合现代标准,不安全的TLS 1.0和1.1协 ...