psql物化視圖自動更新

更新物化視圖示例sql

CREATE TABLE model.test (
id SERIAL PRIMARY KEY,
name VARCHAR(60)
);

. 建立物化視圖oracle

CREATE MATERIALIZED VIEW model.test_materialized_view AS
SELECT * FROM model.test;

. 建立惟一索引,由於 CONCURRENTLY 同步更新須要惟一索引函數

CREATE UNIQUE INDEX test_materialized_view_index ON model.test_materialized_view(id);

. 同步更新,可是須要觸發,不能向 oracle 那種能夠自動觸發更新.net

REFRESH MATERIALIZED VIEW CONCURRENTLY model.test_materialized_view;

. 建立函數執行同步更新和觸發器執行函數blog

create or replace function update_test_materialized_view() returns trigger as $$
declare
begin
REFRESH MATERIALIZED VIEW CONCURRENTLY model.test_materialized_view;
return new;
end;
$$ language plpgsql;
CREATE TRIGGER update_test_materialized_view AFTER UPDATE ON model.test FOR EACH STATEMENT EXECUTE PROCEDURE update_test_materialized_view();
CREATE TRIGGER update_test_materialized_view AFTER UPDATE ON model.test FOR EACH STATEMENT EXECUTE PROCEDURE update_test_materialized_view();
CREATE TRIGGER update_test_materialized_view AFTER UPDATE ON model.test FOR EACH STATEMENT EXECUTE PROCEDURE update_test_materialized_view();

  


---------------------
做者:Resemble_
來源:CSDN
原文:https://blog.csdn.net/qq_27657429/article/details/76643294
版權聲明:本文爲博主原創文章,轉載請附上博文連接!索引

相關文章
相關標籤/搜索