Sunday, May 13, 2018

OPTIMIZER MODE when first_rows leads to bad plans

Beware when setting optimizer_mode for application session, esp. if this session is used to execute different statements. 
I encounter this issue, which was made more elusive because bind variables are not used in sql text. So, no quick fix by sql profile or baseline. I am going to post only the plan two plan with timing information.
select OPTIMIZER,sql_id from DBA_HIST_SQL_PLAN where OPTIMIZER='FIRST_ROWS'; -- will indicate which sql_id using first_rows .


SYS@APPS_DB1> select SQL_ID,PLAN_HASH_VALUE,ELAPSED_TIME,CPU_TIME,PHYSICAL_READ_REQUESTS,DISK_READS from v$sql where PARSING_SCHEMA_NAME='APPS_SCHEMA' and sql_id in('9705zux4zfg3m','3zkfwn4c0dmfm');
SQL_ID       PLAN_HASH_VALUE ELAPSED_TIME   CPU_TIME PHYSICAL_READ_REQUESTS DISK_READS
------------- --------------- ------------ ---------- ---------------------- ----------
3zkfwn490dmfm    1076852662      67753 26996    7       7  ALL_ROWS
9705z9x4zfg3m    2894949837 290494374   45894022       258319 344209  FIRST_ROWS

FIRST_ROWS  -
-----------------------------------------------------------------------------------------------------------------------------
| Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
-----------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |     |     |     | 68 (100)|     |     |     |
|   1 |  SORT ORDER BY        |     |   1 | 280 | 68   (2)| 00:00:01 |     |     |
|*  2 |   HASH JOIN        |     |   1 | 280 | 67   (0)| 00:00:01 |     |     |
|   3 |    VIEW        | VW_JF_SET$161879F6 | 22 |  1408 |   9   (0)| 00:00:01 |     |     |
|   4 |     UNION-ALL        |     |     |     | |     |     |     |
|*  5 |      TABLE ACCESS BY GLOBAL INDEX ROWID| MOVEMENTS     | 11 | 473 |   5   (0)| 00:00:01 | ROWID | ROWID |
|   6 |       INDEX FULL SCAN        | MV_PK     | 18M|     |   3   (0)| 00:00:01 |     |     |
|*  7 |      TABLE ACCESS BY GLOBAL INDEX ROWID| MOVEMENTS     | 11 | 473 |   4   (0)| 00:00:01 | ROWID | ROWID |
|   8 |       INDEX FULL SCAN        | MV_PK     | 18M|     |   3   (0)| 00:00:01 |     |     |
|   9 |    INLIST ITERATOR        |     |     |     | |     |     |     |
|  10 |     TABLE ACCESS BY GLOBAL INDEX ROWID | MOV_MSG     | 38 |  8208 | 58   (0)| 00:00:01 | ROWID | ROWID |
|* 11 |      INDEX RANGE SCAN        | M_M_MRN_IDX     | 38 |     | 23   (0)| 00:00:01 |     |     |
-----------------------------------------------------------------------------------------------------------------------------

ALL_ROWS  -
----------------------------------------------------------------------------------------------------------------------
| Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
----------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |      |      |      | 233 (100)|      |      |      |
|   1 |  SORT ORDER BY        |      |   67 | 17353 | 233   (1)| 00:00:03 |      |      |
|   2 |   NESTED LOOPS        |      |   67 | 17353 | 232   (0)| 00:00:03 |      |      |
|   3 |    NESTED LOOPS        |      |   67 | 17353 | 232   (0)| 00:00:03 |      |      |
|   4 |     INLIST ITERATOR        |      |      |      |   |      |      |      |
|   5 |      TABLE ACCESS BY GLOBAL INDEX ROWID| MOV_MSG     |   67 | 14472 |   98   (0)| 00:00:02 | ROWID | ROWID |
|*  6 |       INDEX RANGE SCAN        | M_M_MRN_IDX |   67 |      |   38   (0)| 00:00:01 |      |      |
|*  7 |     INDEX UNIQUE SCAN        | MV_PK      |    1 |      |    1   (0)| 00:00:01 |      |      |
|   8 |    TABLE ACCESS BY GLOBAL INDEX ROWID  | MOVEMENTS   |    1 |   43 |    2   (0)| 00:00:01 | ROWID | ROWID |
----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment