数据库基础(3)索引与视图

-- 索引

-- 有利于用户的搜索.
-- 相当于一本书的目录,通过目录可以快速的找到对应的资源
-- 在数据库方面,查询一张表有两种检索方式
-- 第一种方式:全表扫描
-- 第二种方式:根据索引检索(效率极高)
-- 效率更高原因:缩小了检索范围

-- 索引使用情景:

-- 1.数据大量
-- 2.数据很少修改 原因:修改数据的时,需要修改索引对象,索引是需要维护的.
-- 3.该字段经常出现在where语句中

-- 添加索引(添加索引对象)

create index 索引名 on 表名(字段名);

-- 删除索引(删除索引对象)

drop index index_name on table_name;

-- 注意:主键和unique约束的字段会自动添加索引

mysql> select * from user;
+----+-----------+---------+
| id | name      | fund    |
+----+-----------+---------+
|  1 | 张三      |   18000 |
|  2 | 李四      |    6000 |
|  3 | 周润发    | 2000000 |
|  5 | 周星星    | 8880000 |
+----+-----------+---------+
4 rows in set (0.04 sec)
mysql> desc user;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int         | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
| fund  | int         | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

-- 查看执行计划; 从type为all可看出这是全表查询

mysql> explain select * from user where fund = 6000;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | user  | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    4 |    25.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

-- 添加索引

mysql> create index user_fund_index on user(fund);
Query OK, 0 rows affected (0.24 sec)
Records: 0  Duplicates: 0  Warnings: 0

-- 查看执行计划; 从type为ref可看出这是索引查询,key也改为了user_fund_index

mysql> explain select * from user where fund = 6000;
+----+-------------+-------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys   | key             | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | user  | NULL       | ref  | user_fund_index | user_fund_index | 5       | const |    1 |   100.00 | NULL  |
+----+-------------+-------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

-- 删除索引

mysql> drop index user_fund_index on user;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select * from user where fund = 6000;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | user  | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    4 |    25.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

-- 底层原理简单介绍
-- 创建索引相当于创建了一个索引对象(BTree) 该对象会保存在一个物理空间当中(内存,硬盘等);
-- 索引对象里有什么呢?
-- 里面是个数据经过排序、分区的列表,并且每个数据对应会有一个物理地址,如:'6000 = 0x3';
-- 根据索引查询时

select * from user where fund = '6000';
相当于
select * from user where 物理地址 = 0x3;

-- 通过索引检索到数据之后,获取到关联的物理地址
-- 有物理地址 数据库会直接定位,直接找到该数据

-- 索引的分类

-- 单一索引:给单个字段添加索引
-- 复合索引:给多个字段联合起来添加1个索引
-- 主键索引:主键上会自动添加索引
-- 唯一索引:有unique约束的字段会自动添加索引

-- 索引什么时候会失效?

-- 其中一个,模糊查询的时候,首字节就是通配符%的时候,索引失效

mysql> select * from user where fund = '%6%';
Empty set, 1 warning (0.00 sec)

-- 视图

-- 封装一个select语句
-- 修改视图里的数据,会把原表数据同时修改.

-- 增加字段更新数据

alter table user add phone varchar(20);
update user set phone = '22345678' where id = 2;
update user set phone = '32345678' where id = 3;
update user set phone = '42345678' where id = 5;
mysql> select * from user;
+----+-----------+---------+----------+
| id | name      | fund    | phone    |
+----+-----------+---------+----------+
|  1 | 张三      |   18000 | 12345678 |
|  2 | 李四      |    6000 | 22345678 |
|  3 | 周润发    | 2000000 | 32345678 |
|  5 | 周星星    | 8880000 | 42345678 |
+----+-----------+---------+----------+
4 rows in set (0.00 sec)

-- 视图创建

create view myview as
    select name,phone from user;

-- 视图查看

mysql> select * from myview;
+-----------+----------+
| name      | phone    |
+-----------+----------+
| 张三      | 12345678 |
| 李四      | 22345678 |
| 周润发    | 32345678 |
| 周星星    | 42345678 |
+-----------+----------+
4 rows in set (0.00 sec)

你可能感兴趣的:(数据库基础(3)索引与视图)