newbieexpert
asked on
Find difference between timestamps (obtained from two different SCNs)
Hello there,
I came up with a SQL to find the timestamps of current scn and applied scn on primary and standby 11gR2 DBs respectively. This is for checking standby LAG. I now need to find the "difference" between these two timestamps in minutes/hours.. Is there a way this can be accomplished? Will appreciate your help in getting this difference.
I tried some options but they didn't work (using to_date)..
select scn_to_timestamp(to_char(c urrent_scn )) ,scn_to_timestamp(to_char( APPLIED_SC N)) "applied scn" from v$database,v$archive_dest where dest_id=2;
SCN_TO_TIMESTAMP(TO_CHAR(C URRENT_SCN ))
-------------------------- ---------- ---------- ---------- ---------- ---------
applied scn
-------------------------- ---------- ---------- ---------- ---------- ---------
14-MAR-18 03.13.08.000000000 AM
14-MAR-18 03.12.59.000000000 AM
select to_date(scn_to_timestamp(t o_char(cur rent_scn)) ,'dd-mon-y y hh.mi.ss.ns AM') as first_date, to_date(scn_to_timestamp(t o_char(APP LIED_SCN)) ,'dd-mon-y y hh.mi.ss.ns AM') as second_date from v$database,v$archive_dest where dest_id=2;
select to_date(scn_to_timestamp(t o_char(cur rent_scn)) ,'dd-mon-y y hh.mi.ss.ns AM') as first_date, to_date(scn_to_timestamp(t o_char(APP LIED_SCN)) ,'dd-mon-y y hh.mi.ss.ns AM') as second_date from v$database,v$archive_dest where dest_id=2
*
ERROR at line 1:
ORA-01821: date format not recognized
select round((second_date - first_date) * (24),2) as time_in_hours from
( select to_date(to_char(scn_to_tim estamp(152 2539347410 1),'mon-dd -yyyy hh24:mi:ss')) as first_date, to_date(to_char(scn_to_tim estamp(172 2539347410 1),'mon-dd -yyyy hh24:mi:ss')) as second_date from dual);
( select to_date(to_char(scn_to_tim estamp(152 2539347410 1),'mon-dd -yyyy hh24:mi:ss')) as first_date, to_date(to_char(scn_to_tim estamp(172 2539347410 1),'mon-dd -yyyy hh24:mi:ss')) as second_date from dual)
*
ERROR at line 2:
ORA-08181: specified number is not a valid system change number
ORA-06512: at "SYS.SCN_TO_TIMESTAMP", line 1
I came up with a SQL to find the timestamps of current scn and applied scn on primary and standby 11gR2 DBs respectively. This is for checking standby LAG. I now need to find the "difference" between these two timestamps in minutes/hours.. Is there a way this can be accomplished? Will appreciate your help in getting this difference.
I tried some options but they didn't work (using to_date)..
select scn_to_timestamp(to_char(c
SCN_TO_TIMESTAMP(TO_CHAR(C
--------------------------
applied scn
--------------------------
14-MAR-18 03.13.08.000000000 AM
14-MAR-18 03.12.59.000000000 AM
select to_date(scn_to_timestamp(t
select to_date(scn_to_timestamp(t
*
ERROR at line 1:
ORA-01821: date format not recognized
select round((second_date - first_date) * (24),2) as time_in_hours from
( select to_date(to_char(scn_to_tim
( select to_date(to_char(scn_to_tim
*
ERROR at line 2:
ORA-08181: specified number is not a valid system change number
ORA-06512: at "SYS.SCN_TO_TIMESTAMP", line 1
What is it you want to do with the time difference? Do you just want to display it or use it somehow?
you can use a date field also to find the difference between the standby database time and the active database time
the standby database is at this time:
but, you also need to check v$recover_file
when files need recovery and are not all synched, v$recover_file contains entries
this is typically so during creation of the standby
the controlfile created with (alter database create standby controlfile) ... will be newer than the database file timestamps
that query should cover that phase too
and if you want the difference between the active database (which is at sysdate time) in minutes:
oops, that query doesn't run on a standby
But this query does work:
the standby database is at this time:
select controlfile_time from v$database;
but, you also need to check v$recover_file
select min(time) from v$recover_file
actually it's the least of above 2 querries:with rt as (select min(time) recover_time from v$recover_file)
select nvl(least(controlfile_time, recover_time), controlfile_time) standby_time from v$database, rt;
when files need recovery and are not all synched, v$recover_file contains entries
this is typically so during creation of the standby
the controlfile created with (alter database create standby controlfile) ... will be newer than the database file timestamps
that query should cover that phase too
and if you want the difference between the active database (which is at sysdate time) in minutes:
with rt as (select min(time) recover_time from v$recover_file)
select (sysdate - nvl(least(controlfile_time, recover_time), controlfile_time))*24*60 standby_time_lag_minutes from v$database, rt;
oops, that query doesn't run on a standby
ERROR at line 2:
ORA-01219: database not open: queries allowed on fixed tables/views only
A standby database doesn't like the WITH clause ... it only accepts query on fixed tablesBut this query does work:
select (sysdate - nvl(least(controlfile_time, (select min(time) from v$recover_file)), controlfile_time))*24*60 standby_time_lag_minutes
from v$database;
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
For example:
SQL> select systimestamp-(systimestamp
+000000000 17:08:34.116888
That is 17 hours, 8 minutes and 34 seconds and some fractional seconds different.
If you want it broken down more, there are MANY examples on the web on extracting individual pieces from an interval.
I'm also not sure what you are after. Everything I remember reading on SCN_TO_TIMESTAMP is that it is sort of an estimate.