oracle中sql语句的优化(转帖)

一、执行顺序及优化细则

1.表名顺序优化
(1) 基础表放下面,当两表进行关联时数据量少的表的表名放右边
表或视图:
Student_info   (30000条数据)
Description_info (30条数据) 
select *
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'     
与   
select *
  from student_info     si--学生信息表
      ,description_info di
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'  
以student_info作为基础表,你会发现运行的速度会有很大的差距。
  
  
(2) 当出现多个表时,关联表被称之为交叉表,交叉表作为基础表
select *
  from description_info di
    ,description_info di2
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and si.school_id = di.lookup_code(+)
   and di.lookup_type(+) = 'SCHOOL_ID'

select *
  from student_info     si--学生信息表
      ,description_info di
      ,description_info di2
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and si.school_id = di.lookup_code(+)
   and di.lookup_type(+) = 'SCHOOL_ID'
以student_info作为基础表,你会发现运行的速度会有很大的差距,
当基础表放在后面,这样的执行速度会明显快很多。

2.where执行顺序
where执行会从至下往上执行
select *
from student_info si --学生信息表
where si.school_id=10 --学院ID
and  si.system_id=100--系ID
摆放where子句时,把能过滤大量数据的条件放在最下边

3. is null 和is not null
当要过滤列为空数据或不为空的数据时使用
select *
from student_info si --学生信息表
where si.school_id is null(当前列中的null为少数时用is not null,否则is null)

4.使用表别名
当查询时出现多个表时,查询时加上别名,
避免出现减少解析的时间字段歧义引起的语法错误。

5. where执行速度比having快
尽可能的使用where代替having
select  from student_info si
group by si.student_id
having si.system_id!=100
  and si.school_id!=10
(select  from student_info si
wehre si.system_id!=100
and si.school_id!=10
group by si.student_id) 
 
6.  * 号引起的执行效率
尽量减少使用select * 来进行查询,当你查询使用*,
数据库会进行解析并将*转换为全部列。


二、替代优化
1、用>=替代>
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id>=10
  与
  select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id>9
  执行时>=会比>执行得要快
 
2、用UNION替换OR (适用于索引列)
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id=10
  union
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id=2 
   上面语句可有效避免全表查询
   select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id=10
  or ui.student_id=2
  如果坚持要用OR, 可以把返回记录最少的索引列写在最前面
   
3、用in 代替or
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id=10
  or ui.student_id=20
  or ui.student_id=30
  改成
  select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id in (10,20,30)
  执行会更有效率
   
4、 Union All 与Union
Union All重复输出两个结果集合中相同记录
如果两个并集中数据都不一样.那么使用Union All 与Union是没有区别的,
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id=10
  union All
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id=2 
  与
  select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id=10
  union
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id=2 
但Union All会比Union要执行得快

5、分离表和索引
总是将你的表和索引建立在另外的表空间内
决不要将这些对象存放到SYSTEM表空间里

三、一些优化技巧


1、计算表的记录数时

select count(si.student_id)
from Student_info si(student_id为索引)

select count(*) from Student_info si
执行时.上面的语句明显会比下面没有用索引统计的语句要快

2.使用函数提高SQL执行速度

当出现复杂的查询sql语名,可以考虑使用函数来提高速度
查询学生信息并查询学生(李明)个人信息与的数学成绩排名

select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and di.description = '李明'
  
而且我们将上面order_num排名写成一个fuction时
create or replace package body order_num_pkg is
function order_num(p_student_id number) return_number is
  v_return_number number;
begin
  select res.order_num --排名
    into v_return_number
    from result res
   where res.student_id = di.student_id
   order by result_math;
  return v_return_number;
exception
  when others then
    null;
    return null;
end;
end order_num_pkg;
执行
select di.description student_name
      ,order_num_pkg.order_num(di.student_id) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and di.description = '李明'
执行查询时的速度也会有所提高    
 
3.减少访问数据库的次数

执行次数的减少(当要查询出student_id=100的学生和student_id=20的学生信息时)
select address_id
from student_info si --学生信息表
where si.student_id=100

