The following query took 25 senconds to run, the first select returned 1645 records and took 406 msec, the second select returned 0 record and took 31 msec. I had tried to use the UNION ALL but the response time seems inconsistent.
SELECT S.SUPPLIER_ID AS "SUPPLIER_ID"
FROM COMPANY_DEFAULT_PERMISSIONS cdp, USERNAME u, SUPPLIER S
WHERE u.username_id = 117120
AND u.company_id = cdp.company_id
AND U.COMPANY_ID = S.COMPANY_ID
AND S.SUPPLIER_ID NOT IN (SELECT SUPPLIER_ID
FROM SUPPLIER_SPM_ACL SSA
WHERE SSA.SUPPLIER_ID = S.SUPPLIER_ID AND (SSA.username_id = 117120 OR SSA.user_group_id IN (SELECT user_group_id FROM USERNAME_GROUP_MAP WHERE username_id = 117120)))
AND cdp.can_read = 1
AND cdp.object_type_id = 9
UNION
SELECT S.SUPPLIER_ID AS "SUPPLIER_ID"
FROM SUPPLIER_SPM_ACL SSA, USERNAME u, SUPPLIER S
WHERE u.username_id = 117120
AND u.username_id = SSA.username_id
AND U.COMPANY_ID = S.COMPANY_ID
AND S.SUPPLIER_ID = SSA.SUPPLIER_ID
AND SSA.can_read = 1
After I added the index hint and the execution plan doesn't have any full table scan, however the query run much slower ( from 390 msec to 56 sec).
SELECT /*+ INDEX(S SUPPLIER_PK) */ S.SUPPLIER_ID AS "SUPPLIER_ID"
FROM COMPANY_DEFAULT_PERMISSIONS cdp, USERNAME u, SUPPLIER S
WHERE u.username_id = 117120
AND u.company_id = cdp.company_id
AND U.COMPANY_ID = S.COMPANY_ID
AND S.SUPPLIER_ID NOT IN(SELECT /*+ INDEX(SSA SUPPLIER_SPM_ACL_PK) */ SUPPLIER_ID
FROM SUPPLIER_SPM_ACL SSA
WHERE SSA.SUPPLIER_ID = S.SUPPLIER_ID AND
(SSA.username_id = 117120 OR SSA.user_group_id
IN (SELECT user_group_id FROM USERNAME_GROUP_MAP WHERE username_id = 117120)))
AND cdp.can_read = 1
AND cdp.object_type_id = 9
I don't see how forcing the index with a hint would improve the performance if the CBO determined a FTS to be more efficient from the get go.
Perhaps if you explained what each query is trying to accomplish.
Is there any reason an OR statement in the where-clause could not
accomplish what you need?
Does this improve anything?
PHP Code:
select decode(a, null, b, a) supplier_id from
(SELECT s.supplier_id a
FROM company_default_permissions cdp, username u, supplier s
WHERE u.username_id = 117120
AND u.company_id = cdp.company_id
AND u.company_id = s.company_id
AND s.supplier_id NOT IN (
SELECT supplier_id
FROM supplier_spm_acl ssa
WHERE ssa.supplier_id = s.supplier_id
AND ( ssa.username_id = 117120
OR ssa.user_group_id IN (
SELECT user_group_id FROM username_group_map
WHERE username_id = 117120)))
AND cdp.can_read = 1
AND cdp.object_type_id = 9)
full outer join
(SELECT s.supplier_id b
FROM supplier_spm_acl ssa, username u, supplier s
WHERE u.username_id = 117120
AND u.username_id = ssa.username_id
AND u.company_id = s.company_id
AND s.supplier_id = ssa.supplier_id
AND ssa.can_read = 1)
on a = b
Bookmarks