Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show last access time of a table in Oracle DB

I'm using Oracle RDBMS for database. 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. I've solutions for finding timestamp of other activities like UPDATE, ALTER, etc. I'm looking specifically for SELECT timestamp.

Any help is appreciated.

like image 754
Jishad Avatar asked May 30 '18 03:05

Jishad


People also ask

What is last DDL time in Oracle?

LAST_DDL_TIME is the last time the table had a DDL (structure) change applied, but does NOT include DML (data).

How can we view last record added to a table in Oracle?

You could use ORA_ROWSCN to get the last scn for each block on the table as an *approximate* means of seeing when things happened, or the flashback query syntax to get transaction information, but you are limited to the undo_retention settings on your database.

How do you find the last modified date of a table in Oracle?

If you want to find, when a table was last modified like insert,update ,delete, then use the dictionary table dba_tab_modifications.

How to find out when a table was last accessed?

Query below returns list of tables and their last using date. In SQL Server you can find out when table was last accessed by quering dm_db_index_usage_stats view, but note that this view is cleaned each time SQL Server is restarted. There are no comments. Click here to write the first comment.

How do I find the last access date of an object?

With a simple query, we can easily see the last access date of the object, the last access date by read type and the types of readings that were identified by the view. To identify tables or views that have not been read since the instance was restarted, simply check the records where the last_access IS NULL column is empty.

How to find when a table was last modified in SQL?

If you want to find, when a table was last modified like insert,update ,delete, then use the dictionary table dba_tab_modifications. 1. Insert into test data: SQL [SCOTT@TDB01]SQL>>]insert into TEST values (10); 1 row created. SQL [SCOTT@TDB01]SQL>>]commit; Commit complete. 2. Check dba_tab_modification:

How do I check if a table has not been read?

To identify tables or views that have not been read since the instance was restarted, simply check the records where the last_access IS NULL column is empty. When testing the example above, remember to change the database msdb to your own or leave no database specified if you use the database from the context of your connection.


2 Answers

You could use AUDIT(traditional auditing):

AUDIT SELECT ON table_name;
like image 93
Lukasz Szozda Avatar answered Sep 28 '22 10:09

Lukasz Szozda


Yes, @lad2025 is right, I'd use AUDIT to find out who is accessing the tables.

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.

like image 34
wolφi Avatar answered Sep 28 '22 11:09

wolφi