select address_id
from student_info si --学生信息表
where si.student_id=20
都进行查询.这样的效率是很低的
而进行
(
select si.address_id,si2.address_id
from student_info si --学生信息表
,student_info si2
where si.student_id=100
and si2.student_id=20

select decode(si.student_id,100,address_id)
   ,decode(si.student_id,20,address_id)
from student_info si
)
执行速度是提高了,但可读性反而差了..
所以这种写法个人并不太推荐

4、用Exists(Not Exists)代替In(Not In)

   在执行当中使用Exists或者Not Exists可以高效的进行查询

5、Exists取代Distinct取唯一值的

   取出关联表部门对员工时,这时取出员工部门时,出现多条..
select distinct di.dept_name
  from departments_info di --部门表
      ,user_info        ui --员工信息表
where ui.dept_no = di.dept_no
   可以修改成
  select di.dept_name
    from departments_info di --部门表
   where  exists (select 'X'
            from user_info ui --员工信息表
           where di.dept_no = ui.dept_no)
6、用表连接代替Exists
   通过表的关联来代替exists会使执行更有效率
select ui.user_name
  from user_info ui--员工信息表
where exists (select 'x '
          from departments_info di--部门表
         where di.dept_no = ui.dept_no
           and ui.dept_cat = 'IT');
执行是比较快,但还可以使用表的连接取得更快的查询效率
   select ui.user_name
    from departments_info di
        ,user_info        ui --员工信息表
   where ui.dept_no = di.dept_no
     and ui.department_type_code = 'IT'

代码是经测试并进行优化所写,
以上只例子,具体使用还是要针对各个不同的具体的业务使用用Exists(Not Exists)代替In(Not In)

四、索引篇

1、运算导致的索引失效

select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and si.student_id+0=100
  
2、类型转换导致的索引失效

select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and di.student_id='100'
 
student_id为number类型的索引,当执行下列语句,
oracle会自动转换成
select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and di.student_id=to_number('100')
所幸,只是解析并转换类型,并没有导到失效,
但要是写成下面,将会使用其失效
select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and to_char(di.student_id)='100'
  
3、在索引列上进行计算引起的问题

select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and di.student_id-2=10
在索引列中进行运算,将会不使用索引而使用全表扫描
而将
select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and di.student_id=10+2
将会得到高效的运行速度

4、 Is not null引起的问题(student_id为索引)

不要把存在空值的列做为索引,否则无法使用索引
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id is not null--索引失效
 
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id>=-1--索引有效

5、Order by导致索引失效(student_id为索引)

select ui.user_name
  from user_info ui--员工信息表
  group by ui.student_id  
而使用
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id>=-1
  将使其有效,
  在order by中只存在两种条件下可以使用索引
  (ORDER BY中所有的列必须包含在相同的索引中并保持在索引中的排列顺序
ORDER BY中所有的列必须定义为非空. )
 
6、自动选择索引
 
如果表中有两个以上(包括两个)索引,其中有一个唯一性索引,而其他是非唯一性.
在这种情况下,ORACLE将使用唯一性索引而完全忽略非唯一性索引.

7、 !=导致索引失效
 
select ui.user_name
  from user_info ui--员工信息表
  where ui.student_id!=0
在Where中使用!=将会把索引失效

8、%导致的索引失效

select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and di.look_code Like ''

select di.description student_name
      ,(select res.order_num--排名
         from result res
        where res.student_id = di.student_id
        order by result_math) order_num
  from description_info di
      ,student_info     si --学生信息表
where si.student_id = di.lookup_code(+)
   and di.lookup_type(+) = 'STUDENT_ID'
   and di.look_code Like '12%'
以上只例子,具体还是要针对各个不同的具体的业务使用

五、oracle 中的not Exists与Not in的性能巨大差异

Not Exists与Not in的作用同样是排除数据,在oracle 中使用not in并不象mysql中的执行那么快,如(
select jt1.doc_num --单据号码
      ,oalc.description school_name --学校名称
      ,oalc2.description system_name --系名称
      ,oalc.description class_name --班级名称
  from java_table1            jt1
      ,java_table_description oalc
      ,java_table_description oalc2
      ,java_table_description oalc3
where oalc.lookup_type(+) = 'JAVA_SCHOOL_NAME'
   and jt1.school_id = oalc.lookup_code(+)
   and oalc2.lookup_type(+) = 'JAVA_SYSTEM_NAME'
   and jt1.system_id = oalc2.lookup_code(+)
   and oalc3.lookup_type(+) = 'JAVA_CLASS_NAME'
   and jt1.class_id = oalc3.lookup_code(+)
   and not exists
(select jt2.header_id
          from java_table2 jt2 jt1.header_id = jt2.header_id))

