在MV上建立触发器实验

2019-05-25 19:43:50 浏览数 (1)

  1. -- 建立MV测试表
  2. CREATE TABLE tbl1
  3. (
  4. a NUMBER,
  5. b VARCHAR2 (20)
  6. );
  7. CREATE UNIQUE INDEX tbl1_pk ON tbl1 (a);
  8. ALTER TABLE tbl1 ADD (CONSTRAINT tbl1_pl PRIMARY KEY(a));
  9. -- 建立MV日志,单一表聚合视图的快速刷新需要指定including new values子句
  10. CREATE MATERIALIZED VIEW LOG ON tbl1 INCLUDING NEW VALUES;
  11. -- 建立MV
  12. CREATE MATERIALIZED VIEW mv_tbl1
  13. BUILD IMMEDIATE
  14. REFRESH FAST
  15. START WITH TO_DATE('2013-06-01 08:00:00','yyyy-mm-dd hh24:mi:ss')
  16. NEXT SYSDATE 1/24
  17. AS
  18. SELECT * FROM tbl1;
  19. -- 建立trigger测试表
  20. CREATE TABLE mv_tbl1_tri
  21. (
  22. a NUMBER,
  23. b VARCHAR (20),
  24. c VARCHAR (20)
  25. );
  26. -- 建立trigger
  27. CREATE OR REPLACE TRIGGER tri_mv
  28. AFTER DELETE OR INSERT OR UPDATE
  29. ON mv_tbl1
  30. REFERENCING NEW AS new OLD AS old
  31. FOR EACH ROW
  32. BEGIN
  33. CASE
  34. WHEN INSERTING THEN
  35. INSERT INTO mv_tbl1_tri VALUES (:new.a, :new.b, 'insert');
  36. WHEN UPDATING THEN
  37. INSERT INTO mv_tbl1_tri VALUES (:new.a, :new.b, 'update');
  38. WHEN DELETING THEN
  39. INSERT INTO mv_tbl1_tri VALUES (:old.a, :old.b, 'delete');
  40. END CASE;
  41. EXCEPTION
  42. WHEN OTHERS THEN
  43. RAISE;
  44. END tri_mv;
  45. /
  46. -- 新增MV测试表数据
  47. INSERT INTO tbl1 VALUES (1, '测试数据1');
  48. INSERT INTO tbl1 VALUES (2, '测试数据2');
  49. INSERT INTO tbl1 VALUES (3, '测试数据3');
  50. COMMIT;
  51. SELECT * FROM tbl1;
  52. SELECT * FROM mlog$_tbl1; -- 生成3条“I”MV日志记录
  53. -- 手工刷新MV
  54. EXEC dbms_mview.refresh('mv_tbl1'); -- 刷新MV,触发3条insert,清空MV日志
  55. SELECT * FROM mlog$_tbl1;
  56. -- 检查trigger测试表
  57. SELECT * FROM mv_tbl1_tri;
  58. -- 修改MV测试表数据
  59. UPDATE tbl1
  60. SET a = 13
  61. WHERE a = 3;
  62. UPDATE tbl1
  63. SET a = 3
  64. WHERE a = 13;
  65. UPDATE tbl1
  66. SET a = 13
  67. WHERE a = 3;
  68. UPDATE tbl1
  69. SET a = 3
  70. WHERE a = 13;
  71. UPDATE tbl1
  72. SET a = 13
  73. WHERE a = 3;
  74. UPDATE tbl1
  75. SET a = 13
  76. WHERE a = 13;
  77. UPDATE tbl1
  78. SET a = 13
  79. WHERE a = 13;
  80. UPDATE tbl1
  81. SET b = '测试数据13'
  82. WHERE a = 13;
  83. UPDATE tbl1
  84. SET b = '测试数据3'
  85. WHERE a = 13;
  86. UPDATE tbl1
  87. SET b = '测试数据13';
  88. COMMIT;
  89. SELECT * FROM tbl1;
  90. SELECT * FROM mlog$_tbl1; -- 对每条记录的update生成2条MV日志记录:当主键发生改变时,记录一对“D”、“I”型记录,表示先删除后插入;当主键不变时,记录一对“U”、“U”型记录,分别表示新值和旧值;
  91. -- 手工刷新MV
  92. EXEC dbms_mview.refresh('mv_tbl1'); -- 刷新MV,对MV日志进行归并处理,对每一个主键,只执行其归并后结果的操作
  93. SELECT * FROM mlog$_tbl1;
  94. -- 检查trigger测试表
  95. SELECT * FROM mv_tbl1_tri; -- 对修改后的新主键13的所有后续修改只触发了1条insert;
  96. DELETE FROM tbl1; -- 生成3条“D”MV日志记录
  97. COMMIT;
  98. SELECT * FROM tbl1;
  99. SELECT * FROM mlog$_tbl1;
  100. -- 手工刷新MV
  101. EXEC dbms_mview.refresh('mv_tbl1'); -- 刷新MV,触发3条delete,清空MV日志
  102. SELECT * FROM mlog$_tbl1;
  103. -- 检查trigger测试表
  104. SELECT * FROM mv_tbl1_tri;
  105. /***
  106. 结论
  107. 1. 在MV上可以建立触发器
  108. 2. MV触发器基于刷新时间点的MV日志归并结果,在一些场景(只要记录两次刷新时间点数据的差异,不需要记录两次刷新之间的历史变化)可以简化应用处理。
  109. ***/

0 人点赞