大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
小编给大家分享一下在Oracle10g中如何使用包DBMS_ADVANCED_REWRITE实现新的查询重写功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
天津ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
从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实现新的查询重写功能”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!