select jt1.doc_num --单据号码
      ,oalc.description school_name --学校名称
      ,oalc2.description system_name --系名称
      ,oalc.description class_name --班级名称
  from java_table1            jt1
      ,java_table_description oalc
      ,java_table_description oalc2
      ,java_table_description oalc3
where oalc.lookup_type(+) = 'JAVA_SCHOOL_NAME'
   and jt1.school_id = oalc.lookup_code(+)
   and oalc2.lookup_type(+) = 'JAVA_SYSTEM_NAME'
   and jt1.system_id = oalc2.lookup_code(+)
   and oalc3.lookup_type(+) = 'JAVA_CLASS_NAME'
   and jt1.class_id = oalc3.lookup_code(+)
   and jt1.header_id not in (select jt2.header_id from java_table2 jt2)

当jt2表中的数据比较大时,就会出现巨大的差异,以上只能是我的个人理解与测试结果(java_table1 视图测试

数据量为36749,java_table2 为300条),如有其它可相互讨论

转载于:https://www.cnblogs.com/liuzhuqing/archive/2012/06/14/7480880.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/361410.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Devoxx Hackergarten的企业Web应用程序原型

我已经连续10年参加DevoxxBe了 。 这是我最喜欢的Java会议,但是谈话时间表并不总是最佳的:有时我想同时看2个精彩的谈话! 因此,在Devoxx的Hackergarten,在参加讲座之间,我们中的一些人开始构建Web应用程序以…

C# 加载C++的dll

[DllImport("kernel32")] private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string fileName) 转载于:https://www.cnblogs.com/sky-jyq/p/11509059.html

谈一谈Http Request 与 Http Response

谈一谈Http Request 与 Http Response   写在前面的话:最近帮朋友弄弄微信商城,对于微信的基础开发,基本上就是各种post、get,有时是微信服务器向我们的服务器post、get数据,有时需要我们自己的服务器向微信服务器各…

Linux常用命令和vi,gdb的使用

1.ls 命令 --------------------------------------------------------------------- ls以默认方式显示当前目录文件列表 ls -a显示所有文件包括隐藏文件 ls -l显示文件属性,包括大小,日期,符号连接,是否可读写及是否可执行 -----…

增压的jstack:如何以100mph的速度调试服务器

使用jstack调试实时Java生产服务器的指南 jstack就像U2一样-从时间的黎明就一直在我们身边,我们似乎无法摆脱它 。 除了笑话,到目前为止,jstack是您调试军用生产服务器中最方便的工具之一。 即便如此,我仍然认为它在情况恶化时能够…

(网页)css和js的版本号问题

HTML页面自动清理js、css文件的缓存,之前用的是?v11每次都要找寻到网页进行更改,非常的麻烦. <script type"text/javascript"> document.write("<script typetext/javascript srcjs/xxxx?v" new Date().getTime() ">"); </s…

wchar_t 的输出问题

wchar_t 定义的变量之所以不能输出呢&#xff1f;我查了一下资料&#xff0c;就是说宽字符类型要本地化&#xff0c;否则输不出想要结果。本地化有三条语句可以使用&#xff0c;任取其一。最后一句是全局函数&#xff0c;前两个是wcout的一个成员函数的两种表达方式。std::wcou…

Apache Cassandra和Java入门(第二部分)

要求 要遵循本教程&#xff0c;您应该已经有一个正在运行的Cassandra实例&#xff08; 一个小型集群会很好 &#xff0c;但不是必需的&#xff09;&#xff0c;已安装Datastax Java驱动程序&#xff08; 请参阅第I部分 &#xff09;&#xff0c;并且已经在这里进行了10分钟的演…

Zabbix监控多个JVM进程

