声明:所有SQL语句均在实验平台验证通过,实验细节可能随时间推移老师会进行修改。在此仅提供解答思路,毕竟我的方法肯定不是最优,而且实验平台有查重功能,不要一昧的复制哦!
题目1
1.将pub用户下表student的3个列sid,name,birthday复制到表test10_01中。
2.执行如下查询,观察运行速度(5秒以上)。
查询Samefirstname相同姓氏的人数。
select * from
(select sid,name,birthday,
(select count(*) from test10_01 where substr(name,1,1)=substr(t1.name,1,1)) samefirstname
from pub.student_testindex t1)
where samefirstname=7
3.为test10_01创建一个仅仅一个索引,保证上面SQL耗时在1秒内。
create table test10_01 as select sid,name,birthday from pub.student;
create index idx_ on test10_01(substr(name,1,1));
题目2
1.将pub用户下表student的3个列sid,name,birthday复制到表test10_02中。
2.将出生日期全部修改成一天:
Update test10_02 set birthday=to_date('19881018','yyyymmdd');
3.为test10_02创建一个仅仅一个索引,保证下面SQL耗时在1秒内。
Samenamebirthday同名同生日的人数,Samebirthday相同出生日期的人数
select * from
(select sid,name,birthday,
(select count(*) from test10_02 where name=t1.name and birthday=t1.birthday) samenamebirthday,
(select count(*) from test10_02 where birthday=t1.birthday) samebirthday
from pub.student_testindex t1)
where samebirthday=3990
create table test10_02 as select sid,name,birthday from pub.student;
update test10_02 set birthday=to_date('19881018','yyyymmdd');
create index idx_2 on test10_02(birthday,name);
题目3
1.pub用户下表student已经用下面两句SQL创建了两索引。
Create index student_birthday on student(birthday);
Create index student_name on student(name);
2.下面SQL不能用索引耗时超过1秒,在逻辑不变下修改SQL条件,使其能使用索引。
查询samefirstname同姓氏的人数、samename同姓名的人数。
select * from
(select sid,name,birthday,
(select count(*) from pub.student where substr(name,1,1)=substr(t1.name,1,1)) samefirstname
from pub.student_testindex t1)
where samefirstname=7
3.修改以后验证耗时在2秒之内,将修改以后语句创建成视图create view test10_3 as select ……。
create or replace view test10_03 as
select a.sid,a.name,a.birthday,b.samefirstname from
(
pub.student_testindex a
join
(select * from
(select first_name,count(*) samefirstname from
(select sid,substr(name,1,1) first_name from pub.student) group by first_name)
where samefirstname=7) b
on substr(a.name,1,1)=b.first_name
);
题目4
1.pub用户下表student已经用下面两句SQL创建了两索引。
Create index student_birthday on student(birthday);
Create index student_name on student(name);
2.下面SQL不能用索引耗时超过1秒,在逻辑不变下修改SQL条件,使其能使用索引。
查询sameyearmonth相同年月的人数、查询sameyear相同年的人数。
select * from
(select sid,name,birthday,
(select count(*) from pub.student where to_char(birthday,'yyyymm')=to_char(t1.birthday,'yyyymm')) sameyearmonth,
(select count(*) from pub.student where extract (year from birthday) =extract (year from t1.birthday)) sameyear
from pub.student_testindex t1)
where sameyearmonth=35
3.修改以后验证耗时在1秒之内,将修改以后语句创建成视图create view test10_4 as select ……。
create view test10_04 as
select a.sid,a.name,a.birthday,b.sameyearmonth,b.sameyear from
(pub.student_testindex a
join
(select * from
(
(select y,count(y) sameyear from
(select extract(year from birthday) y from pub.student) group by y) year
join
(select ym,count(ym) sameyearmonth from
(select to_char(birthday,'yyyymm') ym from pub.student) group by ym) yearm
on year.y=substr(yearm.ym,1,4)
)where sameyearmonth=35) b
on to_char(a.birthday,'yyyymm')=b.ym);
题目5
1.pub用户下表student已经用下面两句SQL创建了两索引。
Create index student_birthday on student(birthday);
Create index student_name on student(name);
2.下面SQL不能用索引耗时超过1秒,在逻辑不变下修改SQL条件,使其能使用索引。
查询nextbirthday晚一天出生的人数
select * from
(select sid,name,birthday,
(select count(*) from pub.student where birthday-1=t1.birthday) nextbirthday
from pub.student_testindex t1)
where nextbirthday=7
3.修改以后验证耗时在1秒之内,将修改以后语句创建成视图create view test10_5 as select ……。
create or replace view test10_05 as
select * from
(select sid,name,birthday,
(select count(*) from pub.student where birthday=t1.birthday+1) nextbirthday
from pub.student_testindex t1)
where nextbirthday=7;
1.题目一求相同姓氏的人数,所以应该在姓上建立索引,通过substr(name,1,1)获得姓。
2.题目二求同名且同生日,所以应该创建复合索引(birthday,name)。
3.题目三,四,五根据SQL查询逻辑,改写SQL语句,优化查询时间。体会到索引对于提高查找效率的重要作用,以及如何合理的描述查找条件以便能够使用索引。对比前后两次的查询时间真的差距很大。
4.3和4我根据查找逻辑重新写了SQL,5只需把-1=改成=+1即可在查询时使用索引。
ps:本人开通了个人的微信公众号,希望大家能关注一下,
我会将资源、文章优先推送到公众号上。
推送自己的一些学习笔记,实验源代码等,
欢迎大家互相交流学习。