您好,登錄后才能下訂單哦!
小編給大家分享一下在Oracle10g中如何使用包DBMS_ADVANCED_REWRITE實現新的查詢重寫功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
從Oracle10g版本1開始,提供了新的包DBMS_ADVANCED_REWRITE來實現查詢重寫。它允許你截獲特定的SQL語句,并將
其重定義為另一個SQL語句。下面是一個簡單的例子:
1、確認測試用戶有必要的權限去運行
CONN sys/password AS SYSDBA
GRANT EXECUTE ON DBMS_ADVANCED_REWRITE TO test;
GRANT CREATE MATERIALIZED VIEW TO test;
[@more@]
2、創建測試用表
CONN test/test
DROP TABLE rewrite_test_tab;
CREATE TABLE rewrite_test_tab (
id NUMBER,
description VARCHAR2(50),
CONSTRAINT rewrite_test_tab_pk PRIMARY KEY (id)
);
INSERT INTO rewrite_test_tab (id, description) VALUES (1, 'GLASGOW');
INSERT INTO rewrite_test_tab (id, description) VALUES (2, 'BIRMINGHAM');
INSERT INTO rewrite_test_tab (id, description) VALUES (3, 'LONDON');
COMMIT;
EXEC DBMS_STATS.gather_table_stats(USER, 'rewrite_test_tab');
3、查詢測試表
SELECT * FROM rewrite_test_tab;
ID DESCRIPTION
---------- --------------------------------------------------
1 GLASGOW
2 BIRMINGHAM
3 LONDON
3 rows selected.
SQL>
4、創建要替換成的SQL使用的視圖,然后將上面的語句重定義為查詢這個視圖
CREATE OR REPLACE VIEW rewrite_test_tab_v AS
SELECT id,
INITCAP(description) AS description
FROM rewrite_test_tab
ORDER BY description;
BEGIN
SYS.DBMS_ADVANCED_REWRITE.declare_rewrite_equivalence (
name => 'test_rewrite',
source_stmt => 'SELECT * FROM rewrite_test_tab',
destination_stmt => 'SELECT * FROM rewrite_test_tab_v',
validate => FALSE,
rewrite_mode => 'TEXT_MATCH');
END;
/
5、初始化參數QUERY_REWRITE_INTEGRITY缺省值為"enforced",意味只有原始語句的輸出和替換后語句的輸出完全
一致才會進行重寫替換。而我們要實現替換后的輸出跟原始輸出是有差別的,所以需要將這個參數的值修改為
"TRUSTED"才行。
ALTER SESSION SET QUERY_REWRITE_INTEGRITY = TRUSTED;
Session altered.
SELECT * FROM rewrite_test_tab;
ID DESCRIPTION
---------- --------------------------------------------------
2 Birmingham
1 Glasgow
3 London
3 rows selected.
SQL>
6、視圖[USER|ALL|DBA]_REWRITE_EQUIVALENCES中有這些重定義查詢的相關信息
SELECT * FROM user_rewrite_equivalences;
OWNER NAME
------------------------------ ------------------------------
SOURCE_STMT
--------------------------------------------------------------------------------
DESTINATION_STMT REWRITE_MO
-------------------------------------------------------------------------------- ----------
TEST TEST_REWRITE
SELECT * FROM rewrite_test_tab
SELECT * FROM rewrite_test_tab_v TEXT_MATCH
1 row selected.
SQL>
看完了這篇文章,相信你對“在Oracle10g中如何使用包DBMS_ADVANCED_REWRITE實現新的查詢重寫功能”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。