简体   繁体   English

显示Oracle DB中表的上次访问时间

[英]Show last access time of a table in Oracle DB

I'm using Oracle RDBMS for database. 我正在使用Oracle RDBMS作为数据库。 I'm trying to clean up the database and want to know if certain tables are even used any more. 我正在尝试清理数据库,并想知道某些表是否已经被使用了。 I require the last accessed time of these tables to reach a conclusion. 我要求这些表的最后访问时间得出结论。

Please note that I need the timestamp of the latest SELECT query. 请注意,我需要最新SELECT查询的时间戳。 I've solutions for finding timestamp of other activities like UPDATE , ALTER , etc. I'm looking specifically for SELECT timestamp. 我有解决方案来查找UPDATEALTER等其他活动的时间戳。我正在寻找SELECT时间戳。

Any help is appreciated. 任何帮助表示赞赏。

您可以使用AUDIT(traditional auditing)

AUDIT SELECT ON table_name;

Yes, @lad2025 is right, I'd use AUDIT to find out who is accessing the tables. 是的,@ lad2025是对的,我会使用AUDIT来找出谁正在访问这些表。

To switch it on, just use: 要打开它,只需使用:

AUDIT SELECT ON scott.emp  BY ACCESS;
AUDIT SELECT ON scott.dept BY ACCESS;  

You can then see which tables are audited by querying: 然后,您可以通过查询来查看哪些表经过审计:

SELECT * 
FROM DBA_OBJ_AUDIT_OPTS 
WHERE owner='SCOTT';

OWNER OBJECT_NAME OBJECT_TYPE SEL
SCOTT DEPT        TABLE       A/A
SCOT  EMP         TABLE       A/A

If somebody selects from one of the audited tables, it appears in the audit trail: 如果有人从其中一个审计表中进行选择,它将显示在审计跟踪中:

SELECT * FROM scott.emp;

SELECT os_username, username, obj_name, action_name, timestamp
  FROM DBA_AUDIT_TRAIL 
 WHERE timestamp BETWEEN SYSDATE-7 AND SYSDATE
   AND owner = 'SCOTT' 
   AND action=3
 ORDER BY timestamp DESC;

OS_USERNAME USERNAME OBJ_NAME ACTION NAME TIMESTAMP
xxxx        yyy      EMP      SELECT      2018-05-30 08:01:07

To switch it off again, use: 要再次关闭它,请使用:

NOAUDIT ALL ON scott.emp;
SELECT * FROM DBA_OBJ_AUDIT_OPTS WHERE OWNER='SCOTT';

OWNER OBJECT_NAME OBJECT_TYPE SEL
SCOTT DEPT        TABLE       A/A

Be cautious, though. 但是要小心。 One row is added to the audit trail for each SELECT, so watch it carefully, it can generate a lot of data very quickly. 每个SELECT的审计跟踪中都添加了一行,因此请仔细观察,它可以非常快速地生成大量数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM