mysql视图,索引

博客 动态
0 187
羽尘
羽尘 2022-02-22 20:55:39
悬赏:0 积分 收藏

mysql视图,索引

一、视图 View

视图是一个虚拟表,是sql语句的查询结果,其内容由查询定义。同真实的表一样,视图包含一系列带有名称的列和行数据,在使用视图时动态生成。视图的数据变化会影响到基表,基表的数据变化也会影响到视图[insert update delete ] ; 创建视图需要create view 权限,并且对于查询涉及的列有select权限;使用create or replace 或者 alter修改视图,那么还需要改视图的drop权限。

视图的核心功能是用来简化查询,查询可以当作一个表来查询。

select * from stu;-- 建立视图create view v_li as    select id,name,score from stu where name like '李%'--show tables;select * from v_li limit 5;-- 建立账号,并授权create user vu identified by 'vu';grant select(id,name) on wxdb.v_li to vu;grant select on wxdb.v_li to vu;create view v_member as    select m.nickname,m.sex,m.mobile  from hbcf.cf_member m;grant select on v_member to vu;-- 删除视图drop view v_member;-- 查看系统所有账号信息select user,host,u.authentication_string,u.plugin from mysql.user u;
-- 查询视图select * from stu01;-- create view vstu as select level 等级,count(*) 人数 from (select id,name,score,casewhen score>=90  then '优秀' when score>=80  then '良好' when score>=60  then '及格' else '补考' end levelfrom stu) as t group by t.level;select * from stu;select * from vstu;-- 查看数据库 d3 所有 view 视图select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA` = 'd3' and `TABLE_TYPE` = 'VIEW'; -- 查看数据库  d3的所有 base table   表select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA` = 'd3' and `TABLE_TYPE` = 'BASE TABLE'; -- 删除视图drop view if exists stu01,vstu;

二、索引

MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度。

打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是一个人力三轮车。

拿汉语字典的目录页(索引)打比方,我们可以按拼音、笔画、偏旁部首等排序的目录(索引)快速查找到需要的字。

索引分单列索引和组合索引。单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引。组合索引,即一个索引包含多个列。

创建索引时,你需要确保该索引是应用在 SQL 查询语句的条件(一般作为 WHERE 子句的条件)。

实际上,索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。

上面都在说使用索引的好处,但过多的使用索引将会造成滥用。因此索引也会有它的缺点:虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件。

建立索引会占用磁盘空间的索引文件。

-- 建立表时指定索引,create table t(t varchar(30),key(t desc))show index from t;-- 删除索引drop index t on t;alter table t add index index1 (t desc);alter table t drop index index1;-- 使用索引select * from stu where name = '李丽';-- 建立唯一索引create unique index  name_index on stu(name asc);create index namegi on stu(name asc,gender desc);-- 删除索引drop index name_index on stu;drop index namegi on stu;-- 查看表中所有索引show index from stu;-- 索引缺点  1 50mb 100mb 2 降低数据的插入,修改速度-- index 索引-- 建立索引create table t1(    id int unsigned auto_increment,    name varchar(30),    score tinyint unsigned,    primary key(id),      key(name desc));create table t2(    id int unsigned,    name varchar(30),    score tinyint unsigned,    index iname (name(30)));create table t3(    id int unsigned,    name varchar(30),    score tinyint unsigned);-- 建立索引create index inn on t3(name)-- 建立唯一索引create unique index inn2 on t3(name);alter table t3 add index inn4(name asc,score desc);-- 使用索引 索引字段是不是出现在where条件中,如果在索引就起作用select * from stu where name = '张三';select * from t3;insert into t3 value(1,'李四1',80);insert into t3 value(2,'李四2',10);insert into t3 value(3,'李四3',50);insert into t3 value(4,'李四4',40);insert into t3 value(5,'李四5',30);insert into t3 value(6,'李四6',20);explain select * from t3 where id = 6;explain select * from t3 where name = '李四3';-- 删除索引drop index inn2 on t3;alter table t3 drop index inn4;-- 查看表中所有索引show index from t3;-- 索引缺点  1 50mb 100mb 2 降低数据的插入,修改速度

desc student查看表结构

show index from student;查看索引

tree

b+tree

 

posted @ 2022-02-22 20:39 一份人间烟火 阅读(5) 评论(0) 编辑 收藏 举报
回帖
    羽尘

    羽尘 (王者 段位)

    2335 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员