Oracle之物化視圖

Oracle之物化視圖數據庫

近期根據項目業務須要對oracle的物化視圖有所接觸,在網上搜尋關於這方面的資料,便於提升,整理內容以下:canvas

物化視圖是一種特殊的物理表,「物化」(Materialized)視圖是相對普通視圖而言的。普通視圖是虛擬表,應用的侷限性大,任何對視圖的查詢,Oracle都實際上轉換爲視圖SQL語句的查詢。這樣對總體查詢性能的提升,並無實質上的好處。
一、物化視圖的類型:ON DEMAND、ON COMMIToracle

    兩者的區別在於刷新方法的不一樣,ON DEMAND顧名思義,僅在該物化視圖「須要」被刷新了,才進行刷新(REFRESH),即更新物化視圖,以保證和基表數據的一致性;而ON COMMIT是說,一旦基表有了COMMIT,即事務提交,則馬上刷新,馬上更新物化視圖,使得數據和基表一致。
二、ON DEMAND物化視圖    ide

物化視圖的建立自己是很複雜和須要優化參數設置的,特別是針 對大型生產數據庫系統而言。但Oracle容許以這種最簡單的,相似於普通視圖的方式來作,因此不可避免的會涉及到默認值問題。也就是說Oracle給物 化視圖的重要定義參數的默認值處理是咱們須要特別注意的。    工具

物化視圖的特色:性能

     (1) 物化視圖在某種意義上說就是一個物理表(並且不只僅是一個物理表),這經過其能夠被user_tables查詢出來,而獲得佐證;優化

    (2) 物化視圖也是一種段(segment),因此其有本身的物理存儲屬性;spa

     (3) 物化視圖會佔用數據庫磁盤空間,這點從user_segment的查詢結果,能夠獲得佐證;日誌

    建立語句:create materialized view mv_name as select * from table_name    默認狀況下,若是沒指定刷新方法和刷新模式,則Oracle默認爲FORCE和DEMAND。
    物化視圖的數據怎麼隨着基表而更新?    Oracle提供了兩種方式,手工刷新和自動刷新,默認爲手工刷新。也就是說,經過咱們手工的執行某個Oracle提供的系統級存儲過程或包,來保證物化 視圖與基表數據一致性。這是最基本的刷新辦法了。自動刷新,其實也就是Oracle會創建一個job,經過這個job來調用相同的存儲過程或包,加以實 現。code

       ON DEMAND物化視圖的特性及其和ON COMMIT物化視圖的區別,即前者不刷新(手工或自動)就不更新物化視圖,然後者不刷新也會更新物化視圖,——只要基表發生了COMMIT。

    建立定時刷新的物化視圖:create materialized view mv_name refresh force on demand start with sysdate next sysdate+1 (指定物化視圖天天刷新一次)

     上述建立的物化視圖天天刷新,可是沒有指定刷新時間,若是要指定刷新時間(好比天天晚上10:00定時刷新一次):create materialized view mv_name refresh force on demand start with sysdate next to_date( concat( to_char( sysdate+1,'dd-mm-yyyy'),' 22:00:00'),'dd-mm-yyyy hh24:mi:ss')
三、ON COMMIT物化視圖

     ON COMMIT物化視圖的建立,和上面建立ON DEMAND的物化視圖區別不大。由於ON DEMAND是默認的,因此ON COMMIT物化視圖,須要再增長個參數便可。   須要注意的是,沒法在定義時僅指定ON COMMIT,還得附帶個參數才行。

     建立ON COMMIT物化視圖:create materialized view mv_name refresh force on commit as select * from table_name    備註:實際建立過程當中,基表須要有主鍵約束,不然會報錯(ORA-12014)

四、物化視圖的刷新

     刷新(Refresh):指當基表發生了DML操做後,物化視圖什麼時候採用哪一種方式和基表進行同步。刷新的模式有兩種:ON DEMAND和ON COMMIT。(如上所述)

     刷新的方法有四種:FAST、COMPLETE、FORCE和NEVER。FAST刷新採用增量刷新,只刷新自上次刷新之後進行的修改。COMPLETE 刷新對整個物化視圖進行徹底的刷新。若是選擇FORCE方式,則Oracle在刷新時會去判斷是否能夠進行快速刷新,若是能夠則採用FAST方式,不然採 用COMPLETE的方式。NEVER指物化視圖不進行任何刷新。

      對於已經建立好的物化視圖,能夠修改其刷新方式,好比把物化視圖mv_name的刷新方式修改成天天晚上10點刷新一次:alter materialized view mv_name refresh force on demand start with sysdate next to_date(concat(to_char(sysdate+1,'dd-mm-yyyy'),' 22:00:00'),'dd-mm-yyyy hh24:mi:ss')   
