物化视图及job定时更新简例

--创建测试表
create table test_dm_rep_m_gw_user
as select * from dm_rep_m_gw_use t where t.month_no<'201304';
--核查确认,当前测试表最大账期为201303账期
SELECT MAX(MONTH_NO) FROM TEST_DM_REP_M_GW_USER;
--创建基于主表的物化视图日志
create materialized view log on test_dm_rep_m_gw_user;
--由于测试表无主键,故在创建基于主表的视图日志时,报如下错误:
ora-12014:table 'test_dm_rep_m_gw_user' does not contain a primary key constraint
--给测试表创建主键,主键创建完毕后按照上面语句创建视图日志
select * from test_dm_rep_m_gw_user;
alter table test_dm_rep_m_gw_user add constraints pk_test_dm_rep_m_gw_user primary key(month_no,prov_id,city_no,kpi_code);
--创建物化视图(对于增量刷新选项,如果在子查询中存在分析函数,则物化视图不起作用)
create materialized view my_test_dm_rep_m_gw_user
refresh fast
start with sysdate next sysdate+2/60 with primary key
as select * from test_dm_rep_m_gw_user;
drop materialized view my_test_dm_rep_m_gw_user;
--确认下物化视图及基表里面的数据记录
select COUNT(*) from dm_rep_m_gw_use t where t.month_no<'201304';
SELECT count(*) from my_test_dm_rep_m_gw_user;
SELECT count(*) from test_dm_rep_m_gw_user;
--向基表中新增加数据记录
insert into test_dm_rep_m_gw_user select * from dm_rep_m_gw_use t where t.month_no='201304';
commit;
--若干时间后,验证物化视图里面数据是否更新

 

--job测试
--创建一张测试表
create table job_test_01
(
id number(12),
c_date date
)
--创建一个sequence
create sequence g_seq
minvalue 1
maxvalue 999999
start with 22
increment by 1
cache 20;
--创建一个存储过程
create or replace procedure prc_g_test is
begin
insert into job_test_01(g_seq.nextval,sysdate);
end prc_t_test;
--创建job
declare  
job number;     
begin
sys.dbms_job.submit(job,'prc_g_test;',sysdate,'sysdate+1/1440');
end;
--查看job:4023
select * from user_jobs;
--运行job
begin
dbms_job.run(4023);
end;
--查看测试表里面数据
select * from job_test_01;

 

你可能感兴趣的:(物化视图及job定时更新简例)