Syntax for creating materialized views
1. Materialized view concept: Store the data queried in the view into a table.
2. Advantages: The query speed is faster than the view.
3. Disadvantages: It takes up resources and needs to update data from time to time.
4. Create materialized views
CREATE METERIALIZED VIEW view_name
[BUILD IMMEDIATE | BUILD DEFERRED ]
REFRESH [FAST|COMPLETE|FORCE]
[
ON [COMMIT |DEMAND ] | START WITH (start_time) NEXT
(next_time)
]
AS
Subquery
- METERIALIZED: materialized.
- BUILD IMMEDIATE: Data is generated when the materialized view is created.
- BUILD DEFERRED: No data is generated when creating a materialized view. Default is BUILD IMMEDIATE
- FAST: fast refresh, incremental refresh.
- COMPLETE: Update the base table, delete the materialized view data, and regenerate the data.
- FORCE: Automatic selection. If it can be refreshed incrementally, it will be refreshed incrementally. If it cannot be refreshed incrementally, it will be refreshed completely.
Default is FORCE - ON COMMIT: When the base table changes, the materialized view is refreshed.
- ON DEMAND: Manual refresh Default: ON DEMAND
Create a manually refreshed materialized view
The default setting for creating a materialized view is manual refresh ON DEMAND. If the data in the base table changes, the tables in the materialized view will not change and need to be refreshed manually.
1、--需求:查询地址 ID,地址名称和所属区域名称
create materialized view view_address1 as
select ad.id,ad.name,ar.name arname
from t_address ad,t_area ar
where ad.areaid=ar.id
--删除物化视图
drop materialized view view_address1;
2、--物化视图一般以mv开头
create materialized view mv_address1 as
select ad.id,ad.name,ar.name arname
from t_address ad,t_area ar
where ad.areaid=ar.id
--查询物化视图
select * from mv_address1;
3、--向基表插入数据
insert into t_address values(8,'西三旗',2,2);
commit;
--查询t_address
select * from t_address;
4、--执行刷新语句
begin
DBMS_MVIEW.refresh('MV_ADDRESS1','C');
end;
5、--向基表插入数据
insert into t_address values(9,'西四旗',2,2);
commit;
6、--命令窗口更新刷新数据(了解)
EXEC DBMS_MVIEW.refresh('MV_ADDRESS1','C');