oracle数据库优化

优化就是选择最有效的方法来执行 SQL 语句。 Oracle 优化器选择它认为最有效的

方法来执行 SQL 语句。

  1. . IS NULL 和 IS NOT NULL

如果某列存在 NULL 值,即使对该列建立索引也不会提高性能。

  1. . 为不同的工作编写不同的 SQL 语句块

为完成不同的工作编写一大块 SQL 程序不是好方法。它往往导致每个任务的结果不优

化。若要 SQL 完成不同的工作,一般应编写不同的语句块比编写一个要好。

  1. . IN 和 EXISTS

Select name from employee where name not in (select name from student);

Select name from employee where not exists (select name from student);

第一句 SQL 语句的执行效率不如第二句。

通过使用 EXISTS , Oracle 会首先检查主查询,然后运行子查询直到它找到第一个匹配

项,这就节省了时间。 Oracle 在执行 IN 子查询时,首先执行子查询,并将获得的结果

列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待

子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用 EXISTS 比使用 IN

通常查询速度快的原因。

  1. . NOT 运算符

Select * from employee where salary<>1000;

Select * from employee where salary<1000 or salary>1000;

第一句 SQL 语句的执行效率不如第二句,因为第二句 SQL 语句可以使用索引。

  1. . Order By 语句

Order By 语句的执行效率很低,因为它要排序。应避免在 Order By 字句中使用表达式。

  1. . 列的连接

select * from employee where name||department=’ZYZBIOINFO’;

select * from employee where name=’ZYZ’ and department=’BIOINFO’;

这两个查询,第二句比第一句会快,因为对于有连接运算符’ || ’的查询 ,Oracle 优化器是不

会使用索引的。

  1. . 通配符‘%’当通配符出现在搜索词首时, Oracle 优化器不使用索引

Select * from employee where name like ‘%Z%’;

Select * from employee where name like ‘Z%’;

第二句的执行效率会比第一句快,但查询结果集可能会不同。

  1. . 应尽量避免混合类型的表达式

假设字段 studentno 为 VARCHAR2 类型

有语句 select * from student where studentno>123;

则 Oracle 会有一个隐含的类型转换。隐含的类型转换可能会使 Oracle 优化器忽略索引。

这时应使用显式的类型转换 select * from student where studentno=to_char(123) 。

  1. . DISTINCT

DISTINCT 总是建立一个排序,所以查询速度也慢。

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus