Mysql入门之索引和视图【Mysql数据库基础】

本文深入解析Mysql中的索引和视图概念,包括索引的创建、删除、使用场景及效率对比,重点讲解B+Tree数据结构在索引中的应用,以及视图的创建和操作,揭示其对数据查询的影响。

4.索引

4.1 索引相当于目录,通过目录可以快速的找到对应的资源。

  在数据库中:查询一张表的时候有两种检索方式:
      弟一种:全表扫描
  第二种:根据索引检索(效率高)
           缩小了扫描的范围

  添加索引是给某一个字段添加索引。
  select ename,sal from emp where ename='SMITH';
  当ename字段上没有添加索引的时候,以上sql语句会进行全表扫描。
  当ename字段上添加索引的时候,以上sql语句会根据索引扫描。快速定位。

4.2 创建索引对象

   create index 索引名称 on 表名(字段名);
mysql> create index emp_sal_index on emp(sal);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

删除索引对象

   drop index 索引名称 on 表名;
mysql> drop index emp_sal_index on emp;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

4.3 什么时候给字段添加索引

   *数据量庞大。
   *该字段很少的DML操作。(字段进行修改操作,索引也需要维护)
   *该字段经常出现在where子句中。

4.4 注意:主键和具有unique约束的字段会自动添加索引。

   根据主键查询效率较高,尽量根据主键检索

4.5 添加索引前后的效率对比

  查看sql语句的执行计划:
  explain select ename,sal from emp where ename='SMITH';

  给薪资sal字段添加索引
    create index emp_sal_index on emp(sal);
  
  查看sql语句的执行计划:
  explain select ename,sal from emp where ename='SMITH';
mysql> explain select ename,sal from emp where ename='smith';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | emp   | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   14 |    10.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)

mysql> create index emp_sal_index on emp(sal);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>  explain select ename,sal from emp where sal='800';
+----+-------------+-------+------------+------+---------------+---------------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key           | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+-------+------------+------+---------------+---------------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | emp   | NULL       | ref  | emp_sal_index | emp_sal_index | 5       | const |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+------+---------------+---------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.01 sec)

4.6 索引底层采用的数据结构是:B+Tree

4.7 索引的实现原理:

  通过BTree缩小扫描范围。底层索引进行了排序,分区,索引会携带数据在表中的‘物理地址’
  最终通过索引检索到数据之后,获取到关联的物理地址,通过物理地址定位表中的数据,效率最高
  select ename from emp where ename='SMITH';
  通过索引转换为:
  select ename from emp where 物理地址=0x3;

4.8 索引分类

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

4.9 索引 模糊查询时失效

  select ename from emp where ename like '%A%';

5 视图(view)

5.1 视图:站在不同的角度去看同一张表的数据。

5.2 创建删除视图

 create view myview as select empno,ename from emp;
 drop view myview;

 注意:只有DQL语句才能以视图对象的方式创建出来。

5.3 对视图进行增删改查,会影响到原表数据。

  可以对视图进行CRUD操作。

5.4 面向视图操作:

 select * from myview;

 create table emp_bak as select * from emp;
 create view myview1 as select empno,ename,sal from emp_bak;
 
 update myview1 set ename='hehe',sal=1 where empno=7639;//通过视图修改原表数据
 delete from myview1 where empno=7369;//通过视图删除原表数据。

5.5 视图的作用

  视图可以隐藏表的实现细节,保密级别较高的系统,
  数据库只对外提供相关视图,程序员只对视图对象进行CRUD。

6.DBA命令

6.1 将数据库中的数据导出sql文件:

 导出整个库
 在windows的dos命令窗口执行:
   mysqldump company>D:\company.sql -uroot -1111
 导出指定表
   mysqldump company emp>D:\company.sql -uroot -1111

6.2 导入数据

create database company;
use company;
source D:\company.sql;
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值