电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

北邮大三数据库实验六数据查询分析实验VIP免费

北邮大三数据库实验六数据查询分析实验_第1页
1/10
北邮大三数据库实验六数据查询分析实验_第2页
2/10
北邮大三数据库实验六数据查询分析实验_第3页
3/10
实验六数据查询分析实验实验目的通过对不同情况下查询语句的执行分析,巩固和加深对查询和查询优化相关理论知识的理解,提高优化数据库系统的实践能力,熟悉了解Sybase中查询分析器的使用,并进一步提高编写复杂查询的SQL程序的能力。实验内容1.索引对查询的影响(1)对结果集只有一个元组的查询分三种情况进行执行(必如查询一个具体学生的信息):不建立索引,(学号上)建立非聚集索引,(学号上)建立聚集索引。建立聚集索引:createclusteredindexstudentonstudent(student_id)go建立非聚集索引:createnonclusteredindexstudent_indexonstudent(student_id)go用查询分析器的执行步骤和结果对执行进行分析比较。select*fromstudentwherestudent_id='30201'不建立索引建立聚集索引建立非聚集索引(2)对结果集中有多个元组的查询(例如查看某门成绩的成绩表)分类似(1)的三种情况进行执行比较。select*fromstudentwherestudent_id>'30401'不建立索引:建立聚集索引:建立非聚集索引:(3)对查询条件为一个连续的范围的查询(例如查看学号在某个范围内的学生的选课情况)分类似(1)的三种情况进行执行比较,注意系统处理的选择。select*fromstudentwherestudent_idbetween'31201'and'31415'不建立索引:建立聚集索引:建立非聚集索引:(4)索引代价。在有索引和无索引的情况下插入数据(例如在选课情况表SC上插入数据),比较插入的执行效率。insertintostudentvalues('31710','张攀','男','1993-1-100:00:00','计算机','3146')deletefromstudentwherestudent_id='31710'无索引:建立聚集索引:建立非聚集索引:2、对相同查询功能不同查询语句的执行比较分析(1)groupbyselectavg(score)fromscgroupbycourse_idhavingcourse_id='C01'selectavg(score)fromscwherecourse_id='C01'比较其查询效率可知,没有groupby的查询时间比较短,查询效率较高(2)selectstudent_id,student_name,birthdayfromstudents1wherebirthday=(selectmax(birthday)fromstudents2wheres1.department=s2.department)另一个:selectdepartment,max(birthday)asmaxAgeintotmpfromstudentgroupbydepartment;selectstudent_id,student_name,birthdayfromstudent,tmpwherestudent.birthday=tmp.maxAgeandtmp.department=student.departmentdroptabletmp查询结果来看,重写的执行时间要快一些,但相差不多,如果数据库比较大的话,执行效果也许更明显(3)对下面两个查询进行比较selectstudent_name,birthdayfromstudentwheredepartment!='电信'andbirthday>all(selectbirthdayfromstudentwheredepartment='电信')另:selectstudent_name,birthdayfromstudentwheredepartment!='电信'andbirthday>(selectmax(birthday)fromstudentwheredepartment='电信')3、查询优化除了建立适当索引,对SQL语句重写外,还有其他手段来进行查询调优,例如调整缓冲区大小,事先建立视图等。设计实现下列查询,使之运行效率最高。写出你的查询形式,以及调优过程;并说明最优情况下的运行时间。(1)查找选修了每一门课的学生。方法一:withstudent1(num,account)as(selectdistinctstudent_id,count(course_id)fromscgroupbystudent_id)selectstudent_namefromstudent1,studentwherestudent1.account>=5andstudent1.num=student.student_id方法二:selectstudent_namefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromscwheresc.student_id=student.student_idandcourse.course_id=sc.course_id))尝试了两种方法,相比较而言,第二种方法的执行速度更快。(2)查找至少选修了课程数据库原理和操作系统的学生的学号。方法一:(selectstudent_idfromstudentviewwherecourse_name='操作系统')intersect(selectstudent_idfromstudentviewwherecourse_name='数据库原理')方法二:(selectstudent_idfromsc,coursewherecourse.course_name='操作系统'andsc.course_id=course.course_id)intersect(selectstudent_idfromsc,coursewherecourse.course_name='数据库原理'andsc.course_id=course.course_id)相比较而言不使用视图的方法查询速度较快实验总结:1、本次实验遇到的第一个问题就是如何建立索引的问题,在之前实验的基础上开始做,发现设置了主键之后,数据库会默认以主键为搜索码建立聚集索引,而且无法删除索引也不好更改,所以只好重新建表格进行实验,自己来设置聚集索引或非聚集索引2、实验遇到的第二个问题是在进行第二步实验的时候,原表格中没有age这一属性,只有birthday这一属性,所以把age全部改为birthday进行实验。3、实验的第三部分有点难度,想到了一种方法之后就很难再用另一种方法把它表示出来,参考了网上的一些资料,集思广益完成了查询优化的设计

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

北邮大三数据库实验六数据查询分析实验

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部