下面的查询需要很长时间,我正在尝试调整查询,但似乎无法正常工作。无论如何,我可以重写查询。请帮我调整查询。
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
答案 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