Orace查询性能优化

来源:互联网 发布:超级列表框读取数据库 编辑:程序博客网 时间:2024/06/05 19:58

Orace查询性能优化
1、避免全表扫描,在涉及到where和order by 的字段上添加索引。
2、会导致放弃索引、变成全表扫描。
(1)在where子句中进行null判断。
解决办法:在num类型的数据设置为0。
(2)在where子句中使用!=、<>操作符
(3)在where子句中使用or。
解决办法:
select id from t where num=10 or num=20
可以这样查询:
select id from t where num=10
union all
select id from t where num=20
这样写就避免了全表扫描。
(4)in 和 not in 也要慎用
解决办法:
select id from t where num in(1,2,3)
对于连续的数值,能用 between 就不要用 in 了:
select id from t where num between 1 and 3
(解决办法用between代替in)
(5)在where子句中对字段进行表达式操作
解决办法:
select id from t where num/2=100
应改为:
select id from t where num=100*2
(6)在where子句中对字段进行函数操作
解决办法
select id from t where substring(name,1,3)=’abc’ // oracle总有的是substr函数。
select id from t where datediff(day,createdate,’2005-11-30’)=0 //查过了确实没有datediff函数。
应改为:
select id from t where name like ‘abc%’
select id from t where createdate>=’2005-11-30’ and createdate<’2005-12-1’ //
oracle 中时间应该把char 转换成 date 如: createdate >= to_date(‘2005-11-30’,’yyyy-mm-dd’)
(7).很多时候用 exists 代替 in 是一个好的选择:
select num from a where num in(select num from b)
用下面的语句替换:
select num from a where exists(select 1 from b where num=a.num)

0 0
原创粉丝点击