Thursday, January 7, 2016

How to join v$database with v$instance

Here is a useful query which displays basic information about the database, using v$database and v$instance.
Take the opportunity to calculate the uptime for the database by subtracting the value of startup_time from the current date.
col platform_name format a30
col open_mode format a20
col host_name format a20
col version format a10
col status format a20
col uptime format a30
col name format a10
set lines 300

SELECT D.NAME,D.PLATFORM_NAME,D.CREATED, D.OPEN_MODE,I.HOST_NAME,I.VERSION, I.ARCHIVER,I.STATUS,
   TO_DSINTERVAL( TO_CHAR(
                                    TO_TIMESTAMP(SYSDATE)-I.STARTUP_TIME
                                   )
                      ) "UPTIME", 
                      (SELECT ROUND(SUM(BYTES)/1024/1024/1024) FROM DBA_DATA_FILES) "DB size GB",  
                      ( SELECT ROUND(BYTES/1024/1024/1024) "mem GB" FROM V$SGAINFO WHERE NAME = 'Maximum SGA Size' ) "SGA max size GB"
  FROM V$DATABASE D INNER JOIN V$INSTANCE I 
  ON UPPER(D.NAME) = UPPER(I.INSTANCE_NAME);

Result may look like the following:


NAME PLATFORM_NAME CREATED OPEN_MODE HOST_NAME VERSION ARCHIVER STATUS UPTIME DB size GB SGA max size GB
proddb01 Linux x86 64-bit 10.12.2015 14:16:46 READ WRITE prodserver01.mycompany.com 11.2.0.4.0 STARTED OPEN +00 13:59:55.000000
929
12

3 comments:

  1. Your query wont work with RAC databases, where instance names differ from database name.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Try this for RAC:

    SELECT (SELECT DB_UNIQUE_NAME FROM v$database) "DB Name",
    HOST_NAME,
    INSTANCE_NAME,
    TO_CHAR (STARTUP_TIME, 'DD-MON-YYYY HH24:MI:SS') AS "Started At(CT)",
    status,
    (SELECT OPEN_MODE FROM v$database) "Open Mode",
    logins,
    database_status,
    FLOOR (SYSDATE - STARTUP_TIME)
    || ' DAYS(S) '
    || TRUNC (
    24
    * ( (SYSDATE - STARTUP_TIME) - TRUNC (SYSDATE - STARTUP_TIME)))
    || ' HOUR(S) '
    || MOD (
    TRUNC (
    1440
    * ( (SYSDATE - STARTUP_TIME) - TRUNC (SYSDATE - STARTUP_TIME))),
    60)
    || ' MINUTE(S) '
    || MOD (
    TRUNC (
    86400
    * ( (SYSDATE - STARTUP_TIME) - TRUNC (SYSDATE - STARTUP_TIME))),
    60)
    || ' SECONDS'
    UPTIME
    FROM sys.V_$INSTANCE

    ReplyDelete