oracle数据库优化
2024-08-29 13:29:12
供稿:网友
中国最大的web开发资源网站及技术社区,
优化就是选择最有效的方法来执行sql语句。oracle优化器选择它认为最有效的
方法来执行sql语句。
1). is null和is not null
如果某列存在null值,即使对该列建立索引也不会提高性能。
2). 为不同的工作编写不同的sql语句块
为完成不同的工作编写一大块sql程序不是好方法。它往往导致每个任务的结果不优
化。若要sql完成不同的工作,一般应编写不同的语句块比编写一个要好。
3). 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
通常查询速度快的原因。
4). not 运算符
select * from employee where salary<>1000;
select * from employee where salary<1000 or salary>1000;
第一句sql语句的执行效率不如第二句,因为第二句sql语句可以使用索引。
5). order by 语句
order by 语句的执行效率很低,因为它要排序。应避免在order by 字句中使用表达式。
6). 列的连接
select * from employee where name||department=’zyzbioinfo’;
select * from employee where name=’zyz’ and department=’bioinfo’;
这两个查询,第二句比第一句会快,因为对于有连接运算符’||’的查询,oracle优化器是不
会使用索引的。
7). 通配符‘%’当通配符出现在搜索词首时,oracle优化器不使用索引
select * from employee where name like ‘%z%’;
select * from employee where name like ‘z%’;
第二句的执行效率会比第一句快,但查询结果集可能会不同。
8). 应尽量避免混合类型的表达式
假设字段studentno为varchar2类型
有语句select * from student where studentno>123;
则oracle会有一个隐含的类型转换。隐含的类型转换可能会使oracle优化器忽略索引。
这时应使用显式的类型转换select * from student where studentno=to_char(123)。
9).distinct
distinct总是建立一个排序,所以查询速度也慢。