2022年天津理工大学数据库实验四--查询优化参照 .pdf

上传人:C****o 文档编号:34255417 上传时间:2022-08-15 格式:PDF 页数:12 大小:927.81KB
返回 下载 相关 举报
2022年天津理工大学数据库实验四--查询优化参照 .pdf_第1页
第1页 / 共12页
2022年天津理工大学数据库实验四--查询优化参照 .pdf_第2页
第2页 / 共12页
点击查看更多>>
资源描述

《2022年天津理工大学数据库实验四--查询优化参照 .pdf》由会员分享,可在线阅读,更多相关《2022年天津理工大学数据库实验四--查询优化参照 .pdf(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、实验报告学院(系)名称:计算机与通信工程学院姓名Touchkiss 学号20125577 专业计算机科学与技术班级2 班实验项目查询优化课程名称数据库系统课程代码0668026实验时间2014/12/5 实验地点7#215 批改意见成绩教师签字:实验环境(软、硬件环境)一、 实验目的1.了解数据库查询优化方法和查询计划的概念2.学会分析查询的代价,并通过建立索引或者修改SQL语句来降低查询代价二、 实验内容与要求基于教材中的student 、course 、SC 表,通过存储过程(要求在报告中写出存储过程),插入多条元组( 1 万条以上),考虑不同的SQL操作,分析比较执行代价。1 单表查询(

2、1)直接查询:查询student表中年龄在20 岁以上的学生记录(2)建立索引后,再查询:查询student表中年龄在20 岁以上的学生记录(3)表中元组数量少,查询结果所占比例大:查询student表中年龄在20 岁以上的学生记录(4)表元组数量多,查询结果所占比例小:查询student表中年龄在20 岁以上的学生记录分析以上四种SQL查询的执行效率,并做总结:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 12 页 - - - - - - - - - 2.多表查询:基

3、于student、course 、 SC 表,按照以下要求,实现多表查询,并分析比较执行效率。 (自行设计查询语句,要求针对同一查询内容,使用以下四种方式)(1)多表连接查询(2)嵌套查询(3)建立索引( 4)使用游标分析比较:3综合练习(1) 对于 student表,不按照姓名创建索引,查询某个姓名,所需要的时间。(2) 对于 student表,按照姓名创建索引,查询某个姓名,所需要的时间。(3) 对于 student表,不按照系别创建索引,查询某个系所有学生,所需要的时间。(4) 对于 student表,按照系别创建各种索引,查询某个系所有学生,所需要的时间。(5) 查询 sc表所需时间。

4、(6) 将 student和 sc连接所需时间。(7) 将 student和 sc和 course连接所需时间。(8)查询选修了“数据库”学生的学号姓名,分别用嵌套和连接的方法,观察两种方法所用的时间。三、本次实验总结:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 12 页 - - - - - - - - - 基于教材中的student 、course 、SC 表,通过存储过程(要求在报告中写出存储过程),插入多条元组( 1 万条以上),考虑不同的SQL操作,分析比较执

5、行代价。存储过程:createprocedure insert_stu sexflag nvarchar = 男 , age int =0, dept char( 8)= MA , i int =1 aswhile i600000 beginset age = 20 if i% 4 = 0 set age =17 if i% 4 = 1 set age =18 if i% 4 = 2 set age = 19 if i% 2 = 0 set sexflag = 男elseset sexflag = 女set dept = MAif i% 3 = 0 set dept = CSif i% 3 =

6、 1 set dept = ISinsertintodbo. Student values ( i, Name +cast ( i as char ),sexflag, age , dept )set i=i+1 endcreateprocedure insert_cou i int= 1 aswhile i1000 begininsertinto dbo. Course ( Cno, Cname , Ccredit )values (i, Course + cast ( i aschar), i% 5)set i=i+1 end名师资料总结 - - -精品资料欢迎下载 - - - - - -

7、 - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 12 页 - - - - - - - - - USE lab GOSETANSI_NULLSONGOSETQUOTED_IDENTIFIER ONGOALTERprocedure dbo . insert_s_c i int=1, j int=1 aswhile i6000 beginset j=1 while j= 20 gosetstatisticsiooffsetstatisticstimeoff(2) 建立索引后,再查询:查询student表中年龄在20 岁以上的学生记录名师资料

