第五十七集 MySQL 索引 方便管理


## 索引的概念

●索引是一个排序的列表,在这个列表中存储着索引的值和包含这个值的数据所在行的物理地址(类似于C语言的链表通过指针指向数据记录的内存地址)。

●使用索引后可以不用扫描全表来定位某行的数据,而是先通过索引表找到该行数据对应的物理地址然后访问相应的数据,因此能加快数据库的查询速度。

●索引就好比是一本书的目录,可以根据目录中的页码快速找到所需的内容。

●索引是表中一列或者若干列值排序的方法。

●建立索引的目的是加快对表中记录的查找或排序

索引的应用

表的主键、外键必须要索引,主键和索引值都具有唯一性,有助于查询,外键关联另一个表的主键,添加索引多表查询时可以快速定位
数据记录超过300行时要有索引,否则数据量太大,每次查询都要把表遍历一遍,会影响数据库的性能
经常与其他表进行连接的表,在连接字段上应该建立索引
唯一性差的字段不适合建立索引
更新太频繁的字段不适合建立索引
经常初见where判断子句中的字段,需要建立索引
索引应该建在选择性高的字段上,如果很少的字段拥有相同值,既有很多独特值,则选择性很高
索引应该建在小字段上,对于大的文本字段甚至超长字段,不要键索引

索引的作用

●设置了合适的索引之后,数据库利用各种快速定位技术,能够大大加快查询速度,这是创建索引的最主要的原因。

●当表很大或查询涉及到多个表时,使用索引可以成千上万倍地提高查询速度。

●可以降低数据库的IO成本,并且索引还可以降低数据库的排序成本。

●通过创建唯一(键)性索引,可以保证数据表中每一行数据的唯一性。

●可以加快表与表之间的连接。

●在使用分组和排序时,可大大减少分组和排序的时间。

索引的副作用:

●索引需要占用额外的磁盘空间。
对于 MyISAM 引擎而言,索引文件和数据文件是分离的,索引文件用于保存数据记录的地址。
而 InnoDB 引擎的表数据文件本身就是索引文件。
●在插入和修改数据时要花费更多的时间,因为索引也要随之变动。

索引的分类

普通索引

最基本的索引类型,没有唯一性之类的限制

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| zsx                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use zsx;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_zsx |
+---------------+
| zsx2          |
| zsx3          |
| xuyichishi    |
+---------------+
3 rows in set (0.00 sec)

mysql> select * from zsx2;
+----+---------+-----------+---------+
| id | name    | score_new | address |
+----+---------+-----------+---------+
|  1 | lisi    | 99        | 上海    |
|  2 | zhansan | 50        | 杭州    |
|  3 | wangwu  | 20        | 杭州    |
|  4 | xuyi    | 40        | 南京    |
|  5 | hh      | 50        | 湖北    |
+----+---------+-----------+---------+
5 rows in set (0.00 sec)
mysql> create index address_index on zsx2(address);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show create table zsx2\G;
*************************** 1. row ***************************
       Table: zsx2
