Pages

Wednesday, April 28, 2021

LEFT JOIN vs INNER JOIN vs WHERE

=====================
General
=====================
LEFT JOIN vs INNER JOIN vs Regular WHERE
 
=====================
Example
=====================
Consider a table of EMP and DEPT where not all EMP entries have an entry in DEPT
How do you return all entries in EMP + Related entries for a specific entry in DEPT?

All there SQLs are the same.
INNER JOIN is the same as using WHERE

SELECT emp.*, dept.dname, dept.loc
  FROM emp
INNER JOIN dept
ON emp.deptno = dept.deptno
       AND dept.dname = 'ACCOUNTING';


SELECT emp.*, dept.dname, dept.loc
FROM emp
INNER JOIN dept
ON emp.deptno = dept.deptno
WHERE dept.dname = 'ACCOUNTING';

SELECT emp.*, dept.dname, dept.loc
FROM emp, dept
WHERE emp.deptno = dept.deptno
AND dept.dname = 'ACCOUNTING';



empno ename  job  mgr   sal    deptno dname  loc
----- ------ ----------- ----- ------ ------ ----------- ----------
7839  KING   PRESIDENT          5000   10 ACCOUNTING  NEW YORK
7782  CLARK  MANAGER   
7839    2450        10   ACCOUNTING    NEW YORK

LEFT JOIN is NOT the same as using WHERE
SELECT emp.*, dept.dname, dept.loc
FROM emp
LEFT JOIN dept
ON emp.deptno = dept.deptno
WHERE dept.dname = 'ACCOUNTING';


empno ename  job  mgr   sal    deptno dname  loc
----- ------ ----------- ----- ------ ------ ----------- ----------
7839  KING   PRESIDENT          5000   10 ACCOUNTING  NEW YORK
7782  CLARK  MANAGER   
7839    2450        10   ACCOUNTING    NEW YORK


SELECT emp.*, dept.dname, dept.loc
FROM emp
LEFT JOIN dept
ON emp.deptno = dept.deptno
AND dept.dname = 'ACCOUNTING';



empno ename  job  mgr   sal    deptno  dname   loc
----- ------ ----------- ----- ------ ------  ----------- ----------
7839  KING   PRESIDENT          5000   10  ACCOUNTING  NEW YORK
7698  BLAKE  MANAGER   7839   
2850     30
7782  CLARK  MANAGER   7839  2450     10  ACCOUNTING  NEW YORK
 566  JONES  MANAGER   7839  2975   20
7788  SCOTT  ANALYST      7566   3000     20
7902   FORD  ANALYST      7566   3000     20


Monday, April 19, 2021

Error ORA-01000 Too many open cursors is coming, and it is not clear why.

==========
General
==========
Error ORA-01000 Too many open cursors is coming, and it is not clear why.
Code below is logging number of cursors for current session, and can be called from various places in code.


CREATE SEQUENCE DEBUG_OPN_CURSORS_ID_SEQ NOMAXVALUE MINVALUE 1 NOCYCLE NOCACHE; 

CREATE TABLE DEBUG_OPEN_CURSORS (
  entry_id   NUMBER,
  sid        NUMBER,
  machine    VARCHAR2(64 BYTE),
  user_name  VARCHAR2(30 BYTE),
  sql_id     VARCHAR2(13 BYTE), 
  sql_text   VARCHAR2(1000 BYTE),
  open_cursors NUMBER,
  comments   VARCHAR2(1000 BYTE)  
  )
TABLESPACE IGT_TABLE ;
  
AS SYS !!  
GRANT SELECT ON SYS.V_$OPEN_CURSOR TO SPARX_700;
GRANT SELECT ON SYS.V_$SESSION TO SPARX_700;
GRANT SELECT ON SYS.V_$SQLAREA TO SPARX_700;

-----------------------
--procedure write_cursors
-----------------------
create or replace procedure write_cursors(p_comments IN VARCHAR2, p_username IN VARCHAR2)  is
pragma autonomous_transaction;

begin
  
  INSERT INTO DEBUG_OPEN_CURSORS (entry_id, sid, machine, user_name, sql_id, sql_text, open_cursors, comments)
  SELECT DEBUG_OPN_CURSORS_ID_SEQ.nextval, sid, machine, user_name, TOP_OPEN_CURSORS_SESSIONS.sql_id, V$SQLAREA.sql_text, open_cursors, p_comments
   FROM (
  --Get sessions with open cursors 
  SELECT SESS.sid sid,  
         SESS.machine machine,  
         OPEN_CURS.user_name user_name,        
         OPEN_CURS.sql_id sql_id,
         count(*) open_cursors
  FROM v$open_cursor OPEN_CURS, 
       v$session SESS
  WHERE OPEN_CURS.saddr = SESS.saddr
    AND SESS.username IS NOT NULL  AND SESS.username NOT IN ('SYS')  
    AND (p_username IS NULL OR (p_username IS NOT NULL AND SESS.username = p_username ))    
    AND OPEN_CURS.user_name IS NOT NULL AND OPEN_CURS.user_name NOT IN ('SYS') 
    AND (p_username IS NULL OR (p_username IS NOT NULL AND OPEN_CURS.user_name = p_username ))
  GROUP BY SESS.sid,  SESS.machine, OPEN_CURS.user_name, OPEN_CURS.sql_id
  HAVING COUNT(1) > 1
  ) TOP_OPEN_CURSORS_SESSIONS,
  V$SQLAREA
  WHERE TOP_OPEN_CURSORS_SESSIONS.sql_id = V$SQLAREA.sql_id(+)
  ;  
  commit;
