一條sql語(yǔ)句變得非常慢的原因及其解決方法

字號(hào):


    現(xiàn)象:一條sql突然運(yùn)行地特別慢.
    select uidtable.column_value, first_name||' '
    ||last_name, company, job_title, upper(member_level),
    upper(service_value)
    from (select * from table(select cast(multiset
    (select b from bbb)as taaa) from dual)) uidtable,member
    where uidtable.column_value = member.login_id(+)
    and member.site='alibaba' and member.site='test';
    出錯(cuò)原因:用戶增加了一個(gè)條件member.site=test,造成連接地順序變化了,原來(lái)地驅(qū)動(dòng)表是uidtable(最多1024條記錄),現(xiàn)在變成了member表做驅(qū)動(dòng)(600w條).所以這條語(yǔ)句變地巨慢.
    但是既然是外連接,為什么連接地順序會(huì)改變呢?因?yàn)橥膺B接地連接順序不是由cost決定地,而是由連接地條件決定地.發(fā)現(xiàn)執(zhí)行計(jì)劃如下:
    -------------------------------------------------------
    | id | operation | name | rows | bytes | cost |
    --------------------------------------------------------
    | 0 | select statement | | 1018 | 72278 | 8155 |
    | 1 | nested loops | | 1018 | 72278 | 8155 |
    | 2 | view | | 4072 | 69224 | 11 |
    | 3 | collection iterator subquery fetch| | | | |
    | 4 | table access full | dual | 4072 | | 11 |
    | 5 | table access full | bbb | 41 | 287 | 2 |
    | 6 | table access by index rowid | member | 1 | 54 | 2 |
    |* 7 | index unique scan | member_site_lid_pk | 4 | | 1 |
    -------------------------------------------------
    為什么根本就沒有執(zhí)行外連接呢?問(wèn)題出在member.site='test'這個(gè)條件上,因?yàn)閷?duì)外連接地表加了條件,造成外連接失效.改為member.site(+)='test'后,問(wèn)題徹底解決.
    ---------------------------------------------------
    | id | operation | name | rows | bytes | cost |
    -----------------------------------------------------
    | 0 | select statement | | 1018 | 72278 | 8155 |
    | 1 | nested loops | | 1018 | 72278 | 8155 |
    | 2 | view | | 4072 | 69224 | 11 |
    | 3 | collection iterator subquery fetch| | | | |
    | 4 | table access full | dual | 4072 | | 11 |
    | 5 | table access full | bbb | 41 | 287 | 2 |
    | 6 | table access by index rowid | member | 1 | 54 | 2 |
    |* 7 | index unique scan | member_site_lid_pk | 4 | | 1 |