2013-08-23 78 views
0

我有一個查詢,看起來像這樣:單更改查詢,大大增加了執行時間

SELECT DISTINCT SUM(SFRSTCR_CREDIT_HR) AS NUM_CREDIT_HRS, COUNT(SFRSTCR_PIDM) OVER(PARTITION BY SUM(SFRSTCR_CREDIT_HR)) AS NUM_STUDENTS 
FROM SATURN.SFRSTCR 
WHERE SFRSTCR_TERM_CODE = '&TermCode' 
    AND SFRSTCR_PIDM IN(SELECT DISTINCT SPRIDEN_PIDM 
         FROM SATURN.SPRIDEN, SATURN.SGBSTDN ta1, SATURN.SFRSTCR 
         WHERE SPRIDEN_PIDM = ta1.SGBSTDN_PIDM 
         AND SPRIDEN_PIDM = SFRSTCR_PIDM 
         AND ta1.SGBSTDN_TERM_CODE_EFF = (SELECT MAX(ta2.SGBSTDN_TERM_CODE_EFF) 
                 FROM SATURN.SGBSTDN ta2 
                 WHERE ta1.SGBSTDN_PIDM = ta2.SGBSTDN_PIDM) 
         AND ta1.SGBSTDN_STST_CODE = 'AS' 
         AND ta1.SGBSTDN_LEVL_CODE = 'US' 
         AND ta1.SGBSTDN_RESD_CODE = 'R' 
         AND SPRIDEN_CHANGE_IND IS NULL 
         AND SFRSTCR_TERM_CODE = '&TermCode' 
         AND (SFRSTCR_RSTS_CODE LIKE 'R%' 
         OR SFRSTCR_RSTS_CODE LIKE 'W%') 
         AND SFRSTCR_CREDIT_HR >= 1) 
    AND SFRSTCR_PIDM NOT IN(SELECT DISTINCT SFRSTCR_PIDM 
          FROM SATURN.SFRSTCR, SATURN.SSBSECT 
          WHERE SFRSTCR_TERM_CODE = SSBSECT_TERM_CODE 
          AND SFRSTCR_CRN = SSBSECT_CRN 
          AND SFRSTCR_TERM_CODE = '&TermCode' 
          AND (SFRSTCR_RSTS_CODE LIKE 'R%' 
          OR SFRSTCR_RSTS_CODE LIKE 'W%') 
          AND SFRSTCR_CREDIT_HR >= 1 
          GROUP BY SFRSTCR_PIDM 
          HAVING MAX(SSBSECT_SEQ_NUMB) LIKE '0%') 
GROUP BY SFRSTCR_PIDM 
HAVING SUM(SFRSTCR_CREDIT_HR) >=1 
ORDER BY NUM_CREDIT_HRS 

此查詢需要5秒執行。但是,如果我將行AND ta1.SGBSTDN_RESD_CODE = 'R'更改爲AND ta1.SGBSTDN_RESD_CODE = 'N',則執行時間會增加到1小時45分鐘。 'N'也是SGBSTDN_RESD_CODE中的有效值,並且如果有的話應該有比'R'值更少的'N'值。

該數據庫是Oracle。有誰知道爲什麼會出現這種行爲?

編輯:解釋計劃似乎是相同的。

帶 'R': 計劃