一、场景说明&#xff1a; 我们这边的环境用的是微服务&#xff0c;每个程序都是有单独的进程及单独的端口号&#xff0c;但用jps查询出来的结果有些还会有重名的情况&#xff0c;所以某些脚本不太适用本场景&#xff1b; 二、需求说明&#xff1a; 需使用Zabbix-server监控每个…

(网页)swiper.js轮播图插件

Swiper4.x使用方法 1.首先加载插件&#xff0c;需要用到的文件有swiper.min.js和swiper.min.css文件。可下载Swiper文件或使用CDN。 <!DOCTYPE html> <html> <head>...<link rel"stylesheet" href"path/to/swiper.min.css"> <…

Android 4.0 Launcher源码分析系列(二)

原文&#xff1a;http://mobile.51cto.com/hot-314700.htm 上一节我们研究了Launcher的整体结构&#xff0c;这一节我们看看整个Laucher的入口点&#xff0c;同时Laucher在加载了它的布局文件Laucher.xml时都干了些什么。 我们在源代码中可以找到LauncherApplication&#xff0…

使用JFace Viewer延迟获取模型元素

Eclipse JFace Viewers显示的模型元素有时需要花费大量时间来加载。 因此&#xff0c; 工作台提供了IDeferredWorkbenchAdapter类型以在后台获取此类模型元素。 不幸的是&#xff0c;似乎仅通过DeferredTreeContentManager派生的AbstractTreeViewer支持此机制。 因此&#xff…

架构设计

好的架构是进化出来的 &#xff0c;不是设计出来的&#xff1b; 不同时期和不同环境有着不同的最佳架构&#xff1b; 存在即合理&#xff0c;合适的才是最好的&#xff1b; 转载于:https://www.cnblogs.com/sky-jyq/p/11527254.html

(网页)js每隔5分钟执行一次ajax请求的实现方法(转)

转自脚本之家: 一个页面好像只能有一个 window.οnlοadfunction(){}&#xff0c;所以要有多个事件&#xff0c;这样写就好了 window.onloadfunction(){ //假设这里每个五分钟执行一次test函数 publicBusi(); personBusi(); } function publicBusi(){ setTimeout(perso…

Flash网站流量统计的方法

在我印象中&#xff0c;一般的免费流量统计代码是不允许嵌套在Flash中使用的&#xff0c;而且也不允许在内置IFrame中嵌套使用&#xff0c;网上检索了一下&#xff0c;找到了Flash网站的流量统计办法&#xff0c;其实就是使用内置IFrame的办法&#xff0c;使用的是51啦&#xf…

从浏览器端JavaScript代码进行服务器端日志记录

应用程序日志记录是我们在部署到应用程序服务器上的应用程序中都要做的事情&#xff0c;对吗&#xff1f; 对于大多数Java开发人员而言&#xff0c;使用Log4J或Logback之类的框架似乎都是理所当然的。 但是&#xff0c;我们编写的在那些讨厌的浏览器中运行的代码又如何呢&#…

Oracle连接字符串记录

<?xml version"1.0" encoding"utf-8"?> <!--有关如何配置 ASP.NET 应用程序的详细信息&#xff0c;请访问https://go.microsoft.com/fwlink/?LinkId301879--> <configuration><configSections><!-- For more information on…

(网页)JS编程中,有时需要在一个方法返回两个个或两个以上的数据

转自脚本之家: 1 使用数组的方式&#xff0c;如下&#xff1a; <html> <head><title>JS函数返回多个值</title> </head> <body><input type"button" onclick"getNames()" value"test" /><script …

(网页)AngularJS 参考手册

指令描述ng-app定义应用程序的根元素。ng-bind绑定 HTML 元素到应用程序数据ng-bind-html绑定 HTML 元素的 innerHTML 到应用程序数据&#xff0c;并移除 HTML 字符串中危险字符ng-bind-template规定要使用模板替换的文本内容ng-blur规定 blur 事件的行为ng-change规定在内容改…

Apache Cassandra和Java入门(第一部分)

在此页面上&#xff0c;您将学到足够的知识来开始使用NoSQL Apache Cassandra和Java&#xff0c;包括如何安装&#xff0c;尝试一些基本命令以及下一步做什么。 要求 要遵循本教程&#xff0c;您应该已经有一个正在运行的Cassandra实例&#xff0c;并且已经在这里进行了10分钟…