2008-04-11

Oracle SQL优化技巧

关键字: 優化
我觉得主要应该从5个方面进行调整:
1.去掉不必要的大型表的全表扫描
2.缓存小型表的全表扫描
3.检验优化索引的使用
4.检验优化的连接技术
5.尽可能减少执行计划的Cost

现在简单的举几个例子

Where子句中有“!=”将不使用索引
select account_name from test where amount != 0 (不使用)
select account_name from test where amount > 0 (使用)

Where条件中对字段增加处理函数将不使用该列的索引
select * from emp where to_char(hire_date,'yyyymmdd')='20080411' (不使用)
select * from emp where hire_date = to_char('20080411','yyyymmdd') (使用)

避免在索引列上使用IS NULL和 IS NOT NULL
select * from emp where dept_code is not null (不使用)
select * from emp where dept_code > 0 (使用)

通配符% 的使用
select * from emp where name like '%A' (不使用索引)
select * from emp where name like 'A%' (使用索引)
评论
morris 2008-04-11
yanyanlong 写道


可以通过建立反向索引..让
select * from emp where name like '%A'
也使用索引


好像我們一般不用反向索引 再説 reverse索引一般用于ops环境
yanyanlong 2008-04-11
通配符% 的使用
select * from emp where name like '%A' (不使用索引)
select * from emp where name like 'A%' (使用索引)

可以通过建立反向索引..让
select * from emp where name like '%A'
也使用索引
morris 2008-04-11
補充一下:

在含有子查询的SQL语句中,要特别注意减少对表的查询.例子:
SELECT EMP_NO FROM EMP WHERE (GROUP,NAME) = ( SELECT
COLUMN1,COLUMN2 FROM TEST WHERE TEST_ID = 604)

最高效的删除重复记录方法 ( 因为使用了ROWID)例子:
DELETE FROM EMP E WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM EMP X WHERE X.EMP_NO = E.EMP_NO);

sql语句用大写的;因为oracle总是先解析sql语句,把小写的字母转换成大写的再执行

在java代码中用到preparedStatement的時候尽量少用连接符“+”连接字符串!
morris 2008-04-11
xiaoych 写道
避免在索引列上使用IS NULL和 IS NOT NULL
select * from emp where dept_code is not null (不使用)
select * from emp where dept_code > 0 (使用)

---------------------

这个应该是走索引的吧?!


不好意思 是我錯了 應該是IS NULL不會用到索引
而IS NOT NULL用到索引全掃描
xiaoych 2008-04-11
避免在索引列上使用IS NULL和 IS NOT NULL
select * from emp where dept_code is not null (不使用)
select * from emp where dept_code > 0 (使用)

---------------------

这个应该是走索引的吧?!
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

morris
搜索本博客
存档
最新评论