1

I have Oracle 21c XE using some Materialized Views (MV) for my application. The most used materialized view, MajorView, is based on other secundary MVs A,C,D , i.e. the MajorView is nested MV since it's query references others MVs A,C and D.

In fact these secundaries MVs are also nested MVs. Then the general schema of MVs referencing is as follows :

  1. MV MajorView references A,C D
  2. MV C references A and B
  3. MV D references C
  4. MV E references MajorView

All these views are created fastly, in a very acceptable time, a matter of few seconds.

MajorView has about 400k rows, a query Q1 run over it to recover the few essential rows for the application tooks about 0,4 seconds in normal conditions. That is fine.

The problem appears after a Refresh in all MVs using this statement

Begin
   DBMS_MVIEW.refresh ('A,B,C,D,MajorView,E','??????',NULL,TRUE,FALSE,1,0,0,TRUE,TRUE);
End;

Although this refresh runs with no problem and also in an acceptable tie, after it the access to the MajorView becomes extremely slow when compared with access just after its creation.

The same query Q1 tooks now about 40 seconds to run while before it run on 0,4 seconds , it's about a 100 time slower !!! It's unacceptable for the application.

Checking the MVs status after the Refresh with the query :

SELECT MVIEW_NAME, STALENESS, LAST_REFRESH_TYPE,COMPILE_STATE, 
       to_char(LAST_REFRESH_END_TIME, 'DD/MM/YY HH24:MI:SS') as LAST_REFRESH_END_TIME,
       to_char(LAST_REFRESH_DATE, 'DD/MM/YY HH24:MI:SS') as LAST_REFRESH_DATE,      
       to_number((LAST_REFRESH_END_TIME - LAST_REFRESH_DATE) * 1440 * 60) AS "Time (sec)"
FROM USER_MVIEWS 

all STALENESS were "FRESH", ALL COMPILE_STATE were "VALID" .

Troubleshooting this problem I ended up finding that if I Drop and Create MajorView again and immediately run Q1 query, the speed become normal, i.e. fast again only 0,4 seconds! Unfortunately this acceptable speed lasts until the next refresh !!

KEY QUESTION of the PROBLEM : Why MajorView Q1 query become slow after a Refresh ?

Other related questions :

  1. Is this refresh method and command sintax right ? Is there any hierarchy of refresh ?
  2. Is there any problem to reference a MV on a query to build another MV ?
  3. Can I use Materialized Views in Oracle 21c XE ? I'm using it , but the documentation does not mention it has Materialized Views !!

Thanks for your help !

---============================================================= Additional information requested by contributors
---============================================================= 1) Explain Plan - when facing the problem "slow query"

Query duration time : 59 sec

Plan hash value: 1025316596

-----------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                          | Name                            | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                   |                                 |      1 |        |     55 |00:00:18.76 |    5543 |       |       |          |
|*  1 |  FILTER                            |                                 |      1 |        |     55 |00:00:18.76 |    5543 |       |       |          |
|   2 |   HASH GROUP BY                    |                                 |      1 |     20 |  28003 |00:00:18.78 |    5543 |  7679K|  2050K| 7329K (0)|
|*  3 |    HASH JOIN                       |                                 |      1 |  22988 |     18M|00:00:18.69 |    5543 |  7346K|  2119K| 7790K (0)|
|*  4 |     HASH JOIN                      |                                 |      1 |   1050 |  28003 |00:00:00.07 |    3834 |   838K|   838K| 1358K (0)|
|*  5 |      HASH JOIN                     |                                 |      1 |     25 |     55 |00:00:00.01 |      56 |   924K|   924K| 1367K (0)|
|*  6 |       HASH JOIN                    |                                 |      1 |     25 |     55 |00:00:00.01 |      23 |  1236K|  1236K| 1180K (0)|
|   7 |        NESTED LOOPS OUTER          |                                 |      1 |      9 |     13 |00:00:00.01 |       8 |       |       |          |
|*  8 |         TABLE ACCESS FULL          | CARTEIRA                        |      1 |      9 |     13 |00:00:00.01 |       6 |       |       |          |
|   9 |         TABLE ACCESS BY INDEX ROWID| PLGITEMS                        |     13 |      1 |      1 |00:00:00.01 |       2 |       |       |          |
|* 10 |          INDEX UNIQUE SCAN         | IDX_PLGITEMS_PLGITEM_ID         |     13 |      1 |      1 |00:00:00.01 |       1 |       |       |          |
|* 11 |        TABLE ACCESS FULL           | CARTEIRAATIVO                   |      1 |    820 |    820 |00:00:00.01 |      15 |       |       |          |
|  12 |       TABLE ACCESS FULL            | ATIVO                           |      1 |   2356 |   2356 |00:00:00.01 |      33 |       |       |          |
|* 13 |      MAT_VIEW ACCESS FULL          | MVW_CARTEIRAATIVOHIST           |      1 |     42 |    268K|00:00:00.23 |    3778 |       |       |          |
|* 14 |     INDEX FAST FULL SCAN           | MVW_CARTEIRAATIVOHIST_IDX_GERAL |      1 |    268K|    268K|00:00:00.22 |    1709 |       |       |          |
-----------------------------------------------------------------------------------------------------------------------------------------------------------