Create Table: CREATE TABLE "zsx2" (
  "id" int(11) NOT NULL,
  "name" varchar(10) NOT NULL,
  "score_new" varchar(3) DEFAULT NULL,
  "address" varchar(40) DEFAULT NULL,
  PRIMARY KEY ("id"),
  KEY "address_index" ("address")				
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

修改表方式创建索引

alter table 表名 add index 索引名 (列名);
mysql> alter table zsx2 add index name_index (name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table zsx2\G;
*************************** 1. row ***************************
       Table: zsx2
Create Table: CREATE TABLE "zsx2" (
  "id" int(11) NOT NULL,
  "name" varchar(10) NOT NULL,
  "score_new" varchar(3) DEFAULT NULL,
  "address" varchar(40) DEFAULT NULL,
  PRIMARY KEY ("id"),
  KEY "address_index" ("address"),
  KEY "name_index" ("name")					
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

创建表的时候指定索引

create table 表名(字段1 数据类型,字段2 数据类型………… ,index 索引名 (列名));

mysql> alter table zsx2 add index name_index (name);
ERROR 1061 (42000): Duplicate key name 'name_index'
mysql> create table test (id int not null,name varchar(20),index name_inxex (name));
Query OK, 0 rows affected (0.01 sec)

mysql> show create table test\G;
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE "test" (
  "id" int(11) NOT NULL,
  "name" varchar(20) DEFAULT NULL,
  KEY "name_inxex" ("name")						
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

唯一性索引

与普通索引不同于唯一索引列的的每个值都唯一,唯一索引允许为空,但是如果用组合索引创建,则列值的组合必须唯一,添加唯一键将自动添加唯一索引

直接创建唯一索引
create unique index 索引名 on 表名 (列名);
mysql> create unique index zsx_index on test(id);
Query OK, 0 rows affected, 1 warning (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> show create table test\G;
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE "test" (
  "id" int(11) NOT NULL,
  "name" varchar(20) DEFAULT NULL,
  UNIQUE KEY "whd_index" ("id"),
  KEY "name_inxex" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

创建表时指定唯一索引

mysql> create table test1 (id int not null,name varchar(20),address varchar(40),index address_index (address));
Query OK, 0 rows affected (0.00 sec)
mysql> show create table test1\G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE "test1" (
  "id" int(11) NOT NULL,
  "name" varchar(20) DEFAULT NULL,
  "address" varchar(40) DEFAULT NULL,
  KEY "address_index" ("address")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

修改表方式创建

alter table 表名 add unique 索引名 (列名);

mysql> alter table test1 add unique name_index (name);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test1\G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE "test1" (
  "id" int(11) NOT NULL,
  "name" varchar(20) DEFAULT NULL,
  "address" varchar(40) DEFAULT NULL,
  UNIQUE KEY "name_index" ("name"),
  KEY "address_index" ("address")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

主键索引

是一种特殊的唯一索引,必须指定为primary key


创建表的时候指定
mysql> create table test2 (id int,name varchar(40),primary key(id));
Query OK, 0 rows affected (0.01 sec)

mysql> show create table test2\G
*************************** 1. row ***************************
       Table: test2
Create Table: CREATE TABLE "test2" (
  "id" int(11) NOT NULL,
  "name" varchar(40) DEFAULT NULL,
  PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

修改表方式时指定
alter table 表名 on primary key (列名);

组合索引

可以在单列或多列上创建的索引
create table 表名 (字段名1 数据类型,字段民2 数据类型,字段名3 数据类型,index 索引名 (列名1,列名2,列名3));


mysql> create table test3 (id int not null,name varchar(40),age varchar(3),address varchar(20),index id_index (id,name,agge));
Query OK, 0 rows affected (0.00 sec)

mysql> show create table test3\G
*************************** 1. row ***************************
       Table: test3
Create Table: CREATE TABLE "test3" (
  "id" int(11) NOT NULL,
  "name" varchar(40) DEFAULT NULL,
  "age" varchar(3) DEFAULT NULL,
  "address" varchar(20) DEFAULT NULL,
  KEY "id_index" ("id","name","age")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

全文索引

适合在进行模糊查询的时候使用,可用于在一片文章中检索文本信息

创建表时指定索引
create table 表名 (字段1 数据类型,字段2 数据类型[,……],fulltext 索引名 (列名));

直接创建索引
create fulltext index 索引名 on 表名(列名):

修改表方式创建
alter table 表名 add fulltext 索引名 (列名);

使用全文索引查询
select * from 表名 where match(列名) against('查询内容');

查看索引

show index from 表名;
show index from 表名\G;
show keys from 表名;
show keys from 表名\G;

mysql> show index from test;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test  |          0 | test_index |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| test  |          0 | id_index   |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| test  |          0 | whd_index  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| test  |          1 | name_inxex |            1 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)

各字段的含义如下:

Table:表的名称
Non_unique:如果索引内容唯一,则为0;如果不唯一,则为1
Key name:索引的名称
Seq_in_index:索引中的列序号,从1开始
Column_name:列名称
Collation:列以什么方式存储在索引中。在MySQL中,有值’A’(升序)或NULL (无分类)
Cardinality:索引中唯一值数目的估计值
Sub part:如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为NULL
Packed:指示关键字如何被压缩。如果没有被压缩,则为NULL
Null:如果列含有NULL,则含有YES。 如果没有,则该列含有NO
Index_type:用过的索引方法(BTREE,FULLTEXT,HASH,RTREE)
Comment:备注

删除索引

drop index 索引名 on 表名; #直接修改索引
alter table 表名 drop index 索引名; #就该方式删除索引
alter table 表名 drop primary key; #删除主键索引

mysql> show create table test\G;
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE "test" (
  "id" int(11) NOT NULL,
  "name" varchar(20) DEFAULT NULL,
  UNIQUE KEY "test_index" ("id"),
  UNIQUE KEY "id_index" ("id"),
  UNIQUE KEY "whd_index" ("id"),
  KEY "name_inxex" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> alter table test drop index whd_index;
Query OK, 0 rows affected (0.01 sec)
mysql> drop index test_index on test;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test\G;
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE "test" (
  "id" int(11) NOT NULL,
  "name" varchar(20) DEFAULT NULL,
  UNIQUE KEY "id_index" ("id"),
  KEY "name_inxex" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

总结

索引类型:
1、普通索引
2、唯一索引
3、主键索引
4、组合索引
5、全文索引
索引创建方式基本可以归类于:
1、直接创建索引
2、修改表方式创建
3、创建标的时候指定索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值