Mysql数据库的索引和视图详解

Mysql数据库的索引和视图详解

Mysql数据库的索引和视图详解

索引的概念

数据库的索引与书籍中的目录类似
在一本书中,无需阅读整本书,利用目录就可以快速查找所需信息
书中的目录是一个词语列表,其中注明了包含各个词的页码
数据库索引
在数据库中,索引数据库程序无需对整个表进行扫描,就可以在其中找到所需数据
数据库中的索引是某个表中一列或若干列的集合,以及物理标识这些值的数据页的逻辑指针清单

索引的作用

设置了合适的索引之后,数据库利用葛总快速的定位技术,能够大大加快查询速率
特别是当表很大时,或者查询涉及到多个表时,使用索引可使查询加快成千倍
可以降低数据库的IO成本,并且索引还可以降低数据库的排序成本
通过创建唯一索引保证数据表数据的唯一性
可以加快表与表之间的连接
在使用分组和排序时,可大大减少分组和排序时间

索引分类

普通索引
这是最基本的索引类型,而且它没有唯一性的限制
唯一性索引
索引的列的所有值都只能出现一次,即必须唯一
主键
主键是一种唯一性索引,但它必须指定为“PRIMARY KEY”
全文索引
全文索引可以在VARCHAR或者TEXT类型的列上创建

创建索引的原则依据

表的主键,外键必须有索引
数据量超过300行的表应该有索引
经常与其他表进行连接的表,在连接字段上应该建立索引
唯一性太差的字段不适合建立索引
更新太频繁的字段不适合创建索引
经常出现在Where字句中的字段,特别是大表的字段,应该建立索引
索引应该建在选择性高的字段上
索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引

索引原理图

Mysql数据库的索引和视图详解

普通索引

普通索引结构语句

create index 索引名字 on tablename(列的列表

create index 索引名字 on tablename(列的列表)
[ ~]# mysql -u root -p #进入mysql数据库
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> show databases;  #查看所有数据库

mysql> use school; #创建school数据库
Database changed
mysql> create table info (    #创建数据表
id int(4) not null primary key auto_increment, #int类型整型为4,不能为空,主键索引,数值自然增长
name varchar(10) not null,  #varchar字符串不能为空
address varchar(50) default ‘nanjing‘,  #字符串默认是nanjing
age int(3) not null); #int类型

Query OK, 0 rows affected (0.05 sec)

mysql> insert into info (name,address,age) values (‘zhangsan‘,‘beijing‘,20),(‘lisi‘,‘shanghai‘,22);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * form info; #查看数据表
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘form info‘ at line 1
mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> desc info; #查看表结构
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(4)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| address | varchar(50) | YES  |     | nanjing |                |
| age     | int(3)      | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> create index index_age on info (age); #创建索引固定搭配,index_age索引作用在info表中的age列
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info; #查看数据表中的索引
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY   |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | index_age |            1 | age         | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> drop index index_age on info; #删除数据表中的index_age的索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY  |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

唯一索引

唯一索引结构语句

create unique index 索引的名字 on tablename (列的列表)

mysql> create unique index unique_name on info (name); #创建唯一索引,create unique index固定搭配起个名字,作用在info的name列中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY     |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | unique_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> alter table info add unique index index_name (name); #另一种方法alter table info add 唯一索引 索引名字,作用在name列名中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

定义索引的三种方式

1.创建表的时候 直接定义
2.create index 索引名称 on 表名 (列名1,列名2);列名可以是多个
3.alter table 表名 add index 索引名称 (列名);

mysql> alter table info add unique index index_name (name); #另一种方法alter table info add 唯一索引 索引名字,作用在name列名中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

mysql> create table user (
    -> id int(4) not null primary key auto_increment,
    -> name varchar(10) not null,
    -> score decimal not null,
    -> hobby int(2) not null default ‘1‘,
    -> index index_score (score));  #在创建表的时候可以直接定义索引
Query OK, 0 rows affected (0.05 sec)

mysql> desc user;
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | int(4)        | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)   | NO   |     | NULL    |                |
| score | decimal(10,0) | NO   | MUL | NULL    |                |
| hobby | int(2)        | NO   |     | 1       |                |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

做两张表做索引查询,两张表合在一起查看

#要对应着列名填写数据
mysql> insert into user (name,score,hobby) values (‘test01‘,88,1),(‘stu01‘,99,2),(‘wangwu‘,77,3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from user;
+----+--------+-------+-------+
| id | name   | score | hobby |
+----+--------+-------+-------+
|  1 | test01 |    88 |     1 |
|  2 | stu01  |    99 |     2 |
|  3 | wangwu |    77 |     3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)

再创建一个表做与上一个表做相连查询

mysql> create table hob (  
    -> id int (2) not null primary key,
    -> hob_name varchar(10) not null);
Query OK, 0 rows affected (0.04 sec)

mysql> desc hob;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(2)      | NO   | PRI | NULL    |       |
| hob_name | varchar(10) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into hob (id,hob_name) values (1,‘看书‘),(2,‘运动‘),(3,‘跑步‘);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from hob;
+----+----------+
| id | hob_name |
+----+----------+
|  1 | 看书     |
|  2 | 运动     |
|  3 | 跑步     |
+----+----------+
3 rows in set (0.00 sec)

mysql> insert into user (name,score,hobby) values (‘zhaoliu‘,66,2); #在user表中再插一行数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from user inner join hob on user.hobby=hob.id; #把user这种表加入到hob表中,user的hobby对应hob里面的id
+----+---------+-------+-------+----+----------+
| id | name    | score | hobby | id | hob_name |
+----+---------+-------+-------+----+----------+
|  1 | test01  |    88 |     1 |  1 | 看书     |
|  2 | stu01   |    99 |     2 |  2 | 运动     |
|  3 | wangwu  |    77 |     3 |  3 | 游     |
|  4 | zhaoliu |    66 |     2 |  2 | 运动     |
+----+---------+-------+-------+----+----------+
4 rows in set (0.00 sec)

看这个表数据觉得不合理,现在需求只要名字和爱好

mysql> select user.name,hob.hob_name from user inner join hob on user.hobby=hob.id;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 跑步     |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

表名别名关联查询

mysql> select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 跑步     |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

创捷视图

创建视图结构语句

create view 视图名 as
视图建立一个映射,把结果呈现出来,真实的数据还在原有表中

mysql> create view view_user as select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id;

Query OK, 0 rows affected (0.00 sec)
mysql> select * from view_user;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 跑步    |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

全文索引

给长的字段,文段做索引

mysql> create fulltext index full_addr on info (address);
Query OK, 0 rows affected, 1 warning (0.21 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | full_addr  |            1 | address     | NULL      |           2 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

组合索引

mysql> create index index_name_score on user (name,score);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from user;
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY          |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_score      |            1 | score       | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            1 | name        | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            2 | score       | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)