五、物化視圖具備表同樣的特徵,因此能夠像對錶同樣,咱們能夠爲它建立索引,建立方法和對錶同樣。
六、物化視圖的刪除:

      雖然物化視圖是和表一塊兒管理的,可是在常用的PLSQL工具中,並不能用刪除表的方式來刪除(在表上右鍵選擇‘drop’並不能刪除物化視圖),可使用語句來實現:drop materialized view mv_name 

物化視圖建立參數

 (1)BUILD BUILD IMMEDIATE 是在建立物化視圖的時候就生成數據 BUILD DEFERRED 則在建立時不生成數據,之後根據須要再生成數據。 默認爲BUILD IMMEDIATE。
(2)REFRESH FAST 增量刷新用物化視圖日誌,來發送主表已經修改的數據行到物化視圖中。 COMPLETE 徹底刷新從新生成整個視圖,若是請求徹底刷新,oracle會完成徹底刷新即便增量刷新可用。 FORCE 若是增量刷新可用Oracle將完成增量刷新,不然將完成徹底刷新,若是不指定刷新方法(FAST, COMPLETE, or FORCE)。 默認選項是Force。
(3)ON ON DEMAND 指物化視圖在用戶須要的時候進行刷新。 ON COMMIT 指出物化視圖在對基表的DML操做提交的同時進行刷新。 默認是ON DEMAND.
(4)START WITH 通知數據庫完成從主表到本地表第一次複製的時間。
(5)NEXT 說明了刷新的時間間隔 根據下一次刷新的時間=上一次執行完成的時間+時間間隔。 爲了保證在用戶須要的時間點刷新,通常使用TRUNC()命令對時間取整到天數,而後加上時間。

具體示例代碼以下所示:

複製代碼

/* Formatted on 2012/3/28 11:26:08 (QP5 v5.149.1003.31008) */
--刪除日誌
TRUNCATE TABLE mlog$_fe_fee;
DROP MATERIALIZED VIEW LOG ON fe_fee;
TRUNCATE TABLE mlog$_fe_order;
DROP MATERIALIZED VIEW LOG ON fe_order;
TRUNCATE TABLE mlog$_fe_job;
DROP MATERIALIZED VIEW LOG ON fe_job;
TRUNCATE TABLE mlog$_fi_acc_bill;
DROP MATERIALIZED VIEW LOG ON fi_acc_bill;
TRUNCATE TABLE mlog$_fi_acc_fee;
DROP MATERIALIZED VIEW LOG ON fi_acc_fee;
TRUNCATE TABLE mlog$_fe_fee_age;
DROP MATERIALIZED VIEW LOG ON fe_fee_age;

--建立基表日誌
CREATE MATERIALIZED VIEW LOG ON fe_fee WITH ROWID, SEQUENCE(job_id, order_id) INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON fe_order WITH ROWID, SEQUENCE( order_id)INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON fe_job WITH ROWID ,SEQUENCE(job_id)INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON fi_acc_bill WITH ROWID, SEQUENCE(bill_id) INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON fi_acc_fee WITH ROWID, SEQUENCE(fee_id) INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW LOG ON fe_fee_age WITH ROWID, SEQUENCE(job_id, order_id) INCLUDING NEW VALUES;

--建立物化視圖
DROP MATERIALIZED VIEW mv_job_fee;


CREATE MATERIALIZED VIEW mv_job_fee
BUILD IMMEDIATE
REFRESH FAST
ON DEMAND
START WITH SYSDATE
NEXT SYSDATE + 5/(60*24)
AS
SELECT f.ROWID fi, j.ROWID ji, o.ROWID oi, b.ROWID bi, c.ROWID ci, f.fee_id,
       f.job_id, f.order_id, f.fee_type, f.fee_code, f.unit_price, f.quantity,
       f.currency, f.fx_rate, f.cust_id, f.invoice_num, f.is_confirm,
       f.blunt_flag, f.verify_balance, f.is_agreement, f.fiscal_period,
       f.attribute, f.continue, f.remark, f.security, f.create_by,
       f.create_time, f.pay_type, f.sharing_type, f.bill_id, f.direction,
       f.profit_loses, f.unit, f.relation_cust, f.amount, f.fx_amout,
       f.modified_by, f.modified_date, f.proportion, f.job_period,
       o.quantity ord_quantity, o.gross_weight ord_gross_weight,
       o.volume ord_volume, o.charge_weight ord_charge_weight,
       o.custom_num ord_custom_num, o.pay_type ord_pay_type,
       o.pay_type2 ord_pay_type2, o.teu ord_teu,
       o.cust_service ord_cust_service, o.oper ord_oper, o.bill ord_bill,
       o.sales ord_sales, o.cust_id ord_cust_id, o.bill_no  bill_no , 1  AS  ord_canvassing,1  AS ord_agent_type,
       j.dept_id job_dept_id,
       j.job_type job_job_type, j.firm job_firm, j.way_bill job_way_bill,
       j.loading job_loading, j.discharging job_discharging, j.etd job_etd,
       j.eta job_eta, j.flight_num job_flight_num, j.provider job_provider,
       j.carrier job_carrier, j.voyage job_voyage, j.quantity job_quantity,
       j.gross_weight job_gross_weight, j.volume job_volume,
       j.charge_weight job_charge_weight, j.teu job_teu,
       j.fee_lock job_fee_lock, j.lock_time job_lock_time,
       j.auditor job_auditor, j.archiveno job_archiveno,
       j.archived_by job_archived_by, j.archived_time job_archived_time,
       j.oversea_agent job_oversea_agent, j.container_info job_container_info,
       j.container_num job_container_num, j.proj_id job_proj_id,
       j.route  job_route,
       b.book_date bill_book_date, b.commit_flag bill_commit_flag,
       b.pay_period bill_pay_period, b.invoice_rise, c.confirm_amount,
       c.confirm_time
  FROM fe_fee f, fe_order o, fe_job j, fi_acc_bill b, fi_acc_fee c
 WHERE     f.job_id = j.job_id(+)
       AND f.order_id = o.order_id(+)
       AND f.bill_id = b.bill_id(+)
       AND f.fee_id = c.fee_id(+);



