Vous êtes sur la page 1sur 10

DATABASE SCRIPTS

How to get the bind values of a sql query


201 views Less than a minute 0
If you have the sql_id of the sql query , then you can get the bind values of the bind variables, from
v$sql_bind_capture. Script:

1
2
3 SELECT
4 sql_id,
5 b. LAST_CAPTURED,
6 t.sql_text sql_text,
7 b.HASH_VALUE,
8 b.name bind_name,
9 b.value_string bind_value
10 FROM
11 gv$sql t
12 JOIN
13 gv$sql_bind_capture b using (sql_id)
14 WHERE
15 b.value_string is not null
16 AND
17 sql_id='&sqlid'
18 /
19

How to find current running sqls in oracle


373 views Less than a minute 1
Use below script to get the current running sql details.

1
2
3 select sesion.sid,
4 sesion.username,
5 optimizer_mode,
6 hash_value,
7 address,
8 cpu_time,
9 elapsed_time,
10 sql_text
11 from v$sqlarea sqlarea, v$session sesion
12 where sesion.sql_hash_value = sqlarea.hash_value
13 and sesion.sql_address = sqlarea.address
14 and sesion.username is not null;
15
Output:
1
2
3 SID USERNAME OPTIMIZER_ HASH_VALUE ADDRESS
4 ---------- ------------------------------ ---------- ---------- ----------------
5 CPU_TIME ELAPSED_TIME
6 ---------- ------------
7 SQL_TEXT
8 --------------------------------------------------------------------------------
9 1993 SYS ALL_ROWS 3727742172 000000041AE77AD8
10 817728 817753
11 select sesion.sid, sesion.username, optimizer_mode, hash_
12 value, address, cpu_time, elapsed_time, sql_text f
13 rom v$sqlarea sqlarea, v$session sesion where sesion.sql_hash_value = sqlarea.h
14 ash_value and sesion.sql_address = sqlarea.address and sesion.username
15 is not null
16

Tablespace monitoring in oracle 12c multitenant


database.
552 views Less than a minute 0

You can use this script to check tablespace space details in 12c Multitenant database. It will provide
information of both root CDB and PDB.

1
2
3 SET LINES 132 PAGES 100
4 COL con_name FORM A15 HEAD "Container|Name"
5 COL tablespace_name FORM A15
6 COL fsm FORM 999,999,999,999 HEAD "Free|Space Meg."
7 COL apm FORM 999,999,999,999 HEAD "Alloc|Space Meg."
8 --
9 COMPUTE SUM OF fsm apm ON REPORT
10 BREAK ON REPORT ON con_id ON con_name ON tablespace_name
11 --
12 WITH x AS (SELECT c1.con_id, cf1.tablespace_name, SUM(cf1.bytes)/1024/1024 fsm
13 FROM cdb_free_space cf1
14 ,v$containers c1
15 WHERE cf1.con_id = c1.con_id
16 GROUP BY c1.con_id, cf1.tablespace_name),
17 y AS (SELECT c2.con_id, cd.tablespace_name, SUM(cd.bytes)/1024/1024 apm
18 FROM cdb_data_files cd
19 ,v$containers c2
20 WHERE cd.con_id = c2.con_id
21 GROUP BY c2.con_id
22 ,cd.tablespace_name)
23 SELECT x.con_id, v.name con_name, x.tablespace_name, x.fsm, y.apm
24 FROM x, y, v$containers v
25 WHERE x.con_id = y.con_id
26 AND x.tablespace_name = y.tablespace_name
27 AND v.con_id = y.con_id
28 UNION
29 SELECT vc2.con_id, vc2.name, tf.tablespace_name, null, SUM(tf.bytes)/1024/1024
30 FROM v$containers vc2, cdb_temp_files tf
31 WHERE vc2.con_id = tf.con_id
32 GROUP BY vc2.con_id, vc2.name, tf.tablespace_name
33 ORDER BY 1, 2;
34
OUTPUT

1
2
3 Container Free Alloc
4 CON_ID Name TABLESPACE_NAME Space Meg. Space Meg.
5 ---------- --------------- --------------- ---------------- ----------------
6 1 CDB$ROOT CHARSETCONVERSI 199 200
7 ON
8
9 PRODUCING 99 100
10 SYSAUX 382 920
11 SYSTEM 3 810
12 TEMP 197
13 TEST_ENCRY 2,047 2,048
14 UNDOTBS1 360 405
15 USERS 4 5
16 5 PROD_MN SYSAUX 253 570
17 SYSTEM 1 270
18 TEMP 20
19 ********** *************** *************** ---------------- ----------------
20 sum 3,347 5,545
21
22 11 rows selected.
23

Realtime monitoring of sql query using


v$sql_plan_monitor
558 views Less than a minute 0

V$SQL_PLAN_MONITOR displays real time plan level monitoring statistics for the currently running
sql queries.. Each row in V$SQL_PLAN_MONITOR corresponds to an operation of the execution plan being
monitored.

1
2
3 --- sql_is from v$sql_monitor
4
5 SELECT sql_id FROM v$sql_monitor;
6
7 SQL_ID
8 -------
9 00tr6c6tngp4x
10
Use below query to get the plan details:

1
2
3 SELECT sid, sql_id, status, plan_line_id,
4 plan_operation || ' ' || plan_options operation, output_rows
5 FROM v$sql_plan_monitor
6 WHERE status not like '%DONE%'
7 ORDER BY 1,4;
8
Output

1
2
3 SID SQL_ID STATUS LINE OPERATION ROWS
4 ---------- ------------- ---------- ----- ------------------------- ----------
5 82 00tr6c6tngp4x EXECUTING 0 INSERT STATEMENT 0
6 1 LOAD TABLE CONVENTIONAL 0
7 2 HASH UNIQUE 0
8 3 HASH JOIN ANTI 0
9 4 NESTED LOOPS ANTI 0
10 5 STATISTICS COLLECTOR 4293890583
11 6 HASH JOIN 4293890583
12 7 NESTED LOOPS 1
13 8 STATISTICS COLLECTOR 1565871
14 9 MERGE JOIN CARTESIAN 1565871
15 10 INDEX RANGE SCAN 5381
16 11 BUFFER SORT 1565871
17 12 INDEX RANGE SCAN 291
18 13 INDEX RANGE SCAN 0
19 14 INDEX FAST FULL SCAN 1860196
20 15 INDEX RANGE SCAN 69
21 16 INDEX RANGE SCAN 0
22
23 17 rows selected.
24
Top 5 cached sql statements by elapsed time in
oracle
811 views Less than a minute 0

Get the Top 10 cached sql statements details

1
2
3 SELECT sql_id,child_number,sql_text, elapsed_time
4 FROM (SELECT sql_id, child_number, sql_text, elapsed_time, cpu_time,
5 disk_reads,
6 RANK () OVER (ORDER BY elapsed_time DESC) AS elapsed_rank
7 FROM v$sql)
8 WHERE elapsed_rank <= 5
9

How to find sql text from a sid


602 views Less than a minute 1

Use below query to get the sql text of a particular sid. When the below script asks for input, enter the SID.

1
2
3 col sql_text form a80
4 set lines 120
5 select sql_text from gv$sqltext where hash_value=
6 (select sql_hash_value from gv$session where sid=&1)
7 order by piece
8 /
9
10
11 SQL> SQL> 2 3 4
12 Enter value for 1: 285
13 old 2: (select sql_hash_value from gv$session where sid=&1)
14 new 2: (select sql_hash_value from gv$session where sid=285)
15
16 SQL_TEXT
17 --------------------------------------------------------------------------------
18 select sql_text from gv$sqltext where hash_value= (select sql_ha
19 sh_value from gv$session where sid=285) order by piece
20
21 2 rows selected.
22

How to find active sessions in oracle database


400 views Less than a minute 2

Use below script to find active sessions in oracle database.


