Skip to Main Content
  • Questions
  • Materialized view refresh time takes longer than table creation.

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Ana.

Asked: July 24, 2017 - 2:03 pm UTC

Last updated: July 26, 2017 - 3:46 am UTC

Version: 11G R2

Viewed 1000+ times

You Asked

Hi!
My database has 2 schemas. On SCHEMA_ONE I have a view that runs in 3 seconds to return 420.000 rows.
In SCHEMA_TWO I have a materialized view, that I always drop and recreate like this:

CREATE MATERIALIZED VIEW my_mat_view_name
REFRESH COMPLETE
AS
SELECT * FROM SCHEMA_ONE.my_view_name;


The script above needs 10:30 minutes to complete.
If I just create a table like below, it takes 1 second.
CREATE TABLE my_table_name
AS
SELECT * FROM SCHEMA_ONE.my_view_name


Why do I have suck a big difference between the 2 scripts? I would like to make the matview script to run as fast as the second script.

and Connor said...

Try something like this to trace the activity

--
-- my sample table (you would use your real table)
--
SQL> create table t as select d.* from dba_objects d,
  2    ( select 1 from dual connect by level <= 20 ) ;

Table created.

SQL>
SQL> exec dbms_monitor.session_trace_enable(waits=>true);

PL/SQL procedure successfully completed.

SQL>
SQL> CREATE MATERIALIZED VIEW my_mat_view_name
  2  REFRESH COMPLETE
  3  AS
  4  SELECT * FROM mcdonac.t;

Materialized view created.

SQL>
SQL> exec dbms_monitor.session_trace_disable;

PL/SQL procedure successfully completed.



In my trace file, I ultimately see the following:

PARSING IN CURSOR #2638142355840 len=69 dep=1 uid=107 oct=1 lid=107 tim=728811237493 hv=3922666197 ad='7ff958e77c50' sqlid='889f2u7nwy8qp'
CREATE TABLE "MCDONAC"."MY_MAT_VIEW_NAME" AS SELECT * FROM mcdonac.t
END OF STMT


so you can see that under the covers, we're pretty much doing the same operation anyway. But run the trace, then run tkprof on the trace file and see where you are losing the time.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

And without trace files?

Ana G, July 25, 2017 - 8:06 am UTC

Thanks for the reply, but I don't have access to DBMS_MONITOR or trace files. Any other ideas of what I could check?

Thanks!
Connor McDonald
July 26, 2017 - 3:46 am UTC

I'd request access. Every developer should be able either to:

a) have direct usage on those facilities, or
b) have a means via to request someone to use them on their behalf and get access to the output.

Feel free to quote me on that :-)

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.