2) Explain Plan after Re-create the MV MajorView (MajorView is MVW_CARTEIRAATIVOHIST)

Query duration time : 0,870sec

Plan hash value: 4205125506
 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                    | Name                              | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                             |                                   |      1 |        |     55 |00:00:00.15 |    5257 |       |       |          |
|*  1 |  HASH JOIN                                   |                                   |      1 |      1 |     55 |00:00:00.15 |    5257 |   873K|   873K| 1362K (0)|
|*  2 |   HASH JOIN                                  |                                   |      1 |      1 |     55 |00:00:00.10 |    5224 |   951K|   951K| 1293K (0)|
|   3 |    NESTED LOOPS                              |                                   |      1 |     17 |     55 |00:00:00.04 |    1665 |       |       |          |
|*  4 |     HASH JOIN                                |                                   |      1 |     25 |     55 |00:00:00.01 |      23 |  1572K|  1572K| 1159K (0)|
|   5 |      NESTED LOOPS OUTER                      |                                   |      1 |      9 |     13 |00:00:00.01 |       8 |       |       |          |
|*  6 |       TABLE ACCESS FULL                      | CARTEIRA                          |      1 |      9 |     13 |00:00:00.01 |       6 |       |       |          |
|   7 |       TABLE ACCESS BY INDEX ROWID            | PLGITEMS                          |     13 |      1 |      1 |00:00:00.01 |       2 |       |       |          |
|*  8 |        INDEX UNIQUE SCAN                     | IDX_PLGITEMS_PLGITEM_ID           |     13 |      1 |      1 |00:00:00.01 |       1 |       |       |          |
|*  9 |      TABLE ACCESS FULL                       | CARTEIRAATIVO                     |      1 |    820 |    820 |00:00:00.01 |      15 |       |       |          |
|  10 |     VIEW PUSHED PREDICATE                    | VW_SQ_1                           |     55 |      1 |     55 |00:00:00.06 |    1642 |       |       |          |
|* 11 |      FILTER                                  |                                   |     55 |        |     55 |00:00:00.06 |    1642 |       |       |          |
|  12 |       SORT AGGREGATE                         |                                   |     55 |      1 |     55 |00:00:00.06 |    1642 |       |       |          |
|* 13 |        MAT_VIEW ACCESS BY INDEX ROWID BATCHED| MVW_CARTEIRAATIVOHIST             |     55 |      1 |  28003 |00:00:00.09 |    1642 |       |       |          |
|  14 |         BITMAP CONVERSION TO ROWIDS          |                                   |     55 |        |  33267 |00:00:00.07 |     878 |       |       |          |
|  15 |          BITMAP AND                          |                                   |     55 |        |     55 |00:00:00.05 |     878 |       |       |          |
|  16 |           BITMAP CONVERSION FROM ROWIDS      |                                   |     55 |        |     55 |00:00:00.03 |     487 |       |       |          |
|* 17 |            INDEX RANGE SCAN                  | MVW_CARTEIRAATIVOHIST_IDX_CART_ID |     55 |   1083 |    182K|00:00:00.14 |     487 |       |       |          |
|  18 |           BITMAP CONVERSION FROM ROWIDS      |                                   |     55 |        |     55 |00:00:00.02 |     391 |       |       |          |
|* 19 |            INDEX RANGE SCAN                  | MVW_CARTEIRAATIVOHIST_IDX_ATV     |     55 |   1083 |    136K|00:00:00.10 |     391 |       |       |          |
|* 20 |    MAT_VIEW ACCESS FULL                      | MVW_CARTEIRAATIVOHIST             |      1 |      1 |    268K|00:00:00.22 |    3559 |       |       |          |
|  21 |   TABLE ACCESS FULL                          | ATIVO                             |      1 |      1 |   2356 |00:00:00.01 |      33 |       |       |          |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Well ... I don't understand why the 2nd Explain Plain is so different from the first case , once the Materialized view does not suffer any change is his creation definition (sql code) !!

