Query:
select max(b.counter) as counter,
b.m_group,
b.m_code,
a.s_id,
s.id_sk
from tbl_various a, tbl_map b, tbl_sheet s
where coalesce(b.part, a.part) = a.part
and coalesce(b.nums, to_number(a.nums)) = a.nums
and coalesce(b.interc, a.interc) = a.interc
and coalesce(b.segment, a.segment) = a.segment
and coalesce(b.acountry, a.acountry) = a.acountry_midas
and coalesce(b.orig_name, a.orig_name) = a.orig_name
and coalesce(b.fact, a.fact) = a.fact
and b.sect is not null
and s.m_code = b.m_code
group by b.m_group, b.m_code, a.s_id, s.id_sk;
Plan:
SELECT STATEMENT, GOAL = ALL_ROWS 86763 1 169
HASH GROUP BY 86763 1 169
HASH JOIN 86762 1 169
TABLE ACCESS FULL TBL_MAP 2 1717 92718
MERGE JOIN CARTESIAN 79688 300133251 34515323865
TABLE ACCESS FULL TBL_SHEET 5 912 18240
BUFFER SORT 79682 329274 31281030
TABLE ACCESS FULL TBL_VARIOUS 87 329274 31281030
Group by is too slow… How does it speed up?
Any row in
bthat hasnullvalues in all the coalesce’d columns and isnot nullinb.sectandb.m_codewill match any row ina, ie do a cartesian join.My guess is that this is causing the problem. Even if there actually are no such rows in
b, the optimizer may go for the cartesian join.You may be able to avoid this by adding
Also, its always a good idea to keep your statistics up-to-date (though I’m not enough of an Oracle expert to know how to do that).
EDIT: This is functionally identical and might work better:
This will make sure that at least one value matches.