create view student_2001(sno,sname,sgender,grade)
as
select s.sno,sname,sgender,grade from s,sc,c
where s.sno = sc.sno and sc.cno = c.cno and cname = '数据库'
and birth like '2001%';
create view V_SPJ(sno,pno,qty)
as
select sno,pno,qty from spj
where jno in
(
select jno from j
where jname = '三建'
);
create view V_SPJ(sno,pno,qty)
as
select sno,pno,qty from spj,s
where spj.jno = j.jno and jname = '三建';
select pno,sum(qty) from V_SPJ
group by pno;
select * from V_SPJ
where sno = 's1';
提示:
update V_SPJ set qty = 1000
where sno = 's1' and pno = 'p1';
delete from V_SPJ where sno = 's1' and pno = 'p1';
略
create index IX_CName on c(cname);
create index IX_ngd on s(sname asc, sgender asc, sdept asc);
提示:可以使用show index from tablename
查看该表上的索引信息。
drop index IX_CNo on c;
未使用索引
使用主键索引
未使用索引
未使用索引
使用复合索引
使用复合索引
使用复合索引
未使用索引
未使用索引
具体情况之后补充
create table userinfo
(
user_id int primary key, //用户ID
username varchar(10), //用户名
gender char(1), //性别
age int, //年龄
c_id int //学院编号
);
生成一万条测试数据插入表中。
explain SELECT * FROM userinfo WHERE age = 30;
explain SELECT * FROM userinfo WHERE age = 30;
对有索引和无索引的查询效率对比可以得到,首先看type列,无索引需要扫描全表,而有索引是ref类型,效率提高;再看rows列,有索引的预估扫描行数约为无索引的1%,可以看出效率大大提高。
explain SELECT username,age,gender,c_id FROM userinfo WHERE gender = 'w' and age = 30 and c_id = 2;
explain SELECT username,age,gender,c_id FROM userinfo WHERE gender = 'w' and age = 30 and c_id = 2;
对比可得宽索引效率要高于窄索引,它的预期查询行数为窄索引的11/196。
宽索引中的最左匹配原则是指,在宽索引的查询中,查询条件最左优先,在检索数据时从联合索引的最左边开始匹配,若不符合顺序,则无法使用所有的宽索引。