ORACLE - 从物化视图创建视图

时间:2014-03-19 14:18:32

标签: regex oracle view ddl materialized-views

我需要在Oracle 11g的常规视图中更改大量的物化视图。 我的第一个方法:

1。)获取DDL

select dbms_metadata.get_ddl(replace(object_type, ' ', '_'), object_name, owner)
from all_objects
where owner = 'MY_SCHEMA'
and object_type = 'MATERIALIZED VIEW'
and object_name in (
'MAT_VIEW1'
,'MAT_VIEW2'
,'MAT_VIEW3'
,'MAT_VIEW4'
-- ..
,'MAT_VIEW100'
); 

每行包含相应MVIEW的DDL,例如:

  CREATE MATERIALIZED VIEW "MY_SCHEMA"."MVIEW1" ("ROW1", "ROW2", "ROW3")
  ORGANIZATION HEAP PCTFREE 0 PCTUSED 40 INITRANS 1 MAXTRANS 255 
  NOCOMPRESS NOLOGGING
  STORAGE(INITIAL 163840 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "MY_TABSPACE" 
  BUILD IMMEDIATE
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "MY_TABLESPACE" 
  REFRESH FORCE ON DEMAND
  WITH PRIMARY KEY USING DEFAULT LOCAL ROLLBACK SEGMENT
  USING ENFORCED CONSTRAINTS DISABLE QUERY REWRITE
  AS SELECT ROWA AS ROW1,
  ROWB AS ROW2,
  ROWC AS ROW3
  FROM MY_SCHEMA.TABLE1

2。)使用正则表达式摆脱其他技术配置。为方便起见,我使用RegexBuddy并复制粘贴文本。 Oracle并不像我注意到的那样支持Look Forward / Backward功能:

(?=ORGANIZATION\sHEAP).+?(QUERY\sREWRITE)

结果:

  CREATE MATERIALIZED VIEW "MY_SCHEMA"."MVIEW1" ("ROW1", "ROW2", "ROW3")
  AS SELECT ROWA AS ROW1,
  ROWB AS ROW2,
  ROWC AS ROW3
  FROM MY_SCHEMA.TABLE1

最后我删除了#34; MATERIALIZED"从每个DDL,现在我有视图的有效DDL。

与此同时,我得到了一个自发的想法,并尝试了

create view usual_view1 as
select * from mat_view1;
嗯,这也很有效,而且工作量很小。

所以我的问题:在根据DDL创建它的常用方法比较具体化视图的定义创建视图时,最终有什么不同吗?

1 个答案:

答案 0 :(得分:1)

以第二种方式创建视图时,使用:

create view usual_view1 as
select * from mat_view1;

...您的视图向您显示实体化视图所持有的数据,而不是基础表。因为那是refresh force on demand,你的“普通”视图将只显示物化视图所持有的陈旧数据,而不是基础表中数据的当前状态。

更重要的是,如果物化视图被删除 - 这似乎可能会替换它们 - 那么您的“正常”视图将变为无效。

create materialized view my_mview
refresh force on demand
as select * from dual;

materialized view MY_MVIEW created.

create view my_view as select * from my_mview;

view MY_VIEW created.

select * from my_view;

DUMMY
-----
X     

drop materialized view my_mview;

materialized view MY_MVIEW dropped.

select * from my_view;

SQL Error: ORA-04063: view "STACKOVERFLOW.MY_VIEW" has errors

select text from user_errors where name = 'MY_VIEW';

ORA-00942: table or view does not exist

显然dual仍然存在,my_mview它再也找不到了。视图不了解物化视图的基础表 - 它没有继承查询所使用的查询,它只显示它包含的内容。它们是非常不同的东西。


你可以通过dbms_metadata.set_transform_param()调用来消除DDL方法中的一些噪音,但据我所知,你无法完全清除它。您可以获得基础查询:

select query
from all_mviews where owner = 'STACKOVERFLOW'
and mview_name = 'MY_MVIEW';

QUERY                                                      
------------------------------------------------------------
select * from dual                                           

您可以操纵它以将create view ... as置于开头;虽然querylong列,但我认为您必须在PL / SQL块中执行此操作,例如:

begin
  for r in (
    select mview_name, query
    from all_mviews
    where owner = 'STACKOVERFLOW'
    and mview_name = 'MY_MVIEW'
  ) loop
    dbms_output.put_line('create or replace view '
       || replace(r.mview_name, 'MVIEW', 'VIEW')
       || ' as ' || r.query ||';');
  end loop;
end;
/

anonymous block completed
create or replace view MY_VIEW as select * from dual;