单一更改查询,大大增加了执行时间

时间:2013-08-23 14:18:37

标签: sql oracle sql-optimization

我有一个如下所示的查询:

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中的有效值,如果有的话,'N'值应该小于'R'值。

数据库是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.

1 个答案:

答案 0 :(得分:-1)

我会尝试在SGBSTDN_RESD_CODE列上添加索引