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

数据库是甲骨文。有谁知道为什么会发生这种行为?

编辑:解释计划似乎是相同的。

使用“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.
4

1 回答 1

-1

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

于 2013-08-23T14:23:54.740 回答