我需要幫助優化以下查詢。完成需要很長時間。它需要近213秒。由於一些限制,我無法添加索引並必須與現有索引一起使用。需要幫助優化oracle查詢
INSERT INTO temp_table_1
(USER_ID, role_id, participant_code, status_id)
WITH A AS
(SELECT USER_ID user_id,ROLE_ID, STATUS_ID,participant_code
FROM USER_ROLE WHERE participant_code IS NOT NULL), --1
B AS
(SELECT ROLE_ID
FROM CMP_ROLE
WHERE GROUP_ID = 3),
C AS (SELECT USER_ID FROM USER) --2
SELECT USER_ID,ROLE_ID,PARTICIPANT_CODE,MAX(STATUS_ID)
FROM A INNER JOIN B USING (ROLE_ID)
INNER JOIN C USING (USER_ID)
GROUP BY USER_ID,role_id,participant_code ;
--1 = query when ran alone takes 100+ seconds
--2 = query when ran alone takes 19 seconds
DELETE temp_table_1
WHERE ROWID NOT IN
(SELECT a.ROWID
FROM temp_table_1 a,
USER_ROLE b
WHERE a.status_id = b.status_id
AND (b.ACTIVE IN (1) OR (b.ACTIVE IN (0,3)
AND SYSDATE BETWEEN b.effective_from_date AND b.effective_to_date))
);
看起來好像編寫查詢的人試圖首先將所有內容放入臨時表中,然後從臨時表中刪除記錄。剩下的就是實際結果。
不能這樣做,不需要刪除?我們只是得到所需的結果,因爲這將節省時間?
你可以發佈解釋計劃輸出? – 2010-03-30 20:55:24