2013-08-23 38 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列添加索引