国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > Oracle > 正文

ORACLE SQL性能優(yōu)化系列 (四)

2024-08-29 13:31:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最大的網(wǎng)站源碼資源下載站,


 

13.       計(jì)算記錄條數(shù)

     和一般的觀點(diǎn)相反, count(*) 比count(1)稍快 , 當(dāng)然如果可以通過(guò)索引檢索,對(duì)索引列的計(jì)數(shù)仍舊是最快的. 例如 count(empno)

 

(譯者按: 在csdn論壇中,曾經(jīng)對(duì)此有過(guò)相當(dāng)熱烈的討論, 作者的觀點(diǎn)并不十分準(zhǔn)確,通過(guò)實(shí)際的測(cè)試,上述三種方法并沒(méi)有顯著的性能差別)

 

14.       用where子句替換having子句

 

     避免使用having子句, having 只會(huì)在檢索出所有記錄之后才對(duì)結(jié)果集進(jìn)行過(guò)濾. 這個(gè)處理需要排序,總計(jì)等操作. 如果能通過(guò)where子句限制記錄的數(shù)目,那就能減少這方面的開(kāi)銷(xiāo).

 

例如:

 

     低效:

     select region,avg(log_size)

     from location

     group by region

     having region region != ‘sydney’

     and region != ‘perth’

 

     高效

     select region,avg(log_size)

     from location

     where region region != ‘sydney’

     and region != ‘perth’

     group by region

(譯者按: having 中的條件一般用于對(duì)一些集合函數(shù)的比較,如count() 等等. 除此而外,一般的條件應(yīng)該寫(xiě)在where子句中)

 

15.       減少對(duì)表的查詢(xún)

在含有子查詢(xún)的sql語(yǔ)句中,要特別注意減少對(duì)表的查詢(xún).

  

例如:

     低效

          select tab_name

          from tables

          where tab_name = ( select tab_name

                                from tab_columns

                                where version = 604)

          and db_ver= ( select db_ver

                           from tab_columns

                           where version = 604)

 

     高效

          select tab_name

          from tables

          where  (tab_name,db_ver)

 = ( select tab_name,db_ver)

                   from tab_columns

                   where version = 604)

 

     update 多個(gè)column 例子:

     低效:

           update emp

           set emp_cat = (select max(category) from emp_categories),

              sal_range = (select max(sal_range) from emp_categories)

           where emp_dept = 0020;

 

     高效:

           update emp

           set (emp_cat, sal_range)

 = (select max(category) , max(sal_range)

 from emp_categories)

           where emp_dept = 0020;

 

       

16.       通過(guò)內(nèi)部函數(shù)提高sql效率.

 

     select h.empno,e.ename,h.hist_type,t.type_desc,count(*)

     from history_type t,emp e,emp_history h

     where h.empno = e.empno

and h.hist_type = t.hist_type

group by h.empno,e.ename,h.hist_type,t.type_desc;

 

通過(guò)調(diào)用下面的函數(shù)可以提高效率.

function lookup_hist_type(typ in number) return varchar2

as

    tdesc varchar2(30);

    cursor c1 is 

        select type_desc

        from history_type

        where hist_type = typ;

begin

    open c1;

    fetch c1 into tdesc;

    close c1;

    return (nvl(tdesc,’?’));

end;

 

function lookup_emp(emp in number) return varchar2

as

    ename varchar2(30);

    cursor c1 is 

        select ename

        from emp

        where empno=emp;

begin

    open c1;

    fetch c1 into ename;

    close c1;

    return (nvl(ename,’?’));

end;

 

select h.empno,lookup_emp(h.empno),

h.hist_type,lookup_hist_type(h.hist_type),count(*)

from emp_history h

group by h.empno , h.hist_type;

 

(譯者按: 經(jīng)常在論壇中看到如 ’能不能用一個(gè)sql寫(xiě)出….’ 的貼子, 殊不知復(fù)雜的sql往往犧牲了執(zhí)行效率. 能夠掌握上面的運(yùn)用函數(shù)解決問(wèn)題的方法在實(shí)際工作中是非常有意義的)

 

 (待續(xù))
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武义县| 沈阳市| 益阳市| 巩义市| 北碚区| 正定县| 临猗县| 自贡市| 吴堡县| 娄底市| 三江| 云霄县| 新竹市| 东明县| 兴义市| 综艺| 余庆县| 吉首市| 耿马| 浙江省| 香港| 宜城市| 二连浩特市| 崇仁县| 井陉县| 土默特左旗| 民和| 德清县| 西平县| 普宁市| 得荣县| 天峨县| 崇阳县| 延津县| 长兴县| 贵阳市| 忻州市| 藁城市| 定西市| 赤峰市| 雷州市|