Oracle中定位数据表的最近DML时间


由于时间久,数据库中的表太多,前后很多人操作过,也不知道哪些表有用哪些没用,于是,想通过判断数据表的最后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>

linuxboy的RSS地址:https://www.linuxboy.net/rssFeed.aspx

本文永久更新链接地址:https://www.linuxboy.net/Linux/2018-11/155427.htm

相关内容