MV定義語句中包含Fact的VIEW,能否Rewrite ?
在資料倉儲環境中,複製一張dimension表是比較容易的事,但對於fact表,也許很多人更傾向於建立一個view,因為在新的環境裡複製一張十幾億條記錄的表,而且將來還要花心思去同步更新,其工作量是難以想象的。於是我們通常把MV的定義在IFCT對應的VIEW上,為了證明是否MV 定義語句裡面包含VIEW,也 可以達到REWRITE的效果,我們做如下測試:
1. 以SUM_SALES_PSCAT_WEEK_MV為原型,建立測試MV和定於語句中的所有Fact和dimension;
###SUM_SALES_PSCAT_WEEK_MV
### Definition: ######################
SELECT p.prod_subcategory, t.week_ending_day,
sum(s.amount_sold) AS sum_amount_sold
FROM sales s, products p, times t
where s.time_id = t.time_id and s.prod_id = p.prod_id
group by p.prod_subcategory, t.week_ending_day
---- 察看mview的定義語句和table的ddl:
select * from dba_mviews where mview_name='SUM_SALES_PSCAT_WEEK_MV'
select * from dba_tables where table_name='SUM_SALES_PSCAT_WEEK_MV'
######################## Build new MV ##############
------- create new products (新的dimension,用來對應新的fact view)
CREATE TABLE PRODUCTS_TEST_QR
(
PROD_ID NUMBER(6) NOT NULL,
PROD_NAME VARCHAR2(50 BYTE) NOT NULL,
PROD_DESC VARCHAR2(4000 BYTE) NOT NULL,
PROD_SUBCATEGORY VARCHAR2(50 BYTE) NOT NULL,
PROD_SUBCATEGORY_ID NUMBER NOT NULL,
PROD_SUBCATEGORY_DESC VARCHAR2(2000 BYTE) NOT NULL,
PROD_CATEGORY VARCHAR2(50 BYTE) NOT NULL,
PROD_CATEGORY_ID NUMBER NOT NULL,
PROD_CATEGORY_DESC VARCHAR2(2000 BYTE) NOT NULL,
PROD_WEIGHT_CLASS NUMBER(3) NOT NULL,
PROD_UNIT_OF_MEASURE VARCHAR2(20 BYTE),
PROD_PACK_SIZE VARCHAR2(30 BYTE) NOT NULL,
SUPPLIER_ID NUMBER(6) NOT NULL,
PROD_STATUS VARCHAR2(20 BYTE) NOT NULL,
PROD_LIST_PRICE NUMBER(8,2) NOT NULL,
PROD_MIN_PRICE NUMBER(8,2) NOT NULL,
PROD_TOTAL VARCHAR2(13 BYTE) NOT NULL,
PROD_TOTAL_ID NUMBER NOT NULL,
PROD_SRC_ID NUMBER,
PROD_EFF_FROM DATE,
PROD_EFF_TO DATE,
PROD_VALID VARCHAR2(1 BYTE)
)
TABLESPACE EXAMPLE
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
NOLOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
CREATE BITMAP INDEX PRODUCTS_PROD_TEST_QR ON PRODUCTS_test_qr
(PROD_STATUS)
NOLOGGING
TABLESPACE EXAMPLE
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
NOPARALLEL;
CREATE UNIQUE INDEX PRODUCTS_TEST_QR_PK ON PRODUCTS_test_qr
(PROD_ID)
NOLOGGING
TABLESPACE EXAMPLE
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
NOPARALLEL;
CREATE INDEX PRODUCTS_PROD_TEST_QR_IX ON PRODUCTS_test_qr
(PROD_SUBCATEGORY)
NOLOGGING
TABLESPACE EXAMPLE
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
NOPARALLEL;
CREATE INDEX PRODUCTS_PROD_CA_TEST_QR_IX ON PRODUCTS_test_qr
(PROD_CATEGORY)
NOLOGGING
TABLESPACE EXAMPLE
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
NOPARALLEL;
ALTER TABLE PRODUCTS_test_qr ADD (
CONSTRAINT PRODUCTS_TEST_QR_PK
PRIMARY KEY
(PROD_ID)
USING INDEX
TABLESPACE EXAMPLE
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
));
INSERT INTO PRODUCTS_TEST_QR select * from products;
------ 建立fact所對應的VIEW:
CREATE view sales_view as select * from sales;
------ 注意,在view上建立外來鍵,透過trigger控制pf約束;
alter view sales_view add constraint product_vw_fk foreign key (PROD_ID) references PRODUCTS_TEST_QR (PRODUCTS_TEST_QR_PK) disable;
alter view sales_view add constraint time_vw_fk foreign key (TIME_ID) references TIMES (TIME_ID) disable;
ALTER VIEW sales_view MODIFY CONSTRAINT product_vw_fk RELY;
ALTER VIEW sales_view MODIFY CONSTRAINT time_vw_fk RELY;--( Rely為了在query_rewrite_integrity為trust和stale_tolerated的情況下能夠rewrite)
--- 建立mv的基表:
CREATE TABLE SUM_SALES_TEST_QR_MV
(
PROD_SUBCATEGORY VARCHAR2(50 BYTE) NOT NULL,
WEEK_ENDING_DAY DATE NOT NULL,
SUM_AMOUNT_SOLD NUMBER
)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
CREATE UNIQUE INDEX I_SNAP$_SUM_SALES_TEST_QR_WE ON SUM_SALES_TEST_QR_MV
(SYS_OP_MAP_NONNULL("PROD_SUBCATEGORY"), SYS_OP_MAP_NONNULL("WEEK_ENDING_DAY"))
LOGGING
TABLESPACE USERS
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
NOPARALLEL;
--- 建立新的MV :SUM_SALES_TEST_QR_MV
DROP MATERIALIZED VIEW SUM_SALES_TEST_QR_MV;
CREATE MATERIALIZED VIEW SUM_SALES_TEST_QR_MV
ON PREBUILT TABLE
REFRESH FORCE ON DEMAND
ENABLE QUERY REWRITE
AS
SELECT p.prod_subcategory, t.week_ending_day,
sum(s.amount_sold) AS sum_amount_sold
FROM sales_view s, PRODUCTS_TEST_QR p, times t
where s.time_id = t.time_id and s.prod_id = p.prod_id
group by p.prod_subcategory, t.week_ending_day;
################### test Rewrite:
1. 測試sales_view與products_test_qr的查詢:
explain plan for
SELECT p.prod_subcategory,
sum(s.amount_sold) AS sum_amount_sold
FROM sales_view s, products_test_qr p
where s.prod_id = p.prod_id
group by p.prod_subcategory;
------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 40 | 3 (34)| 00:00:01 |
| 1 | HASH GROUP BY | | 1 | 40 | 3 (34)| 00:00:01 |
| 2 | MAT_VIEW REWRITE ACCESS FULL| SUM_SALES_TEST_QR_MV | 1 | 40 | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------
2. 測試sales_view與Times的查詢:
explain plan for
SELECT t.week_ending_day,
sum(s.amount_sold) AS sum_amount_sold
FROM sales_view s, times t
where s.time_id = t.time_id
group by t.week_ending_day;
------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 22 | 3 (34)| 00:00:01 |
| 1 | HASH GROUP BY | | 1 | 22 | 3 (34)| 00:00:01 |
| 2 | MAT_VIEW REWRITE ACCESS FULL| SUM_SALES_TEST_QR_MV | 1 | 22 | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------
以上結果證明view也可以做query rewrite, 但前提dimension要不同;
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/661919/viewspace-1008098/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql語句中JOIN ON 的使用SQL
- 【VIEW】Oracle如何查詢固定檢視的定義或底層sql語句ViewOracleSQL
- sql語句中#{}和${}的區別SQL
- nginx rewrite語法格式Nginx
- Android自定義View之定點寫文字AndroidView
- SQL語句中exists和in的區別SQL
- Ruby 中的語句中斷和返回
- SQL語句中not in 和not exist的區別SQL
- Android 自定義 View:包含多種狀態的下載用圓形進度條AndroidView
- 樹的定義 基本術語
- Android自定義View:View(二)AndroidView
- 難被定義的美團,能否成為AT之外的新勢力?
- MyBatis在SQL語句中取list的大小MyBatisSQL
- 2.7.6.2.1 ALTER SYSTEM SET語句中的SCOPE子句
- drools中Fact的equality modes
- 自定義VIEWView
- c語言的定義與宣告C語言
- MyBatis的使用三(在sql語句中傳值)MyBatisSQL
- Python 提取出SQL語句中Where的值的方法PythonSQL
- c 語言中巨集定義和定義全域性變數的區別變數
- Android自定義view-自繪ViewAndroidView
- 樹的定義及相關術語
- 封裝自定義圓角方向並且可設定投影的View封裝View
- Oracle sql 語句中帶有特殊的字元處理OracleSQL字元
- 如何自動填充SQL語句中的公共欄位SQL
- SQL語句中的AND和OR執行順序問題SQL
- Flutter自定義View的實現FlutterView
- c語言函式指標的定義C語言函式指標
- lua語法-變數的定義與使用變數
- [20180928]避免表示式在sql語句中.txtSQL
- Android自定義View整合AndroidView
- 自定義View之SwitchViewView
- MyBatis從插入語句中檢索自動生成的IDMyBatis
- SQL語句中聚合函式忽略NULL值的總結SQL函式Null
- 直播平臺原始碼,自定義設定 View 四個角的圓角 以及邊框的設定原始碼View
- Android自定義View之Canvas的使用AndroidViewCanvas
- Web 3.0 術語及其簡單英語定義Web
- Android自定義View之Window、ViewRootImpl和View的三大流程AndroidView
- C++ 這個語句中[&]是什麼意思C++