由于时间久, 数据库中的表太多, 前后很多人操作过, 也不知道哪些表有用哪些没用, 于是, 想通过判断数据表的最后 DML 时间, 来确定哪些数据表最近没操作过.
- SQL> create table A as select id,name from t_employee;
- Table created
- SQL> select tb.table_name,tb.monitoring from user_tables tb where table_name='A';
- TABLE_NAME MONITORING
- ------------------------------ ----------
- A YES
由此可以看到, 数据表默认都是启动了 monitoring 功能的.
下面, 通过 ORA_ROWSCN 来定位表 A 的最后 dml 时间.
- SQL> select max(ora_rowscn), scn_to_timestamp(max(ora_rowscn)) from mvs.A;
- MAX(ORA_ROWSCN) SCN_TO_TIMESTAMP(MAX(ORA_ROWSC
- --------------- --------------------------------------------------------------------------------
155220760 29-11 月 - 11 11.25.50.000000000 上午
有人说只要表为 monitoring 状态, 从视图 user_tab_modifications 也可以看到, 可实际上确什么都没查到.
- SQL> select * from user_tab_modifications where table_name='A';
- TABLE_NAME PARTITION_NAME SUBPARTITION_NAME INSERTS UPDATES DELETES TIMESTAMP TRUNCATED DROP_SEGMENTS
- ------------------------------ ------------------------------ ------------------------------ ---------- ---------- ---------- ----------- --------- -------------
删除 A 中的数据, 只剩下一条.
- SQL> select max(ora_rowscn), scn_to_timestamp(max(ora_rowscn)) from mvs.A;
- MAX(ORA_ROWSCN) SCN_TO_TIMESTAMP(MAX(ORA_ROWSC
- --------------- --------------------------------------------------------------------------------
155223006 29-11 月 - 11 11.46.33.000000000 上午
然后再插入一条记录.
- SQL> insert into a(id,name) values(1,'test');
- 1 row inserted
- SQL> commit;
- Commit complete
查看记录及对应的伪列 ORA_ROWSCN 值.
- SQL> select id,name,ora_rowscn from a order by id;
- ID NAME ORA_ROWSCN
- ---------- -------------------- ----------------------
- 1 test 155223032
- 1108 s11 155223006
- SQL>
通过上面的伪列 ORA_ROWSCN 及函数 SCN_TO_TIMESTAMP(ORA_ROWSCN) 就可以获得该行数据的最后 DML 时间.
- SQL> insert into a(id,name) values(2,'test');
- 1 row inserted
- SQL> insert into a(id,name) values(3,'test');
- 1 row inserted
- SQL> commit;
- Commit complete
- SQL> select id,name,ora_rowscn from a order by id;
- ID NAME ORA_ROWSCN
- ---------- -------------------- ----------
- 1 test 155226434
- 2 test 155226434
- 3 test 155226434
- 1108 s11 155223006
- SQL> insert into a(id,name) values(4,'test');
- 1 row inserted
- SQL> commit;
- Commit complete
- SQL> select id,name,ora_rowscn from a order by id;
- ID NAME ORA_ROWSCN
- ---------- -------------------- ----------
- 1 test 155226448
- 2 test 155226448
- 3 test 155226448
- 4 test 155226448
- 1108 s11 155223006
- SQL> insert into a(id,name) values(5,'test2');
- 1 row inserted
- SQL> commit;
- Commit complete
- SQL> select id,name,ora_rowscn from a order by id;
- ID NAME ORA_ROWSCN
- ---------- -------------------- ----------
- 1 test 155226463
- 2 test 155226463
- 3 test 155226463
- 4 test 155226463
- 5 test2 155226463
- 1108 s11 155223006
- 6 rows selected
- SQL>
来源: http://www.linuxidc.com/Linux/2018-11/155427.htm