`
小嘴冰凉
  • 浏览: 448330 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

嵌套SQL的查询速度比较分析

阅读更多
嵌套SQL的查询速度比较分析

文章中使用Oracle自带的HR数据库,故代码可以直接进行测试。
代码一:
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'A%'
or t.first_name like 'B%'
or  t.first_name like 'H%'
or  t.first_name like 'K%'
or  t.first_name like 'M%'
or  t.first_name like 'J%'
or  t.first_name like 'N%'
执行计划:


代码二:
select *
from (
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'A%'
or t.first_name like 'B%'
or  t.first_name like 'H%'
or  t.first_name like 'K%'
or  t.first_name like 'M%'
or  t.first_name like 'J%'
or  t.first_name like 'N%'
)
执行计划:


对比:代码1与代码2的执行计划相同

代码三:
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'A%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'B%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'H%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'K%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'M%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'J%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'N%'

执行计划:


代码四:
select * from
(
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'A%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'B%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'H%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'K%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'M%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'J%'
union
select t.employee_id, t.first_name, t.phone_number  from HR.Employees t where t.first_name like 'N%'
)
执行计划:

对比:代码4中Sort Unique与代码3的执行计划相同。但Oracle在处理代码4的查询语句时,构建了一个内部视图来整理查询结果,其中需要21次IO操作,故需要更长的时间。

其他:在一个SQL中,使用“OR”语句比使用多个Union会花费更短的时间。

代码五:
代码5-1:
select *
from
(select *  from HR.Employees tx where tx.department_id = 50) T1,
(select * from HR.Departments ty where ty.department_id < 150) T2
where t1.department_id = t2.department_id
代码5-2:
select *
from
HR.Employees t1,
HR.Departments T2
where t1.department_id = t2.department_id and t1.department_id = 50 and t2.department_id < 150
代码5-3:
select *
from
HR.Employees T1,
HR.Departments T2
where t1.department_id = t2.department_id(+) and t1.department_id = 50 and t2.department_id <150
代码5-4:
select *
from
HR.Employees T1,
HR.Departments T2
where t1.department_id(+) = t2.department_id and t1.department_id = 50 and t2.department_id <150
代码5-1到代码5-4的Oracle执行计划分析结果相同:

对比:代码5-1到代码5-4的执行计划相同。Oracle是先对T1和T2中数据进行过滤后,再对结果集进行关联查询。且Oracle对表过滤内容进行了优化,对表Departments的查询优化为 TY.Department_ID=50 而不是 TY.Department_ID<150


http://www.cnblogs.com/joyyuan97/archive/2008/09/13/516333.html
分享到:
评论

相关推荐

    SQL查询安全性及性能优化

    执行计划比较复杂的SQL语句质量就不是很高 我们还可以结合时间统计【set statistics TIME ON..】一起使用,通过和时间统计结合使用可以更好地发挥执行计划的作用 有了执行计划和执行时间我们就很容易判断一条...

    LECCO SQL Expert (智能自动SQL优化)

    运行速度约有22.75倍的提升(源SQL语句运行时间为2.73秒,SQL124运行时间为0.12秒,如图5)。图5 测试结果 我们把SQL124放入源代码中,结束一条SQL语句的优化工作。从上例可以看到,LECCO SQL Expert的自动重写技术使...

    SQL语法大全

    SQL语法大全 SQL语法大全 1. ASP与Access数据库连接: dim conn,mdbfile mdbfile=server.mappath("数据库名称.mdb") set conn=server.createobject("adodb.connection") conn.open "driver={microsoft access ...

    C#开发经验技巧宝典

    0865 提高SQL性能加快执行速度 513 0866 控制批处理内语句的执行 513 0867 执行查询但是显示列信息 514 0868 获取连接或试图连接的次数 514 0869 获取当前数据库的语言名 514 19.5 时间与谓词 514 0870...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    SQL(Structured Query Language)结构化查询语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。同时也是数据库脚本文件的扩展名。  SQL语言主要包含5个部分  数据定义...

    收获不知Oracle

    2.2.2.1 从一普通查询SQL说起20 2.2.2.2 老余故事终现用心良苦23 2.2.2.3 一起体会Oracle代价 27 2.2.3 体系结构原理再探 30 2.2.3.1 从一普通更新语句说起30 2.2.3.2 体系结构中提交的探讨34 2.2.3.3 劳模的评选 38...

    Access+2000中文版高级编程

    10.2.4 在子窗体中使用SQL的UNION语句查询所有记录 266 10.2.5 在组合框控件外显示组合框的列 268 10.2.6 根据用户的输入添加新的组合框选项 271 10.3 使用本机的Access选项卡控件 273 10.3.1 本机的选项卡...

    Access 2000中文版高级编程(part1)

    10.2.4 在子窗体中使用SQL的UNION语句查询所有记录 266 10.2.5 在组合框控件外显示组合框的列 268 10.2.6 根据用户的输入添加新的组合框选项 271 10.3 使用本机的Access选项卡控件 273 10.3.1 本机的选项卡控件...

    ORACLE9i_优化设计与系统调整

    §14.8.1 嵌套连接- 181 §14.8.2 合并连接- 183 第15章 使用优化器提示 183 §15.1 提示(Hint)概念 184 §15.1.1 提示的指定 184 §15.2 使用提示 185 §15.2.1 提示的指定 185 §15.2.1.1 ALL_ROWS 186 §15.2....

    C#编程经验技巧宝典

    85 &lt;br&gt;0130 复制字符串中指定的字符 85 &lt;br&gt;0131 巧截字符串的数字 86 &lt;br&gt;0132 如何存储变长字符串 86 &lt;br&gt;0133 在进行字符串比较时忽略大小写 87 &lt;br&gt;0134 如何去除字符串尾空格 87 ...

    优秀代码编辑器工具 PhpDesigner 8.1.2 Portable 绿色中文免费版.zip

    phpDesigner 设计最主要的目的就是加快你的编程速度,让你更轻松。然而,也正是这个原因,它更倾向于初学者。 优秀代码编辑器工具 PhpDesigner 中文版优秀代码编辑器工具 PhpDesigner 中文版 phpDesigner 最主要的...

    网络数据库课件ppt(web数据库ppt)

    重点掌握查询语句的使用,包括连接查询和嵌套查询。 (3)了解嵌入式SQL和动态SQL技术。 2.重点、难点 重点:掌握SQL语言的各种用法 (四)关系数据理论 ( 2学时) 1 问题的提出 2 规范化(1~4NF) 3 数据依赖的公理...

    s15_aragon_data_lecture_notes:CU Boulder 2015 年Spring数据工程研讨会的讲义

    数据工程课堂笔记 第1讲 我们创建了我们的个人笔记存储库并更新了这个自述文件。 大数据有哪些组成部分? 社交网络 数据分析 贮存 无SQL Infoviz - 您如何向用户显示大数据集?...API 版本控制 资源嵌套 Ruby 客户端

    mysql官方中文参考手册

    7.2.3. SELECT查询的速度 7.2.4. MySQL怎样优化WHERE子句 7.2.5. 范围优化 7.2.6. 索引合并优化 7.2.7. MySQL如何优化IS NULL 7.2.8. MySQL如何优化DISTINCT 7.2.9. MySQL如何优化LEFT JOIN和RIGHT JOIN 7.2.10. ...

Global site tag (gtag.js) - Google Analytics