当前位置:网站首页>Mysql中的索引与视图
Mysql中的索引与视图
2022-04-23 05:54:00 【MiMenge】
索引
什么是索引
数据库中有两种检索表方式:
-
全表扫描:(表中数据过多时会有效率问题)
-
索引检索:(效率高)
原理:缩小了扫描文件的范围。
注意
-----索引虽然可以提高检索效率,但不可随便添加索引,因为索引也是数据库中的对象,也需要不断地维护,有维护成本的。数据一旦被更改,索引也可能被更改
什么情况下添加索引
- 数据量庞大(根据客户的需求,根据线上的环境)
- 该字段很少dml操作。(因为字段进行修改操作,索引也需要维护)
- 该字段经常出现在where子句中。(经常根据某个字段查询)
尽量根据主键检索
创建一个索引
create index <索引名称> on <表名>([字段名]);
mysql> create index yin on pay(id);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select stu_class,stu_name from t_student where stu_class = 2;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | t_student | NULL | ALL | NULL | NULL | NULL | NULL | 8 | 12.50 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
删除索引
drop index <索引名> on <表名>;
查看sql语句的执行计划
explain <语句>;
mysql> explain select * from pay;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | pay | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)
索引的分类
-
单一索引:给单个字段添加索引
-
复合索引:给多个字段添加联合索引
-
主键索引:主键上会自动添加索引
-
唯一索引:有unique约束的字段上会自动添加索引
索引的失效
模糊查询可能会使索引查询失效
---------第一个通配符使用的使%
视图(views)
视图是什么
站在不同的角度去看待数据。(同一张表的数据,通过不同的角度去看待)
注意
:通过视图进行增删改查会影响到原表数据
创建视图
create view <视图名> as select 查询语句;
视图操作
mysql> create view v_student as select stu_id,stu_name,stu_age from t_student;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from v_student;
+--------+----------+---------+
| stu_id | stu_name | stu_age |
+--------+----------+---------+
| 1 | tom | 18 |
| 2 | andy | 17 |
| 3 | zhansan | 16 |
| 4 | jery | 20 |
| 5 | ldy | 19 |
| 6 | lis | NULL |
| 7 | lby | NULL |
| 14 | mah | 19 |
+--------+----------+---------+
8 rows in set (0.00 sec)
视图的作用
视图可以隐藏表的实现细节。保密级别较高的系统,数据库只对外提供相关视图,我们只能对视图进行操作
版权声明
本文为[MiMenge]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_53584564/article/details/123661515
边栏推荐
猜你喜欢
卷积神经网络实现CIFAR100数据集分类
基于Keras的时装分类案例
VHDL finite state machine (FSM) code example
逻辑回归原理及代码实现
查漏补缺(六)
Analysis of fixed point PID code of FOC motor Library
Qt 给应用程序加图标
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
深蓝学院激光slam理论与实践 -第二章(里程计标定)作业
[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview
随机推荐
汇编 32位无符号加法计算器
VHDL 有限状态机(FSM) 代码示例
Log writing method (with time)
Shell脚本 单引号、双引号和反引号的区别
赛氪-二进制
HDU-Tunnel Warfare
FOC电机库 定点PID代码分析
特效案例收集:鼠标星球小尾巴
赛氪-zeal
Set up a personal blog of jpress
[UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
查漏补缺(五)
Eigen 库常用基本用法 备忘
Running QT program in visual Stdio
FOC single resistance sampling position loop control servo motor
v-for下定时给图片添加动画
逻辑回归原理及代码实现
Shell脚本的通配符和特殊符号
C语言中volatile的使用
约瑟夫序列 线段树 O(nlogn)