高效率Oracle SQL语句
1、Where子句中的連接順序:
ORACLE采用自下而上的順序解析WHERE子句。
根據(jù)這個(gè)原理,表之間的連接必須寫在其他WHERE條件之前, 那些可以過濾掉最大數(shù)量記錄的條件必須寫在WHERE子句的末尾。
舉例:
(低效)
select ... from table1 t1 where t1.sal > 300 and t1.jobtype = '0001' and 20 < (select count(*) from table1 t2 where t2.pno = t1.tno;
(高效)
select ... from table1 t1 where 20 < (select count(*) from table1 t2 where t2.pno = t1.tno and t1.sal > 300 and t1.jobtype = '0001';
2、Select子句中避免使用 “ * ”:
當(dāng)你想在select子句中列出所有的column時(shí),使用動(dòng)態(tài)SQL列引用 ‘*' 是一個(gè)方便的方法。
不幸的是,這是一個(gè)非常低效的方法。
實(shí)際上,ORACLE在解析的過程中,會(huì)將 '*' 依次轉(zhuǎn)換成所有的列名, 這個(gè)工作是通過查詢數(shù)據(jù)字典完成的, 這意味著將耗費(fèi)更多的時(shí)間。
3、減少訪問數(shù)據(jù)庫的次數(shù):
當(dāng)執(zhí)行每條SQL語句時(shí),ORACLE在內(nèi)部執(zhí)行了許多工作:
解析SQL語句、估算索引的利用率、綁定變量、讀數(shù)據(jù)塊等等。
由此可見,減少訪問數(shù)據(jù)庫的次數(shù),就能實(shí)際上減少ORACLE的工作量。
舉例:
題目——我要查找編號(hào)為0001、0002學(xué)生的信息。
(低效)
select name,age,gender,address from t_student where id = '0001';
select name,age,gender,address from t_student where id = '0002';
(高效)
select a.name,a.age,a.gender,a.address,b.name,b.age,b.gender,b.address from t_student a,t_student b where a.id = '0001' and b.id = '0002';
4、使用Decode函數(shù)來減少處理時(shí)間:
使用DECODE函數(shù)可以避免重復(fù)掃描相同記錄或重復(fù)連接相同的表。
舉例:
(低效)
select count(*), sum(banace) from table1 where dept_id = '0001' and name like 'anger%';
select count(*), sum(banace) from table1 where dept_id = '0002' and name like 'anger%';
(高效)
select? count(decode(dept_id,'0001','XYZ',null)) count_01,count(decode(dept_id,'0002','XYZ',null)) count_02,
sum(decode(dept_id,'0001',dept_id,null)) sum_01,sum(decode(dept_id,'0002',dept_id,null)) sum_02
? from table1
? where name like 'anger%';
5、整合簡(jiǎn)單,無關(guān)聯(lián)的數(shù)據(jù)庫訪問:
如果你有幾個(gè)簡(jiǎn)單的數(shù)據(jù)庫查詢語句,你可以把它們整合到一個(gè)查詢中(即使它們之間沒有關(guān)系)
舉例:
(低效)
select name from table1 where id = '0001';
select name from table2 where id = '0001';
select name from table3 where id = '0001';
(高效)
select t1.name, t2.name, t3.name
??? from table1 t1, table2 t2, table3 t3
??? where t1.id(+) = '0001' and t2.id(+) = '0001' and t3.id(+) = '0001'
【注:上面例子雖然高效,但是可讀性差,需要量情而定啊!】
6、刪除重復(fù)記錄:
最高效的刪除重復(fù)記錄方法 ( 因?yàn)槭褂昧薘OWID)
舉例:
delete from table1 t1
? where t1.rowid > (select min(t2.rowid) from table1 t2 where t1.id = t2.id);
7、盡量不要使用having子句,可以考慮用where替換。
having只會(huì)在檢索出所有記錄之后才對(duì)結(jié)果集進(jìn)行過濾. 這個(gè)處理需要排序,總計(jì)等操作。
如果能通過where子句限制記錄的數(shù)目,那就能減少這方面的開銷。
8、盡量用表的別名:
當(dāng)在SQL語句中連接多個(gè)表時(shí),請(qǐng)使用表的別名并把別名前綴于每個(gè)Column上。
這樣一來,就可以減少解析的時(shí)間并減少那些由Column歧義引起的語法錯(cuò)誤。
9、用exists替代in(發(fā)現(xiàn)好多程序員不知道這個(gè)怎么用):
在許多基于基礎(chǔ)表的查詢中,為了滿足一個(gè)條件,往往需要對(duì)另一個(gè)表進(jìn)行聯(lián)接。
在這種情況下,使用exists(或not exists)通常將提高查詢的效率。
舉例:
(低效)
select ... from table1 t1 where t1.id > 10 and pno in (select no from table2 where name like 'www%');
(高效)
select ... from table1 t1 where t1.id > 10 and exists (select 1 from table2 t2 where t1.pno = t2.no and name like 'www%');
10、用not exists替代not in:
在子查詢中,not in子句將執(zhí)行一個(gè)內(nèi)部的排序和合并。
無論在哪種情況下,not in都是最低效的 (因?yàn)樗鼘?duì)子查詢中的表執(zhí)行了一個(gè)全表遍歷)。
為了避免使用not in,我們可以把它改寫成外連接(Outer Joins)或not exists。
11、用exists替換distinct:
當(dāng)提交一個(gè)包含一對(duì)多表信息的查詢時(shí),避免在select子句中使用distinct. 一般可以考慮用exists替換
舉例:
(低效)
select distinct d.dept_no, d.dept_name from t_dept d, t_emp e where d.dept_no = e.dept_no;
(高效)
select d.dept_no, d.dept_name from t_dept d where exists (select 1 from t_emp where d.dept_no = e.dept_no);
exists使查詢更為迅速,因?yàn)镽DBMS核心模塊將在子查詢的條件一旦滿足后,立刻返回結(jié)果.
12、用表連接替換exists:
通常來說,采用表連接的方式比exists更有效率。
舉例:
(低效)
select ename from emp e where exists (select 1 from dept where dept_no = e.dept_no and dept_cat = 'W');
SELECT ENAME
(高效)
select ename from dept d, emp e where e.dept_no = d.dept_no and dept_cat = 'W';
13、避免在索引列上使用is null和is not null
避免在索引中使用任何可以為空的列,ORACLE將無法使用該索引。
對(duì)于單列索引,如果列包含空值,索引中將不存在此記錄;
對(duì)于復(fù)合索引,如果每個(gè)列都為空,索引中同樣不存在此記錄;
如果至少有一個(gè)列不為空,則記錄存在于索引中。
舉例:
如果唯一性索引建立在表的A列和B列上, 并且表中存在一條記錄的A,B值為(123,null),
ORACLE將不接受下一條具有相同A,B值(123,null)的記錄(插入),
然而如果所有的索引列都為空,ORACLE將認(rèn)為整個(gè)鍵值為空而空不等于空。
因此你可以插入1000 條具有相同鍵值的記錄,當(dāng)然它們都是空!
因?yàn)榭罩挡淮嬖谟谒饕兄?所以WHERE子句中對(duì)索引列進(jìn)行空值比較將使ORACLE停用該索引。
?
轉(zhuǎn)載于:https://www.cnblogs.com/azhqiang/p/4059361.html
總結(jié)
以上是生活随笔為你收集整理的高效率Oracle SQL语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果6s强制删除id锁_#Vlog# 苹
- 下一篇: vue中echarts3d 使用(3d地