1 One or more rows were retrieved using index SATURN.PK_SGBSTDN . The index was scanned in ascending order.. 
2 The rows were sorted in order to be grouped. 
3 A view definition was processed, either from a stored view SYS.VW_SQ_1 or as defined by steps 2. 
4 Rows were retrieved using the unique index SATURN.PK_SGBSTDN . 
5 Rows from table SATURN.SGBSTDN were accessed using rowid got from an index. 
6 For each row retrieved by step 3, the operation in step 5 was performed to find a matching row. 
7 One or more rows were retrieved using index SATURN.SPRIDEN_PIDM_INDEX . The index was scanned in ascending order.. 
8 For each row retrieved by step 6, the operation in step 7 was performed to find a matching row. 
9 One or more rows were retrieved using index SATURN.SFRSTCR_KEY_INDEX3 . The index was scanned in ascending order.. 
10 For each row retrieved by step 8, the operation in step 9 was performed to find a matching row. 
11 Rows from table SATURN.SFRSTCR were accessed using rowid got from an index. 
12 For each row retrieved by step 10, the operation in step 11 was performed to find a matching row. 
13 One or more rows were retrieved using index SATURN.PK_SFRSTCR . The index was scanned in ascending order.. 
14 Rows from table SATURN.SFRSTCR were accessed using rowid got from an index. 
15 Rows were retrieved using the unique index SATURN.PK_SSBSECT . 
16 For each row retrieved by step 14, the operation in step 15 was performed to find a matching row. 
17 Rows from table SATURN.SSBSECT were accessed using rowid got from an index. 
18 For each row retrieved by step 16, the operation in step 17 was performed to find a matching row. 
19 SORT GROUP BY NOSORT 
20 For the rows returned by step 19, filter out rows depending on filter criteria. 
21 One or more rows were retrieved using index SATURN.PK_SFRSTCR . The index was scanned in ascending order.. 
22 Rows from table SATURN.SFRSTCR were accessed using rowid got from an index. 
23 SORT GROUP BY NOSORT 
24 WINDOW SORT 
25 The rows from step 24 were sorted to eliminate duplicate rows. 
26 Rows were returned by the SELECT statement. 

隨着 'N' 計劃

1 One or more rows were retrieved using index SATURN.PK_SGBSTDN . The index was scanned in ascending order.. 
2 The rows were sorted in order to be grouped. 
3 A view definition was processed, either from a stored view SYS.VW_SQ_1 or as defined by steps 2. 
4 Rows were retrieved using the unique index SATURN.PK_SGBSTDN . 
5 Rows from table SATURN.SGBSTDN were accessed using rowid got from an index. 
6 For each row retrieved by step 3, the operation in step 5 was performed to find a matching row. 
7 One or more rows were retrieved using index SATURN.SPRIDEN_PIDM_INDEX . The index was scanned in ascending order.. 
8 For each row retrieved by step 6, the operation in step 7 was performed to find a matching row. 
9 One or more rows were retrieved using index SATURN.SFRSTCR_KEY_INDEX3 . The index was scanned in ascending order.. 
10 For each row retrieved by step 8, the operation in step 9 was performed to find a matching row. 
11 Rows from table SATURN.SFRSTCR were accessed using rowid got from an index. 
12 For each row retrieved by step 10, the operation in step 11 was performed to find a matching row. 
13 One or more rows were retrieved using index SATURN.PK_SFRSTCR . The index was scanned in ascending order.. 
14 Rows from table SATURN.SFRSTCR were accessed using rowid got from an index. 
15 Rows were retrieved using the unique index SATURN.PK_SSBSECT . 
16 For each row retrieved by step 14, the operation in step 15 was performed to find a matching row. 
17 Rows from table SATURN.SSBSECT were accessed using rowid got from an index. 
18 For each row retrieved by step 16, the operation in step 17 was performed to find a matching row. 
19 SORT GROUP BY NOSORT 
20 For the rows returned by step 19, filter out rows depending on filter criteria. 
21 One or more rows were retrieved using index SATURN.PK_SFRSTCR . The index was scanned in ascending order.. 
22 Rows from table SATURN.SFRSTCR were accessed using rowid got from an index. 
23 SORT GROUP BY NOSORT 
24 WINDOW SORT 
25 The rows from step 24 were sorted to eliminate duplicate rows. 
26 Rows were returned by the SELECT statement. 
+0

哪兩種查詢計劃?統計信息是否最新? 'sgbstdn_resd_code'上有直方圖嗎?這個直方圖是否準確? –

+0

您是否檢查過每個查詢的執行計劃?發揮你的執行計劃的差異,你應該找出額外的時間來自哪裏。不同的(效率較低的)執行計劃可能是過時統計的產物 – GarethD

+0

與您的確切問題無關,但「不在(子問題)」很慢。您可以通過使用「not exists」或「in(選擇某個字段減去選擇您想要排除它的同一字段)」來加速該部分「 –

回答

-1

我會嘗試在SGBSTDN_RESD_CODE列添加索引