EDIT ==> Finally, for any reason, I don't know how, query became slow again then I could run Explain Plan and collect A&E rows for both cases now.

JRG
  • 513
  • 9
  • 23
  • 1
    Something's different about the plans before & after the refresh. Please share the [execution plans](https://blogs.oracle.com/optimizer/post/how-to-generate-a-useful-sql-execution-plan) for fast and slow versions of the query. Ensure this includes the estimated and actual rows for each step. – Chris Saxon Jul 03 '23 at 11:49
  • Make sure you test the slow query *three* times after refreshing. This problem might be due to dynamic reoptimization, where the optimizer learns from its mistakes and automatically changes the query based on mistakes from the first or second run. – Jon Heller Jul 05 '23 at 01:00
  • @JonHeller, thks for the reply. Yeah I've done that, after refreshing I run queries several times, then refreshed again and run more a couple of times, same behaviour = slow ! – JRG Jul 05 '23 at 03:15
  • @ChrisSaxon, thanks for your attention. I'm providing the Explain Plan right now. In few minutes I'll upload here. – JRG Jul 05 '23 at 03:16
  • 1
    Please add A&E rows to all the plans. – Chris Saxon Jul 05 '23 at 15:18
  • @ChrisSaxon, for the first query "the slow one" I forgot to add A&E rows, sorry. Then I rebuild the MV and it became "fast" and I uploaded execution plan with A&E rows . In order to upload an execution plan with A&E rows for the first query I have make it slow again, BUT I don't know exactly which turn it slow. I'm trying all procedures that usually run in my application, but it is not working. The only sure is sometime, somehow it become slow. I'm keeping trying. Any tip ? – JRG Jul 05 '23 at 20:16
  • @ChrisSaxon, there you go ! After several runs, refresh, startup/shutdown finally the MajorView became slow again, so that I could get both Plans with A&E rows. Waiting for your evaluation ! Thks. – JRG Jul 06 '23 at 03:10
  • @JRG All the plans show significant cardinality differences whenever MVW_CARTEIRAATIVOHIST is used. For example, estimating 1 row when the operation actually returns 268K. Check when the stats were last gathered (DBA_TABLES.LAST_ANALYZED) and try re-gathering the stats with `begin dbms_stats.gather_table_stats(user, 'MVW_CARTEIRAATIVOHIST'); end;` Also, what is the predicate on MVW_CARTEIRAATIVOHIST? Maybe it's something weird that Oracle has a hard time estimating. – Jon Heller Jul 07 '23 at 03:56
  • 1
    Did the number of rows change significantly in any of the MV's after the refresh? Can you try to gather statistics after the refresh and then check the performance of the query? – Martin Schapendonk Jul 12 '23 at 12:58

0 Answers0