监控表的使用有那么几种方式
1. 审计,10g的颗粒度已经很细了,磁盘空间是个问题.
2. 还有就是查看 FLASHBACK_TRANSACTION_QUERY,但是这个与undo_retention以及undo大小有关系,表大的话,查这个很费劲.
3. 再来就是触发器,高并发的情况下要谨慎使用.
闲话不多说,做个用触发器监控数据表使用的例子.
先建立测试表.
create table TGLOG
(
USERCODE VARCHAR2(25) not null,
TIME DATE not null,
TYPE VARCHAR2(25) not null,
IP VARCHAR2(25) not null,
RAMARK1 VARCHAR2(25) not null,
REMARK2 VARCHAR2(225)
);
create table TGTEST
(
USERCODE VARCHAR2(10) not null,
LMAGENTID VARCHAR2(25) not null,
REMARK VARCHAR2(225),
VALIDSTATUS VARCHAR2(1) not null,
FLAG VARCHAR2(1)
);
目标:对TGTEST的inser,update,delete 都记录下相应信息
(操作类型,username,timestamp,ip,machine,module),并且只跟踪usercode='0000000000'的数据.
CREATE OR REPLACE TRIGGER tg_trace_tab_tgtest
AFTER UPDATE or delete or insert ON TGTEST
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
v_type VARCHAR2(500);
old_value int;
new_value int;
BEGIN
v_type:='';
old_value:=:old.usercode;
new_value:=:new.usercode;
if(old_value='0000000000' or new_value='0000000000') then
IF inserting THEN
v_type:='INSERT';
ELSIF updating THEN
v_type:='UPDATE';
ELSIF deleting THEN
v_type:='DELETE';
END IF;
INSERT INTO tglog
select username,sysdate,v_type,SYS_CONTEXT('USERENV','IP_ADDRESS'),machine,program
from v$session where AUDSID = USERENV('SESSIONID');
end if;
END;
/
OK,可以开始测试了.
INSERT INTO tgtest SELECT '0000000000','22','333','1','1' FROM dual ;
COMMIT ;
UPDATE tgtest SET flag=0 WHERE usercode='0000000000';
COMMIT ;
DELETE FROM tgtest WHERE usercode='0000000000';
COMMIT;
SELECT * FROM tglog ORDER BY TIME ASC ;
USERCODE TIME TYPE IP RAMARK1 REMARK2
---------- ------------------- ---------- ------------------------- ------------------------- --------------------
FUDB 2010-07-22 17:20:20 111 192.168.3.188 MSHOME\TPADGERRARD plsqldev.exe
FUDB 2010-07-22 17:38:34 INSERT 192.168.3.188 MSHOME\TPADGERRARD plsqldev.exe
FUDB 2010-07-22 17:38:59 INSERT 192.168.3.188 MSHOME\TPADGERRARD plsqldev.exe
FUDB 2010-07-22 17:39:34 INSERT 192.168.3.188 MSHOME\TPADGERRARD plsqldev.exe
FUDB 2010-07-22 17:41:03 UPDATE 192.168.3.188 MSHOME\TPADGERRARD plsqldev.exe
FUDB 2010-07-22 21:15:20 DELETE 192.168.3.188 MSHOME\TPADGERRARD plsqldev.exe
FUDB 2010-07-22 21:15:56 INSERT 192.168.3.188 MSHOME\TPADGERRARD plsqldev.exe
FUDB 2010-07-22 21:16:40 DELETE 192.168.3.188 MSHOME\TPADGERRARD plsqldev.exe
FUDB 2010-07-22 21:32:20 INSERT 192.168.3.49 WORKGROUP\MESORACLE sqlplus.exe