end write_cursors;
-----------------------
 
BEGIN
 write_cursors('Step A', 'MY_SCHEMA');
 write_cursors('Step B', 'MY_SCHEMA');
 write_cursors('Step C', 'MY_SCHEMA');
END;
/



====================
Get SQL Text of current running SQLs
====================
SELECT sql_text, sql_id, executions, fetches, end_of_fetch_count
FROM V$SQLAREA 
WHERE sql_id IN (
SELECT NVL(sql_id,prev_sql_id) sql_id
 FROM (
SELECT V_SESSTAT.value OPEN_CURSORS,
       V_SESSION.username,
       V_SESSION.sid||'-'||V_SESSION.serial# sid_serial,
       V_PROCESS.spid,
       V_SESSION.machine,
       V_SESSION.program,
       V_SESSION.sql_id,
       V_SESSION.prev_sql_id       
  FROM V$SESSTAT V_SESSTAT,
       V$STATNAME V_STATNAME,
       V$SESSION V_SESSION,
       V$PROCESS V_PROCESS
 WHERE V_SESSTAT.statistic# = V_STATNAME.statistic#
   AND V_PROCESS.addr=V_SESSION.paddr
   AND V_SESSTAT.sid = V_SESSION.sid
   AND V_STATNAME.name = 'opened cursors current'
   AND V_SESSTAT.value > 1
   )
)

Executions - Total number of executions, totalled over all the child cursors
Fetches - Number of fetches associated with the SQL statement
End of Fetch Count - Number of times this cursor was fully executed
                     The value of this statistic is not incremented when the cursor is partially executed, 
     either because it failed during the execution or 
     because only the first few rows produced by this cursor are 
                     fetched before the cursor is closed or re-executed.

================================
A script to sample open cursors per session
================================
gen_cursors_list.sql
COL sid FOR A10
COL machine FOR A30
COL program FOR A30
col USER_NAME FOR A30
COL sql_id FOR A20
COL sql_text FOR A200
COL run_date FOR A20
COL open_cursors FOR 99999999
SET PAGESIZE 1000
SET LINESIZE 1000
SET heading on
SET feedback off

spool open_cursors_list.txt append

SELECT sid, 
       spid, 
       open_cursors, 
       machine, 
       program, 
       user_name, 
       TOP_OPEN_CURSORS_SESSIONS.sql_id, 
       V$SQLAREA.sql_text, 
       TO_CHAR(SYSDATE,'YYYYMMDD hh24:mi:ss') as run_date
   FROM (
  --Get sessions with open cursors
  SELECT SESS.sid sid,
         PROC.spid spid,
         SESS.machine machine,
         SESS.program program,
         OPEN_CURS.user_name user_name,
         OPEN_CURS.sql_id sql_id,
         count(*) open_cursors
  FROM v$open_cursor OPEN_CURS,
       v$session SESS,
       v$process PROC
  WHERE OPEN_CURS.saddr = SESS.saddr
    AND SESS.username IS NOT NULL AND SESS.username NOT IN ('SYS')
    AND OPEN_CURS.user_name IS NOT NULL AND OPEN_CURS.user_name NOT IN ('SYS')
    AND PROC.addr=SESS.paddr
  GROUP BY SESS.sid, PROC.spid, SESS.machine, SESS.program, OPEN_CURS.user_name, OPEN_CURS.sql_id
  HAVING COUNT(1) > 10
  ) TOP_OPEN_CURSORS_SESSIONS,
  V$SQLAREA
  WHERE TOP_OPEN_CURSORS_SESSIONS.sql_id = V$SQLAREA.sql_id(+);
spool off
EXIT;

gen_cursors_list.sh
#!/bin/bash
. /etc/sh/orash/oracle_login.sh igt
sqlplus DEU_TDGQQ_SBQQQ/"Deu_#123_SBQQQ$"@rraf @gen_cursors_list.sql

Tuesday, April 13, 2021

A Function that return a table

=====================
A Function that return a table
=====================

--CREATE OR REPLACE TYPE myTableType   AS TABLE OF myObjectFormat
--Create the types to support the table function.
--DROP TYPE t_network_id_row;
--DROP TYPE t_network_id_tab;

--CREATE TYPE t_network_id_row AS OBJECT (
--  id           NUMBER
--);
--/

--CREATE TYPE t_network_id_tab IS TABLE OF t_network_id_row;
--/

