SQL Query需要很长时间

时间:2015-11-02 11:59:35

标签: sql oracle performance oracle11g sql-tuning

下面的查询需要很长时间,我正在尝试调整查询,但似乎无法正常工作。无论如何,我可以重写查询。请帮我调整查询。

 SELECT DISTINCT TC.V_ID,A.ID,A.NAME 
    FROM OM_A_DATA TC 
       INNER JOIN ACC_DOM_EXT AE ON TC.DOMAINS = AE.DOMAIN
        LEFT OUTER JOIN ACCOUNT A ON A.ID=AE.ACCOUNTID
    WHERE V_ID not in (select distinct V_ID from DATA_ACC_MAP)


OM_A_DATA  has 41,696,776 rows
ACC_DOM_EXT has 106,725 rows
ACCOUNT has 106731 rows
DATA_ACC_MAP has 91021 rows          

2 个答案:

答案 0 :(得分:3)

not exists通常比not in更好:

SELECT DISTINCT TC.V_ID, A.ID, A.NAME 
FROM OM_A_DATA TC INNER JOIN
     ACC_DOM_EXT AE
     ON TC.DOMAINS = AE.DOMAIN LEFT OUTER JOIN
     ACCOUNT A
     ON A.ID = AE.ACCOUNTID
WHERE NOT EXISTS (SELECT 1 FROM DATA_ACC_MAP dam WHERE dam.V_ID = tc.V_ID );

显然,你应该在DATA_ACC_MAP(V_ID)上有一个索引。您还应该在其他列上包含索引:OM_A_DATA(V_ID, DOMAINS)ACC_DOM_EXT(DOMAIN, ACCOUNTID)ACCOUNT(ID, NAME)

答案 1 :(得分:1)

删除DISTINCT

SELECT DISTINCT TC.V_ID, A.ID, A.NAME 
FROM OM_A_DATA TC 
INNER JOIN ACC_DOM_EXT AE ON TC.DOMAINS = AE.DOMAIN
LEFT OUTER JOIN ACCOUNT A ON A.ID = AE.ACCOUNTID
WHERE V_ID NOT IN (SELECT V_ID FROM DATA_ACC_MAP)

或使用INNER JOIN而不是子查询。

SELECT DISTINCT TC.V_ID, A.ID, A.NAME 
FROM OM_A_DATA TC 
INNER JOIN ACC_DOM_EXT AE ON TC.DOMAINS = AE.DOMAIN
INNER JOIN DATA_ACC_MAP AD ON TC.V_ID != AD.V_ID 
LEFT OUTER JOIN ACCOUNT A ON A.ID = AE.ACCOUNTID

或使用LEFT JOIN而不是子查询和更改的WHERE子句。

SELECT DISTINCT TC.V_ID, A.ID, A.NAME 
FROM OM_A_DATA TC 
INNER JOIN ACC_DOM_EXT AE ON TC.DOMAINS = AE.DOMAIN
LEFT JOIN DATA_ACC_MAP AD ON TC.V_ID = AD.V_ID 
LEFT OUTER JOIN ACCOUNT A ON A.ID = AE.ACCOUNTID
WHERE AD.V_ID IS NULL