[Solved] Query to Check date and time of Last DML done on a table in oracle

EverSQL Database Performance Knowledge Base

Query to Check date and time of Last DML done on a table in oracle

Database type:

As per the requirement i need to capture the the last DML operations in tables in complete schema while searching i have noticed two resources

Resource 1 https://dba.stackexchange.com/questions/115062/how-to-get-a-last-dml-operation-in-oracle-10g-11g

Resource 2 https://oraclebykkbakshi.blogspot.com/2018/04/check-date-and-time-of-last-dml-done-on.html

Among the above i have tried using both the queries

Query1 - DBA_TAB_MODIFICATIONS

SELECT TABLE_OWNER,TABLE_NAME,INSERTS,UPDATES,DELETES,TIMESTAMP AS LAST_CHANGE
FROM  DBA_TAB_MODIFICATIONS
WHERE TO_CHAR(TIMESTAMP,'DD.MM.YYYY') = TO_CHAR(sysdate,'DD.MM.YYYY') 
and table_owner='SCHEMA_NAME'
and table_name in ('MY LIST OF TABLES')

Result - it fetched only the results which had fetched only few tables

Query2 - is very slow

select max(ora_rowscn),SCN_TO_TIMESTAMP(max(ora_rowscn)) FROM 'MY LIST OF TABLES'

If i check for any tables using query 2 i am getting the below error

Error Query 2

ORA-08181: specified number is not a valid system change number
ORA-06512: at "SYS.SCN_TO_TIMESTAMP", line 1
08181. 00000 -  "specified number is not a valid system change number"
*Cause:    supplied scn was beyond the bounds of a valid scn.
*Action:   use a valid scn.

how to check all the tables in a single go ,since if i need to use query 2 ,how do i list out multiple tables in a single query ?

so my question is which is the correct query to search for ?

or any other better ways to look for ,any suggestions ?

Upon suggestion i have tried running the query

SELECT *
  FROM 'TABLE_NAME' t
MINUS
SELECT *
  FROM 'TABLE_NAME'
  AS OF timestamp systimestamp - interval '1' day t

AS OF timestamp systimestamp - interval '1' day t - what is the value i have to use here ?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Calling Functions With Indexed Columns (query line: 11): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `TIMESTAMP` is indexed, the index won’t be used as it’s wrapped with the function `TO_CHAR`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 11): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `sysdate` is indexed, the index won’t be used as it’s wrapped with the function `TO_CHAR`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
CREATE INDEX dba_modifications_idx_table_owner_table_name ON DBA_TAB_MODIFICATIONS (table_owner,table_name);
The optimized query:
SELECT
        DBA_TAB_MODIFICATIONS.TABLE_OWNER,
        DBA_TAB_MODIFICATIONS.TABLE_NAME,
        DBA_TAB_MODIFICATIONS.INSERTS,
        DBA_TAB_MODIFICATIONS.UPDATES,
        DBA_TAB_MODIFICATIONS.DELETES,
        DBA_TAB_MODIFICATIONS.TIMESTAMP AS LAST_CHANGE 
    FROM
        DBA_TAB_MODIFICATIONS 
    WHERE
        TO_CHAR(DBA_TAB_MODIFICATIONS.TIMESTAMP, 'DD.MM.YYYY') = TO_CHAR(DBA_TAB_MODIFICATIONS.sysdate, 'DD.MM.YYYY') 
        AND DBA_TAB_MODIFICATIONS.table_owner = 'SCHEMA_NAME' 
        AND DBA_TAB_MODIFICATIONS.table_name IN (
            'MY LIST OF TABLES'
        )

Related Articles



* original question posted on StackOverflow here.