8、总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 12 页 - - - - - - - - - (3) 表中元组数量少,查询结果所占比例大:查询student表中年龄在19 岁以下的学生记录STU 表中共 1000 条数据(4) 表元组数量多,查询结果所占比例小:查询student表中年龄在20 岁以上的学生记录Student 表中有 600000 条数据分析以上四种SQL查询的执行效率,并做总结:2.多表查询:基于student、course 、 SC 表,按照以下要求,实现多表

9、查询,并分析比较执行效率。(自行设计查询语句,要求针对同一查询内容,使用以下四种方式)(1)多表连接查询setstatisticsioonsetstatisticstimeongoselect Sname from dbo. Student , dbo. Course, dbo. SC where Student . Sno =SC . Sno and SC. Cno = Course . Cno and Cname =Course1and Grade 90 gosetstatisticsiooffsetstatisticstimeoff名师资料总结 - - -精品资料欢迎下载 - - - -

10、 - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 12 页 - - - - - - - - - (2)嵌套查询setstatisticsioonsetstatisticstimeongoselect Sname from Student where Sno in( select Sno from SC where Grade 90 and Cno =( select Cno from Course where Cname =Courserse1)gosetstatisticsiooffsetstatisticstimeoff (

11、3)建立索引setstatisticsioonsetstatisticstimeongocreateindex Studentgra1 on SC(Grade)select Sname from dbo. Student , dbo. Course, dbo. SC where Student . Sno =SC . Sno and SC. Cno = Course . Cno and Cname =Courserse1and Grade90 gosetstatisticsiooffsetstatisticstimeoff 名师资料总结 - - -精品资料欢迎下载 - - - - - - -

12、- - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 12 页 - - - - - - - - - (4)使用游标setstatisticsioonsetstatisticstimeongodeclare my_cursor cursorscrolldynamicforselect Sname from dbo. Student , dbo. Course, dbo. SC where Student . Sno =SC . Sno and SC. Cno = Course . Cno and Cname =Course1and Grade 90

13、 open my_cursor declare name sysnamefetchnextfrom my_cursor into name while (fetch_status =0)beginprint name fetchnextfrom my_cursor into name endclose my_cursor deallocate my_cursor gosetstatisticsiooffsetstatisticstimeoff名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - -

14、 第 7 页,共 12 页 - - - - - - - - - 分析比较:3综合练习(1) 对于 student表,不按照姓名创建索引,查询某个姓名,所需要的时间。setstatisticsioonsetstatisticstimeongoselect* from dbo . Student where Sname = Name1999gosetstatisticsiooffsetstatisticstimeoff (2) 对于 student表,按照姓名创建索引,查询某个姓名,所需要的时间。setstatisticsioonsetstatisticstimeongocreateindex S

15、name on Student ( Sname )select* from dbo . Student where Sname =Name1999gosetstatisticsiooffsetstatisticstimeoff名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 12 页 - - - - - - - - - (3) 对于 student表,不按照系别创建索引,查询某个系所有学生,所需要的时间。setstatisticsioonsetstatisticstimeo

16、ngoselect* from dbo . Student where Sdept =CSgosetstatisticsiooffsetstatisticstimeoff(4) 对于 student表,按照系别创建各种索引,查询某个系所有学生,所需要的时间。setstatisticsioonsetstatisticstimeongocreateindex Sdept on dbo. Student( Sdept)select* from dbo . Student where Sdept =CSgosetstatisticsiooffsetstatisticstimeoff名师资料总结 - -

17、 -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 12 页 - - - - - - - - - (5) 查询 sc表所需时间。setstatisticsioonsetstatisticstimeongoselect* from dbo . SC gosetstatisticsiooffsetstatisticstimeoff(6) 将 student和 sc连接所需时间。setstatisticsioonsetstatisticstimeongoselect* from dbo . Stude

18、nt , dbo. SC where Student . Sno =SC . Sno gosetstatisticsiooffsetstatisticstimeoff(7) 将 student和 sc和 course连接所需时间。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 12 页 - - - - - - - - - setstatisticsioonsetstatisticstimeongoselect* from dbo . Student , dbo. SC

19、, dbo. Course where Student . Sno=SC . Sno and SC. Cno =Course. Cno gosetstatisticsiooffsetstatisticstimeoff(8)查询选修了“数据库”学生的学号姓名,分别用嵌套和连接的方法,观察两种方法所用的时间。setstatisticsioonsetstatisticstimeongoselect Sno, Sname from dbo . Student where Sno in( select sno from dbo. SC where Cno in( select cno from dbo.

20、 Course where Cname = Course999 )gosetstatisticsiooffsetstatisticstimeoff名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 12 页 - - - - - - - - - 本次实验总结:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 12 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