I have two queries that run very fast on their own (Milliseconds) however when I combine them with a UNION the performance nose dives. I can't think of anything obviuos, they reurn the same sort of data, and the query does parse OK, but it just takes for ever to return, also when I run the queries on their own the statement cost is 2 and 3, but the unioned one has a cost of 20!!. The query is below and the explain plan. If any one can see a problem or recommend a new approach I'd be very grateful
Cheers,
Mike.
select * from (select /* RULE */ fol_folder_title, fol_folder_id, fol_folder_no, 0 item_no, 0 item_id, 0 item_yn, 0 item
from nim_cfg_folders
where exists (select /* RULE */ 1
from nim_cfg_folder_items,
nim_cfg_items,
(select pac_access_id
from nim_sec_user_profiles,
nim_sec_profile_accesses
where pac_profile_id = upr_profile_id
and upr_user_id = 133351) pac2
where fol_folder_id = fit_folder_id
and fit_item_id = ite_item_id
and ite_web_dll_name is not null
and ite_briefcase_specific_yn = 'N'
and nvl(ite_access_method,pac2.pac_access_id) = pac2.pac_access_id
)
and exists (select 1
from nim_sec_user_profiles,
nim_sec_profile_accesses
where pac_profile_id = upr_profile_id
and upr_user_id = 133351
and nvl(fol_access_method,pac_access_id) = pac_access_id))
union
select ite_item_title, fol_folder_id, fol_folder_no, fit_item_no, fit_item_id, 1 item_yn, ite_item_id
from nim_cfg_folders, nim_cfg_folder_items, nim_cfg_items
where fol_folder_id = 10716
and fol_folder_id = fit_folder_id
and fit_item_id = ite_item_id
and ite_web_dll_name is not null
and ite_briefcase_specific_yn = 'N'
and exists (select 1
from nim_sec_user_profiles, nim_sec_profile_accesses
where upr_user_id = 133351
and pac_profile_id = upr_profile_id
and nvl(ite_access_method,pac_access_id) = pac_access_id
)
order by 1,3,5
SELECT STATEMENT Optimizer=CHOOSE (Cost=20 Card=4 Bytes=406)
SORT (UNIQUE) (Cost=19 Card=4 Bytes=406)
UNION-ALL
NESTED LOOPS (Cost=9 Card=2 Bytes=230)
NESTED LOOPS (Cost=9 Card=32 Bytes=3424)
NESTED LOOPS (Cost=7 Card=2 Bytes=198)
NESTED LOOPS (Cost=7 Card=32 Bytes=2912)
NESTED LOOPS (Cost=5 Card=2 Bytes=166)
NESTED LOOPS (Cost=3 Card=2 Bytes=130)
TABLE ACCESS (FULL) OF NIM_CFG_ITEMS (Cost=1 Card=2 Bytes=118)
INDEX (FULL SCAN) OF FIT_C_PK (UNIQUE) (Cost=1 Card=51 Bytes=306)
TABLE ACCESS (BY INDEX ROWID) OF NIM_CFG_FOLDERS (Cost=1 Card=7 Bytes=126)
INDEX (UNIQUE SCAN) OF FOL_C_PK (UNIQUE)
INDEX (FAST FULL SCAN) OF PAC_C_PK (UNIQUE) (Cost=1 Card=1576 Bytes=12608)
INDEX (UNIQUE SCAN) OF UPR_C_PK (UNIQUE)
INDEX (FAST FULL SCAN) OF PAC_C_PK (UNIQUE) (Cost=1 Card=1576 Bytes=12608)
INDEX (UNIQUE SCAN) OF UPR_C_PK (UNIQUE)
NESTED LOOPS (Cost=6 Card=2 Bytes=176)
NESTED LOOPS (Cost=6 Card=32 Bytes=2560)
NESTED LOOPS (Cost=4 Card=2 Bytes=144)
NESTED LOOPS (Cost=2 Card=2 Bytes=128)
TABLE ACCESS (BY INDEX ROWID) OF NIM_CFG_FOLDERS (Cost=1 Card=1 Bytes=5)
INDEX (UNIQUE SCAN) OF FOL_C_PK (UNIQUE)
TABLE ACCESS (FULL) OF NIM_CFG_ITEMS (Cost=1 Card=2 Bytes=118)
TABLE ACCESS (BY INDEX ROWID) OF NIM_CFG_FOLDER_ITEMS (Cost=1 Card=8 Bytes=64)
INDEX (UNIQUE SCAN) OF FIT_C_PK (UNIQUE)
INDEX (FAST FULL SCAN) OF PAC_C_PK (UNIQUE) (Cost=1 Card=1576 Bytes=12608)
INDEX (UNIQUE SCAN) OF UPR_C_PK (UNIQUE)
Cheers,
Mike.
select * from (select /* RULE */ fol_folder_title, fol_folder_id, fol_folder_no, 0 item_no, 0 item_id, 0 item_yn, 0 item
from nim_cfg_folders
where exists (select /* RULE */ 1
from nim_cfg_folder_items,
nim_cfg_items,
(select pac_access_id
from nim_sec_user_profiles,
nim_sec_profile_accesses
where pac_profile_id = upr_profile_id
and upr_user_id = 133351) pac2
where fol_folder_id = fit_folder_id
and fit_item_id = ite_item_id
and ite_web_dll_name is not null
and ite_briefcase_specific_yn = 'N'
and nvl(ite_access_method,pac2.pac_access_id) = pac2.pac_access_id
)
and exists (select 1
from nim_sec_user_profiles,
nim_sec_profile_accesses
where pac_profile_id = upr_profile_id
and upr_user_id = 133351
and nvl(fol_access_method,pac_access_id) = pac_access_id))
union
select ite_item_title, fol_folder_id, fol_folder_no, fit_item_no, fit_item_id, 1 item_yn, ite_item_id
from nim_cfg_folders, nim_cfg_folder_items, nim_cfg_items
where fol_folder_id = 10716
and fol_folder_id = fit_folder_id
and fit_item_id = ite_item_id
and ite_web_dll_name is not null
and ite_briefcase_specific_yn = 'N'
and exists (select 1
from nim_sec_user_profiles, nim_sec_profile_accesses
where upr_user_id = 133351
and pac_profile_id = upr_profile_id
and nvl(ite_access_method,pac_access_id) = pac_access_id
)
order by 1,3,5
SELECT STATEMENT Optimizer=CHOOSE (Cost=20 Card=4 Bytes=406)
SORT (UNIQUE) (Cost=19 Card=4 Bytes=406)
UNION-ALL
NESTED LOOPS (Cost=9 Card=2 Bytes=230)
NESTED LOOPS (Cost=9 Card=32 Bytes=3424)
NESTED LOOPS (Cost=7 Card=2 Bytes=198)
NESTED LOOPS (Cost=7 Card=32 Bytes=2912)
NESTED LOOPS (Cost=5 Card=2 Bytes=166)
NESTED LOOPS (Cost=3 Card=2 Bytes=130)
TABLE ACCESS (FULL) OF NIM_CFG_ITEMS (Cost=1 Card=2 Bytes=118)
INDEX (FULL SCAN) OF FIT_C_PK (UNIQUE) (Cost=1 Card=51 Bytes=306)
TABLE ACCESS (BY INDEX ROWID) OF NIM_CFG_FOLDERS (Cost=1 Card=7 Bytes=126)
INDEX (UNIQUE SCAN) OF FOL_C_PK (UNIQUE)
INDEX (FAST FULL SCAN) OF PAC_C_PK (UNIQUE) (Cost=1 Card=1576 Bytes=12608)
INDEX (UNIQUE SCAN) OF UPR_C_PK (UNIQUE)
INDEX (FAST FULL SCAN) OF PAC_C_PK (UNIQUE) (Cost=1 Card=1576 Bytes=12608)
INDEX (UNIQUE SCAN) OF UPR_C_PK (UNIQUE)
NESTED LOOPS (Cost=6 Card=2 Bytes=176)
NESTED LOOPS (Cost=6 Card=32 Bytes=2560)
NESTED LOOPS (Cost=4 Card=2 Bytes=144)
NESTED LOOPS (Cost=2 Card=2 Bytes=128)
TABLE ACCESS (BY INDEX ROWID) OF NIM_CFG_FOLDERS (Cost=1 Card=1 Bytes=5)
INDEX (UNIQUE SCAN) OF FOL_C_PK (UNIQUE)
TABLE ACCESS (FULL) OF NIM_CFG_ITEMS (Cost=1 Card=2 Bytes=118)
TABLE ACCESS (BY INDEX ROWID) OF NIM_CFG_FOLDER_ITEMS (Cost=1 Card=8 Bytes=64)
INDEX (UNIQUE SCAN) OF FIT_C_PK (UNIQUE)
INDEX (FAST FULL SCAN) OF PAC_C_PK (UNIQUE) (Cost=1 Card=1576 Bytes=12608)
INDEX (UNIQUE SCAN) OF UPR_C_PK (UNIQUE)