1
2
3 set echo off
4 set linesize 95
5 set head on
6 set feedback on
7 col sid head "Sid" form 9999 trunc
8 col serial# form 99999 trunc head "Ser#"
9 col username form a8 trunc
10 col osuser form a7 trunc
11 col machine form a20 trunc head "Client|Machine"
12 col program form a15 trunc head "Client|Program"
13 col login form a11
14 col "last call" form 9999999 trunc head "Last Call|In Secs"
15 col status form a6 trunc
16 select sid,serial#,substr(username,1,10) username,substr(osuser,1,10) osuser,
17 substr(program||module,1,15) program,substr(machine,1,22) machine,
18 to_char(logon_time,'ddMon hh24:mi') login,
19 last_call_et "last call",status
20 from v$session where status='ACTIVE'
21 order by 1
22 /
23

How to get the size of an oracle database


681 views Less than a minute 1

Use below query to get the size of a database.

1
2
3 col "Database Size" format a20
4 col "Free space" format a20
5 col "Used space" format a20
6 select round(sum(used.bytes) / 1024 / 1024 / 1024 ) || ' GB' "Database Size"
7 , round(sum(used.bytes) / 1024 / 1024 / 1024 ) -
8 round(free.p / 1024 / 1024 / 1024) || ' GB' "Used space"
9 , round(free.p / 1024 / 1024 / 1024) || ' GB' "Free space"
10 from (select bytes
11 from v$datafile
12 union all
13 select bytes
14 from v$tempfile
15 union all
16 select bytes
17 from v$log) used
18 , (select sum(bytes) as p
19 from dba_free_space) free
20 group by free.p
21 /
22
1
2
3 Database Size Used space Free space
4 -------------------- -------------------- --------------------
5 8 GB 5 GB 3 GB
6

Query to get child table and foreign key status


331 views Less than a minute 1

Use below query to get child key and foreign key status:

1
2
col owner form a10
3
col FK_column form a15
4
col table_name form a30
5
select b.owner, b.table_name child_table,b.status,
6
c.column_name FK_column, b.constraint_name
7
from dba_constraints a, dba_constraints b, dba_cons_columns c
8
where a.owner=b.r_owner
9
and b.owner=c.owner
10
and b.table_name=c.table_name
11
and b.constraint_name=c.constraint_name
12
and a.constraint_name=b.r_constraint_name
13
and b.constraint_type='R'
14
and a.owner='&OWNER'
15
and a.table_name='&TABLE_NAME'
16
and a.CONSTRAINT_TYPE='P';
17
18
19
OWNER CHILD_TABLE STATUS FK_COLUMN CONSTRAINT_NAME
20
---------- ------------------------------ -------- --------------- ------------------------------
21
DDR MDT_RM_DEVICE_TRAND DISABLED DEV_TRAND_STATU
22
MDT_RM_DACID_TRAND_STATUS_FK
23
S_ID
24

Enable constraints:

1
2
3 alter table FDR.MDT_RM_CALL_CLOSEOUT_METER disable constraints MDT_RM_CALL_CLOSEOUT_METER_FK;
4
5 Table altered.
6
Find the parent table of child table:

1
2
3 SELECT owner,
4 table_name
5 FROM dba_constraints
6 WHERE constraint_name IN (SELECT r_constraint_name
7 FROM dba_constraints WHERE
8 table_name = 'PUROLATOR_ACCOUNTS' AND constraint_type = 'R');
9

scripts to monitor standby database


800 views Less than a minute 1

FIND THE ARCHIVE LAG BETWEEN PRIMARY AND STANDBY:

