阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

166次阅读
没有评论

共计 2670 个字符,预计需要花费 7 分钟才能阅读完成。

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

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2670字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中