DROP MATERIALIZED VIEW mv_order_cargo;

CREATE MATERIALIZED VIEW mv_order_cargo
BUILD IMMEDIATE
REFRESH FAST
ON DEMAND
START WITH SYSDATE
NEXT SYSDATE + 10/(60*24)
AS
SELECT j.ROWID ji, o.ROWID oi, o.order_id, o.job_type, o.cust_id, o.dept_id,
       o.firm, o.job_id, o.quantity, o.gross_weight, o.volume,
       o.charge_weight, o.custom_num, j.loading, j.discharging, o.pay_type,
       o.pay_type2, o.teu, o.cust_service, o.oper, o.bill, o.sales,
       o.booking_type, o.route, o.assign_agent, j.way_bill, j.etd, j.eta,
       j.flight_num, j.provider, j.carrier, j.voyage, j.quantity job_quantity,
       j.gross_weight job_gross_weight, j.volume job_volume,
       j.charge_weight job_charge_weight, j.teu job_teu, j.job_period,
       j.oversea_agent, j.container_info, j.container_num
  FROM fe_order o, fe_job j
 WHERE o.job_id = j.job_id(+);

DROP MATERIALIZED VIEW mv_fee_age;

CREATE MATERIALIZED VIEW mv_fee_age
BUILD IMMEDIATE
REFRESH FAST
ON DEMAND
START WITH SYSDATE
NEXT SYSDATE + 5/(60*24)
AS
SELECT a.ROWID ai, f.ROWID fi, j.ROWID ji, o.ROWID oi, a.fee_id,
       a.job_id, a.order_id, f.fee_type, f.fee_code, f.unit_price, f.quantity,
       f.currency, f.fx_rate, f.cust_id, f.invoice_num, f.is_confirm,
       f.blunt_flag, f.verify_balance, f.is_agreement, f.fiscal_period,
       f.attribute, f.continue, f.remark, f.security, f.create_by,
       f.create_time, f.pay_type, f.sharing_type, f.bill_id, f.direction,
       f.profit_loses, f.unit, f.relation_cust, f.amount, f.fx_amout,
       f.modified_by, f.modified_date, f.proportion, f.job_period,
       o.quantity ord_quantity, o.gross_weight ord_gross_weight,
       o.volume ord_volume, o.charge_weight ord_charge_weight,
       o.custom_num ord_custom_num, o.pay_type ord_pay_type,
       o.pay_type2 ord_pay_type2, o.teu ord_teu,
       o.cust_service ord_cust_service, o.oper ord_oper, o.bill ord_bill,
       o.sales ord_sales, o.cust_id ord_cust_id, j.dept_id job_dept_id,
       j.job_type job_job_type, j.firm job_firm, j.way_bill job_way_bill,
       j.loading job_loading, j.discharging job_discharging, j.etd job_etd,
       j.eta job_eta, j.flight_num job_flight_num, j.provider job_provider,
       j.carrier job_carrier, j.voyage job_voyage, j.quantity job_quantity,
       j.gross_weight job_gross_weight, j.volume job_volume,
       j.charge_weight job_charge_weight, j.teu job_teu,
       j.fee_lock job_fee_lock, j.lock_time job_lock_time,
       j.auditor job_auditor, j.archiveno job_archiveno,
       j.archived_by job_archived_by, j.archived_time job_archived_time,
       j.oversea_agent job_oversea_agent, j.container_info job_container_info,
       j.container_num job_container_num, j.proj_id job_proj_id
  FROM fe_fee_age a, fe_fee f, fe_order o, fe_job j
 WHERE a.fee_id = f.fee_id(+)
 AND a.job_id = j.job_id(+)
 AND a.order_id = o.order_id(+);
相關文章
相關標籤/搜索