--SELECT id FROM   TABLE(test_function('SGA_W')) ORDER BY id DESC;

CREATE OR REPLACE FUNCTION test_function (p_table_name         IN USER_TABLES.table_name%type) RETURN t_network_id_tab AS

  CURSOR get_tables_cur (cp_prefix IN VARCHAR2) IS
  SELECT table_name
   FROM USER_TABLES
  WHERE SUBSTR(table_name,1,LENGTH(cp_prefix)) = cp_prefix;
  v_ret_number    NUMBER;

  l_tab  t_network_id_tab := t_network_id_tab();
  
BEGIN
  v_ret_number := 0;
  FOR get_tables_rec IN get_tables_cur(p_table_name) LOOP
    v_ret_number := v_ret_number + 1;    
    l_tab.extend;
    l_tab(l_tab.last) := T_NETWORK_ID_ROW(1);
  END LOOP;

  RETURN l_tab;
END test_function;


Monday, April 12, 2021

cmd bat script to delete files in a directory olden than X days

@echo OFF

REM cd D:\SfiGen\ArchiveDir
SET DEL_DIR=D:\SfiGen\ArchiveDir
SET WORK_DIR=
D:\SfiGen\scripts
SET KEEP_DAYS=30

ECHO start delete files from  %DEL_DIR% older than %KEEP_DAYS% days

cd %DEL_DIR%
forfiles /p %DEL_DIR% /s /d -%KEEP_DAYS% /c "cmd /c del /Q @file"
ECHO Files Deleted
cd %WORK_DIR%


Thursday, April 8, 2021

Golden Gate Extracts are in ABENDED state. Error: Missing Log File WAITING FOR REDO: FILE NA


Golden Gate Extracts are in ABENDED state with error:

ERROR   OGG-02870  Missing Log File WAITING FOR REDO: FILE NA, THREAD 1, SEQUENCE 0, SCN 0x00000003465f1e7d. Read Position SCN: 3.1180639857 (14065541745).
ERROR   OGG-01668  PROCESS ABENDING.

The Integrated Extract is "looking" for a archive log which was deleted, and cannot be recovered.

Option 1.
Start Integrated Extract now, need to unregister + register Extract.
This would mean loosing old transactions.

DBLOGIN USERID OGG PASSWORD XXXXXX
STOP EXTRACT EXT_S_01
UNREGISTER EXTRACT EXT_S_01 DATABASE
DELETE EXTRACT EXT_S_01
REGISTER EXTRACT EXT_S_01 DATABASE
ADD EXTRACT EXT_S_01 INTEGRATED TRANLOG, BEGIN NOW
ADD EXTTRAIL /software/ogg/191/dirdat/01/out/es EXTRACT EXT_S_01
START EXTRACT EXT_S_01


Option 2 - If archive log files are still available, restore archive logs via RMAN, and start Extract. But it might not be available, as in this example:

oracle@my_server:/oracle_db/db2/db_igt/arch>% rman target /

Recovery Manager: Release 12.2.0.1.0 - Production on Sun Jun 27 19:34:04 2021

Copyright (c) 1982, 2017, Oracle and/or its affiliates.  All rights reserved.

connected to target database: IGT (DBID=1255688165)

RMAN> RESTORE ARCHIVELOG FROM SEQUENCE 297470 UNTIL SEQUENCE 297501;

Starting restore at 27-JUN-21
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=606 device type=DISK

archived log for thread 1 with sequence 297501 is already on disk as file /oracle_db/db2/db_igt/arch/arch0001_297501_1017403749.arc
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 06/27/2021 19:34:12
RMAN-06026: some targets not found - aborting restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297500 and starting SCN of 28088222868 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297499 and starting SCN of 28088222153 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297498 and starting SCN of 28088220469 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297497 and starting SCN of 28088219284 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297496 and starting SCN of 28088218181 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297495 and starting SCN of 28088217156 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297494 and starting SCN of 28088216516 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297493 and starting SCN of 28088111765 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297492 and starting SCN of 28088068835 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297491 and starting SCN of 28088067820 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297490 and starting SCN of 28088066631 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297489 and starting SCN of 28088065907 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297488 and starting SCN of 28088022437 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297487 and starting SCN of 28088021554 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297486 and starting SCN of 28088020909 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297485 and starting SCN of 28088020341 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297484 and starting SCN of 28088018858 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297483 and starting SCN of 28088015844 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297482 and starting SCN of 28087942561 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297481 and starting SCN of 28087891891 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297480 and starting SCN of 28087885099 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297479 and starting SCN of 28087881524 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297478 and starting SCN of 28087874736 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297477 and starting SCN of 28087861817 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297476 and starting SCN of 28087855120 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297475 and starting SCN of 28087804793 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297474 and starting SCN of 28087802216 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297473 and starting SCN of 28087790188 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297472 and starting SCN of 28087787782 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297471 and starting SCN of 28087774056 found to restore
RMAN-06025: no backup of archived log for thread 1 with sequence 297470 and starting SCN of 28087770060 found to restore