FULL JOIN還是少用為妙

2021-08-31 11:00:04 字數 1459 閱讀 3983

今天早上到公司,發現乙個job跑了幾個小時還沒停下來,測試的時候這個任務執行兩分鐘就結束了。於是找dba幫我查原因,原**大致如此:

select g2.col1, g1.col2

from (select nvl(tt.col1,pp.col1) col1, nvl(tt.col2,0) + nvl(pp.col2,0) col2

from (select u.col1, count(*) as col2

from a g

inner join b u on g.username = u.username

where ...

group by u.col1) tt

full join

(select col1, count(*) as col2

from c t1, d t2

where ...

group by t2.col1) pp

on tt.col1 = pp.col1) g1,

b g2

where ...;

經過分解執行,發現沒有問題,只要整體執行就特別慢。檢視執行計畫,發現問題出現在full join上, tt的結果比較多,pp的結果相當少。dba建議改用unoin all,於是改為:

select g2.col1, g1.col2

from (select col1, sum(col2)

from (select u.col1, count(*) as col2

from a g

inner join b u on g.username = u.username

where ...

group by u.col1

union all

select col1, count(*) as col2

from c t1, d t2

where ...

group by t2.col1)

group by username) g1,

b g2

where ...;

重新執行任務,ok!

最近又遇到oracle的乙個bug,在儲存過程中執行cube函式,產生600錯誤:

-- for

execute immediate 'alter session set "_optimizer_cost_based_transformation" = off';

同時,今天dba幫我解決了乙個問題,還是full join引起的,這個sql的執行計畫cost值大的可怕, 執行兩個小時進度還只是百分之零點幾.

dba拿出了殺手鐗:

alter

session

set"_complex_view_merging"

=false;

問題搞定!(當然,實際上可以有別的辦法繞過去,那就是不使用full join也能解決問題) 

Mysql實現多表full join

mysql是不支援full join操作的,但是確實有這個需求應該怎麼處理?學生報名參加的課程表 insert into test course student,course values 楊過 體育課 小龍女 舞蹈課 郭靖 語文課 黃蓉 高數課 學生報名參加的社團 insert into test...

FULL JOIN 查詢取並集

現在,我們希望列出所有的人,以及他們的定單,以及所有的定單,以及定購它們的人。您可以使用下面的 select 語句 select persons.lastname,persons.firstname,orders.orderno from persons full join orders on pe...

sql中的幾種join 及 full join問題

注意 oracle資料庫支援full join,mysql是不支援full join的,但仍然可以同過左外連線 union 右外連線實現 初始化sql語句 join 建表語句 drop database ifexists test create database test use test 左表t1...