1
2
3 select LOG_ARCHIVED-LOG_APPLIED "LOG_GAP" from
4 (SELECT MAX(SEQUENCE#) LOG_ARCHIVED
5 FROM V$ARCHIVED_LOG WHERE DEST_ID=1 AND ARCHIVED='YES'),
6 (SELECT MAX(SEQUENCE#) LOG_APPLIED
7 FROM V$ARCHIVED_LOG WHERE DEST_ID=2 AND APPLIED='YES');
8
CHECK THE STATUS OF DIFFERENT PROCESS:

1
2
3 SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY ;
4
5 PROCESS STATUS THREAD# SEQUENCE# BLOCK# BLOCKS
6 --------- ------------ ---------- ---------- ---------- ----------
7 ARCH CONNECTED 0 0 0 0
8 ARCH CONNECTED 0 0 0 0
9 ARCH CONNECTED 0 0 0 0
10 ARCH CONNECTED 0 0 0 0
11 MRP0 WAIT_FOR_LOG 1 53056 0 0
12 RFS IDLE 0 0 0 0
13 RFS IDLE 0 0 0 0
14 RFS IDLE 1 53056 10935 2
15 RFS IDLE 0 0 0 0
16
17 9 rows selected.
18
LAST SEQUENCE RECEIVED AND LAST SEQUENCE APPLIED:

1
2
3 SQL> SELECT al.thrd "Thread", almax "Last Seq Received", lhmax "Last Seq Applied" FROM
4 2 (select thread# thrd, MAX(sequence#) almax FROM v$archived_log WHERE resetlogs_change#=(SELECT
5 resetlogs_change#
6 3 FROM v$database) GROUP BY thread#) al, (SELECT thread# thrd, MAX(sequence#) lhmax FROM v$log_history
7 WHERE resetlogs_change#=(SELECT resetlogs_change# FROM v$database) GROUP BY thread#) lh WHERE al.thrd =
8 lh.thrd; 4
9
10 Thread Last Seq Received Last Seq Applied
11 ---------- ----------------- ----------------
1 49482 49482

CHECK THE MESSAGES/ERRORS IN STNADBY DATABASE:

1
2
3 set pagesize 2000
4 set lines 2000
5 col MESSAGE for a90
6 select message,timestamp from V$DATAGUARD_STATUS where timestamp > sysdate - 1/6;
7 MESSAGE TIMESTAMP
8 ------------------------------------------------------------------------------------------ ---------
9 RFS[48]: No standby redo logfiles created 05-AUG-15
10 Media Recovery Log /uv1010/arch/MRSX/arch_MRSX_779539386_1_49481.log 05-AUG-15
11 Media Recovery Waiting for thread 1 sequence 49482 (in transit) 05-AUG-15
12 RFS[48]: No standby redo logfiles created 05-AUG-15
13 Media Recovery Log /uv1010/arch/MRSX/arch_MRSX_779539386_1_49482.log 05-AUG-15
14 Media Recovery Waiting for thread 1 sequence 49483 (in transit) 05-AUG-15
15
16 6 rows selected.
17
CHECK THE NUMBER OF ARCHIVES GETTING GENERATING ON HOURLY BASIS:

1
2
3 SELECT TO_CHAR(TRUNC(FIRST_TIME),'Mon DD') "DG Date",
4 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'00',1,0)),'9999') "12AM",
5 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'01',1,0)),'9999') "01AM",
6 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'02',1,0)),'9999') "02AM",
7 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'03',1,0)),'9999') "03AM",
8 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'04',1,0)),'9999') "04AM",
9 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'05',1,0)),'9999') "05AM",
10 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'06',1,0)),'9999') "06AM",
11 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'07',1,0)),'9999') "07AM",
12 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'08',1,0)),'9999') "08AM",
13 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'09',1,0)),'9999') "09AM",
14 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'10',1,0)),'9999') "10AM",
15 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'11',1,0)),'9999') "11AM",
16 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'12',1,0)),'9999') "12PM",
17 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'13',1,0)),'9999') "1PM",
18 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'14',1,0)),'9999') "2PM",
19 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'15',1,0)),'9999') "3PM",
20 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'16',1,0)),'9999') "4PM",
21 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'17',1,0)),'9999') "5PM",
22 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'18',1,0)),'9999') "6PM",
23 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'19',1,0)),'9999') "7PM",
24 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'20',1,0)),'9999') "8PM",
25 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'21',1,0)),'9999') "9PM",
26 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'22',1,0)),'9999') "10PM",
27 TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,'HH24'),'23',1,0)),'9999') "11PM"
28 FROM V$LOG_HISTORY
29 GROUP BY TRUNC(FIRST_TIME)
30 ORDER BY TRUNC(FIRST_TIME) DESC
31 /
32