oracle查詢最佳化,oracle最佳化

來源:互聯網
上載者:User

oracle查詢最佳化,oracle最佳化

1. 查詢條件合理排序

Oracle採用自下而上的順序解析WHERE字據,從最佳化效能角度考慮,建議將那些可以過濾掉大量記錄行的條件寫在WHERE子句的末尾,而將表

之間的串連條件置於其他WHERE子句之前,即對易排查的條件先做判斷處理,這樣在過濾掉儘可能多的記錄後再進行等值串連,可以提高檢索效率。

例如:

SELECT empno, ename, job, sal, dept.deptno, dname 

FROM emp, dept 

WHERE emp.deptno = dept.deptno AND emp.deptno = 20;

要比下述語句的查詢效率高一些:

SELETE empno, ename, job, sal, dept.deptno, dname

FROM emp, dept

WHERE emp.deptno = 20 AND emp.deptno = dept.deptno;

2.串連中使用表別名

在進行串連查詢時,建議在SQL語句中使用表的別名, 並把別名首碼與每個欄位上。這樣可以減少解析的時間,並可避免因欄位名存在歧義(使用多個

表中出現的同名欄位)而導致的語法錯誤。例如下述語句:

SELECT e.empno, e.ename, e.job, e.sal, e.deptno, d.name

FROM emp e, dept d

WHERE e.deptno = d.deptno AND e.deptno = 20;

3.用EXISTS替換DISTINCT

在進行一對多關聯性的表間串連查詢時,如果要剔除結果中的重複行,可以考慮使用EXISTS(結合子查詢)替換DISTINCT。

例如:

SELECT deptno, dname FROM dept d WHERE EXIST (SELECT 'y' FROM emp e WHERE e.deptno = d.deptno);

4.用WHERE替換HAVING

由於SELECT語句的執行順序為:先WHERE子句, 在GROUP BY 子句, 然後SELECT查詢, 再後HAVINT子句,最後是ORDER BY子句,因此在進行分組

查詢時,如果過濾條件不涉及分組計算,則應該使用WHERE語句替換HAVING指定的過濾條件

例如:

SELECT deptno, avg(sal) FROM emp

WHERE deptno IN (10, 20)

GROUP BY deptno;

效率會高於下面

SELECT deptno, avg(sal) FROM emp

GROUP BY deptno

HAVING deptno IN (10, 20);

當然, 如果分組查詢的過濾條件設計分組計算,就只能在HAVING子句中指定了。

5.使用系統函數

系統函數畢竟是資料庫廠商提供的“專業”解決方案,相對更可靠一些



oracle 查詢最佳化

使用exists(),不會遍曆全表的
select case when exists(select * from user where name ='xxx') then 1 else 0 end as aa from dual
 
oracle 查詢最佳化

暈死,你那個20130131都知道是查2013年1月份的了,你就直接='201301'就好了,substr用多了會造成查詢速度緩慢的
豎著顯示就union all就好了,像樓上那位說的那樣,橫著顯示這樣
select sum(case when term_mon<3 then 1 else 0 end) 小於3數量,sum(case when term_mon<3 then bal else 0 end) 小於3值,sum(case when term_mon>3 and term_mon<6 then 1 else 0 end) 大於3小於6數量,sum(case when term_mon>3 and term_mon<6 then bal else 0 end) 大於3小於6值,sum(case when term_mon>6 and term_mon<12 then 1 else 0 end) 大於6小於12數量,sum(case when term_mon>6 and term_mon<12 then bal else 0 end) 大於6小於12值,sum(case when term_mon>12 then 1 else 0 end) 大於12數量,sum(case when term_mon>12 then bal else 0 end) 大於12值from rpt_xd where substr(beg_date,1,6)= '201301'
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.