copyright http://cupegraf.com/html
視圖是指數據庫只存儲定義該視圖的查詢語句(內容是查詢時產生),而物化視圖是一個其查詢語句查詢後的內容並存儲的視圖(內容是建立物化視圖刷新視圖時產生,數據可修改。獨立)。由於物化視圖是視圖的一個物化表結構,可是裏面的數據是建立時刷新查詢到額數據,當原數據更新修改時若是物化視圖的表沒有更新會形成數據的不一致。從9.4開始增長增量刷新,9.5的版本中支持物化視圖也支持索引,修改表空間,指定用戶訪問權限python
postgresql9.5物化視圖測試:git
search package: sudo apt-cache search dtrace-*github
dtrac && readline install :sudo apt-get install systemtap-sdt-dev libssl-dev libpam-dev libxml2-dev libxslt-dev libtcl8.4 libperl-dev python-devsql
./configure --prefix=/home/pg5/pgsql9.5-devel --with-port=5433 --with-perl --without-tcl --with-python --with-openssl --with-pam --without-ldap --with-libxml --with-libxslt --enable-thread-safety --with-blocksize=32 --enable-dtrace --enable-debugshell
make 數據庫
sudo make installbash
cd postgresql源碼目錄/contriboracle
make app
sudo make install(後面要用到fdw)
.bashrc的環境變量要配置:
export PGHOME=/home/pg5/pgsql9.5-devel export PATH=$PATH:$PGHOME/bin export PGDATA=/home/pg5/data export PGUSER=pg5 export PGPORT=5433
測試數據:
create database eachma ENCODING=UTF8; create table tbl(id int primary key,info text,crt_time timestamp); insert into tbl select generate_series(1,100000),md5(random()::text),clock_timestamp(); create materialized view tbl_view as select * from tbl where id<1000 with no data; create materialized view tbl_view1 as select * from tbl with no data; create unique index idx_tbl_view_id on tbl_view(id); create unique index idx_tbl_view1_id on tbl_view1(id); refresh materialized view tbl_view; refresh materialized view tbl_view1; \timing #打開事務執行時間
增量刷新;refresh materialized view concurrently tbl_view1;
非增量刷新:refresh materialized view tbl_view1;
pg中查看物化視圖表:
select * from pg_matviews;
eachma=# select * from pg_matviews; -[ RECORD 1 ]+------------------------- schemaname | public matviewname | tbl_view matviewowner | pg5 tablespace | hasindexes | t ispopulated | t definition | SELECT tbl.id, + | tbl.info, + | tbl.crt_time + | FROM tbl + | WHERE (tbl.id < 1000); -[ RECORD 2 ]+------------------------- schemaname | public matviewname | tbl_view1 matviewowner | pg5 tablespace | hasindexes | t ispopulated | t definition | SELECT tbl.id, + | tbl.info, + | tbl.crt_time + | FROM tbl; Time: 0.758 ms
增量刷新不會鎖表,阻斷其餘查詢。可是視圖的非增量刷新會鎖表。二者利弊不一,前者不鎖表,可是執行須要的時間比較長,由於是Join查詢須要一條條的數據進行對比,以時間來換取查詢鎖。以致於不會影響到物化視圖的查詢工做 。然後者的執行等待時間比較短.但其餘的查詢須要等待刷新以後才能完成:
增量刷新:
eachma=# begin; BEGIN Time: 0.117 ms eachma=# refresh materialized view concurrently tbl_view1; REFRESH MATERIALIZED VIEW Time: 2085.527 ms eachma=# commit; COMMIT Time: 2.718 ms eachma=# end
#非增量刷新 eachma=# begin; BEGIN Time: 0.104 ms eachma=# refresh materialized view tbl_view1; REFRESH MATERIALIZED VIEW Time: 209.312 ms eachma=# commit; COMMIT Time: 9.777 ms eachma=# end; WARNING: there is no transaction in progress COMMIT Time: 0.318 ms
pg是支持外部表物化視圖。例如oracle裏面有一個表的數據是咱們想須要的,可是通常狀況下是須要咱們經過odbc或者是dump出來。可是由於pg支持外部表(FDW,dblink)。能夠經過建立一個咱們須要oracle中數據,那麼能夠先建立一個外部表,而後給這個外部表建立物化視圖,這樣也減小的數據的拷貝,oracle有數據更新時也能夠去更新視圖。但要注意物化視圖的表增量的刷新要與遠程表規則一致(索引)
下面是作一個外部表(以postgresql9.4的庫爲外部庫)的物化視圖測試:
eachma=# CREATE EXTENSION postgres_fdw;
安裝成功:
eachma=# \df List of functions Schema | Name | Result data type | Argument data types | Type --------+------------------------+------------------+---------------------+-------- public | postgres_fdw_handler | fdw_handler | | normal public | postgres_fdw_validator | void | text[], oid | normal
create server pg4 VERSION '9.4' foreign data wrapper postgres_fdw OPTIONS (host '211.69.228.50',dbname 'tech',port '5432'); #建立server CREATE USER MAPPING for pg5 server pg4 options(user 'eachma',password '612345');#建立映射用戶 CREATE FOREIGN TABLE orders(order_num integer not null,order_date date not null,cust_id character(10) not null) server pg4 OPTIONS (schema_name 'public',table_name 'orders');#外部表目前不支持主健約束 eachma=# select * from pg_foreign_table; #外部表 -[ RECORD 1 ]------------------------------------- ftrelid | 16579 ftserver | 16577 ftoptions | {schema_name=public,table_name=orders}
eachma=# select * from orders; order_num | order_date | cust_id -----------+------------+------------ 20005 | 2012-05-01 | 1000000001 20006 | 2012-01-12 | 1000000003 20007 | 2012-01-30 | 1000000004 20008 | 2012-02-03 | 1000000005 20009 | 2012-02-08 | 1000000001 (5 rows) eachma=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+------- public | tbl | table | pg5 (1 row)
建立一個外部表物化視圖:
eachma=# create materialized view order_view as select * from orders with no data; SELECT 0 eachma=# select * from order_view; #沒有刷新 ERROR: materialized view "order_view" has not been populated HINT: Use the REFRESH MATERIALIZED VIEW command. eachma=# refresh materialized view order_view; REFRESH MATERIALIZED VIEW eachma=# select * from order_view; order_num | order_date | cust_id -----------+------------+------------ 20005 | 2012-05-01 | 1000000001 20006 | 2012-01-12 | 1000000003 20007 | 2012-01-30 | 1000000004 20008 | 2012-02-03 | 1000000005 20009 | 2012-02-08 | 1000000001 (5 rows) eachma=# select * from pg_matviews; -[ RECORD 1 ]+-------------------------- schemaname | public matviewname | order_view matviewowner | pg5 tablespace | hasindexes | f ispopulated | t definition | SELECT orders.order_num,+ | orders.order_date, + | orders.cust_id + | FROM orders;
參考:http://www.postgresql.org/docs/9.5/static/postgres-fdw.html
postgresql資料:https://github.com/ty4z2008/Qix/blob/master/pg.md