版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验六 数据查询分析实验实验目的通过对不同情况下查询语句的执行分析,巩固和加深对查询和查询优化相关理论知识的理解,提高优化数据库系统的实践能力,熟悉了解Sybase中查询分析器的使用,并进一步提高编写复杂查询的SQL 程序的能力。实验内容1.索引对查询的影响(1)对结果集只有一个元组的查询分三种情况进行执行(必如查询一个具体学生的信息):不建立索引,(学号上)建立非聚集索引,(学号上)建立聚集索引。建立聚集索引:create clustered index studenton student(student_id)go建立非聚集索引:create nonclustered index stud
2、ent_indexon student(student_id)go用查询分析器的执行步骤和结果对执行进行分析比较。select * from student where student_id='30201'不建立索引建立聚集索引建立非聚集索引(2)对结果集中有多个元组的查询(例如查看某门成绩的成绩表)分类似(1)的三种情况进行执行比较。select * from student where student_id>'30401'不建立索引:建立聚集索引:建立非聚集索引:(3)对查询条件为一个连续的范围的查询(例如查看学号在某个范围内的学生的选课情况)分类似(
3、1)的三种情况进行执行比较,注意系统处理的选择。select * from student where student_id between '31201' and '31415'不建立索引:建立聚集索引:建立非聚集索引:(4)索引代价。在有索引和无索引的情况下插入数据(例如在选课情况表SC 上插入数据),比较插入的执行效率。insert into student values('31710','张攀','男','1993-1-1 00:00:00','计算机','3146&
4、#39;)delete from student where student_id ='31710'无索引:建立聚集索引:建立非聚集索引:2、对相同查询功能不同查询语句的执行比较分析(1) group byselect avg(score)from scgroup by course_idhaving course_id='C01'select avg(score)from scwhere course_id='C01'比较其查询效率可知,没有group by的查询时间比较短,查询效率较高(2)select student_id,student_
5、name,birthdayfrom student s1where birthday=(select max(birthday)from student s2where s1.department = s2.department)另一个:select department,max(birthday) as maxAge into tmpfrom studentgroup by department;select student_id,student_name,birthdayfrom student,tmpwhere student.birthday = tmp.maxAge and tmp.
6、department=student.departmentdrop table tmp查询结果来看,重写的执行时间要快一些,但相差不多,如果数据库比较大的话,执行效果也许更明显(3)对下面两个查询进行比较select student_name,birthdayfrom studentwhere department!='电信' and birthday> all(select birthdayfrom studentwhere department ='电信')另:select student_name,birthdayfrom studentwhere
7、department!='电信' and birthday> (select max(birthday)from studentwhere department ='电信')3、查询优化除了建立适当索引,对SQL 语句重写外,还有其他手段来进行查询调优,例如调整缓冲区大小,事先建立视图等。设计实现下列查询,使之运行效率最高。写出你的查询形式,以及调优过程;并说明最优情况下的运行时间。(1) 查找选修了每一门课的学生。方法一:with student1(num,account) as(select distinct student_id, count(cou
8、rse_id)from scgroup by student_id)select student_namefrom student1,studentwhere student1.account>=5 and student1.num=student.student_id方法二:select student_namefrom studentwhere not exists(select * from coursewhere not exists(select *from scwhere sc.student_id=student.student_id and course.course_i
9、d=sc.course_id)尝试了两种方法,相比较而言,第二种方法的执行速度更快。(2) 查找至少选修了课程数据库原理和操作系统的学生的学号。方法一:(select student_idfrom studentviewwhere course_name='操作系统')intersect(select student_idfrom studentview where course_name='数据库原理')方法二:(select student_idfrom sc,coursewhere course.course_name='操作系统' and sc.course_id= course.course_id)intersect(select student_idfrom sc, coursewhere course.course_name='数据库原理' and sc. course_id = course. course_id)相比较而言不使用视图的方法查询速度较快实验总结:1、 本次实验遇到的第一个问题就是如何建立索引的问题,在之前实验的基础上开始做,发现设置了主键之后,数据库会默认以主键为搜索码建立聚集索引,而且无法删除索引也不好更改,所以只好重新建表格进行实验,自己来
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 六年级健康教育教学设计
- 2025年衢州市公安局柯城分局公开招聘辅警8人备考题库及答案详解一套
- 2025年兴业银行武汉分行社会招聘备考题库及参考答案详解一套
- 2025年南平市公安局建阳分局公开招聘警务辅助人员备考题库及答案详解一套
- 2025年资阳市公安局公开招聘警务辅助人员的备考题库及完整答案详解一套
- 2025年民生银行兰州分行社会招聘备考题库及参考答案详解一套
- 2025年成都银行招聘总行专职信用审批人等岗位7人备考题库及完整答案详解1套
- 银行业专题研究:2026年信贷十大展望
- 2025年沧州职业技术学院招聘真题(行政管理岗)
- 2026年及未来5年市场数据中国道路塑钢护栏市场深度分析及投资战略咨询报告
- 2026包钢(集团)公司新员工招聘(322人)笔试考试参考试题及答案解析
- 浙江大学《普通化学》(第6版)笔记和课后习题(含考研真题)详解
- 2026年消防设施操作员之消防设备基础知识考试题库500道附答案【轻巧夺冠】
- 河南省南阳市2025-2026学年高二上学期期中语文试题(含答案)(解析版)
- T-CRCRA 010-2023 非物质文化遗产传承与保护规范
- 2025广西公需科目培训考试答案(90分)一区两地一园一通道建设人工智能时代的机遇与挑战
- GA/T 1132-2014车辆出入口电动栏杆机技术要求
- 三角函数的叠加之辅助角公式【公开课教学课件】
- 2023年北京市朝阳区城管协管员招聘笔试模拟试题及答案解析
- 【公开课】高三生物一轮复习课件:生态系统的能量流动
- 学校后勤管理工作课件
评论
0/150
提交评论