понедельник, 31 июля 2017 г.

Determine How Long a Session has been Idle using v$session

Following SQL using information from LAST_CALL_ET:
  1. select s.*
  2. --select 'ALTER SYSTEM KILL SESSION ''' || s.sid || ',' || s.serial# || ''' IMMEDIATE;' kill_serial_sid
  3. from (
  4. SELECT s.sid,s.serial#,s.username
  5. ,s.status
  6. ,substr(s.machine,1,10)
  7. ,s.osuser,s.module
  8. ,to_char(logon_time, 'mm/dd/yy hh24:mi:ss') logon_time
  9. -- idle time
  10. ,to_dsinterval(
  11. -- days separately
  12. substr('0'||trunc(last_call_et/86400),-2,2) || ' ' ||
  13. -- hours
  14. substr('0'||trunc(mod(last_call_et,86400)/3600),-2,2) || ':' ||
  15. -- minutes
  16. substr('0'||trunc(mod(mod(last_call_et,86400),3600)/60),-2,2) || ':' ||
  17. --seconds
  18. substr('0'||mod(mod(mod(last_call_et,86400),3600),60),-2,2)
  19. ) idle_time
  20. FROM v$session s, v$process p
  21. WHERE s.username IS NOT NULL
  22. -- use outer join to show sniped sessions in
  23. -- v$session that don''t have an OS process
  24. AND p.addr(+) = s.paddr
  25. ) s
  26. where 1 = 1
  27. and s.status = 'INACTIVE'
  28. and s.idle_time >= to_dsinterval('1 00:00:00')
  29. ORDER BY idle_time desc;
Note: LAST_CALL_ET - If the session STATUS is currently ACTIVE, then the value represents the elapsed time in seconds since the session has become active.
If the session STATUS is currently INACTIVE, then the value represents the elapsed time in seconds since the session has become inactive.

Search Idle Sessions using filter in Enterprise Manager

You can specify search criteria using WHERE clause

as following:
  1. 1 = 1
  2. and username IS NOT NULL
  3. and status = 'INACTIVE'
  4. and to_dsinterval(
  5. -- days separately
  6. substr('0'||trunc(last_call_et/86400),-2,2) || ' ' ||
  7. -- hours
  8. substr('0'||trunc(mod(last_call_et,86400)/3600),-2,2) || ':' ||
  9. -- minutes
  10. substr('0'||trunc(mod(mod(last_call_et,86400),3600)/60),-2,2) || ':' ||
  11. --seconds
  12. substr('0'||mod(mod(mod(last_call_et,86400),3600),60),-2,2)
  13. ) >= to_dsinterval('1 00:00:00')

среда, 7 июня 2017 г.

Search values in all columns of all tables

Улучшил процедуру whereIsValue из статьи  SQL: Поиск значения по всем колонкам всех таблиц:

  • Добавил параметр schemaName для поиска в конкретной схеме
  • Заменил user_ views на all_ чтобы была возможность поиска по любой схеме
  • Обернул в кавычки "' || columnName || '" так как столкнулся с ситуацией когда колонка называлась зарезервированным словом "DATE" 
  1. create or replace procedure whereIsValue(p_schemaName varchar2, p_searchValue varchar2)
  2. AS
  3. TYPE VALCUR IS REF CURSOR;
  4. cursor tabl(c_schemaName varchar2) is select table_name from all_tables where owner = c_schemaName order by table_name;
  5. cursor col (c_schemaName varchar2, c_tablename varchar2) is select column_name from all_tab_columns where owner = c_schemaName and table_name like c_tableName;
  6. valueCursor VALCUR;
  7. tableName varchar2(50);
  8. columnName varchar2(50);
  9. columnValue varchar2(500);
  10. begin
  11. open tabl(p_schemaName);
  12. LOOP
  13. fetch tabl into tableName;
  14. EXIT WHEN tabl%NOTFOUND;
  15. --dbms_output.put_line('Search in table - ' || tableName);
  16. OPEN col(p_schemaName, tableName);
  17. LOOP
  18. fetch col into columnName;
  19. EXIT WHEN col%NOTFOUND;
  20. --dbms_output.put_line('Search in column - ' || columnName);
  21. OPEN valueCursor for 'select "' || columnName || '" from ' || p_schemaName || '."' || tableName || '"';
  22. LOOP
  23. BEGIN
  24. fetch valueCursor into columnValue;
  25. EXIT WHEN valueCursor%NOTFOUND;
  26. if (columnValue like p_searchValue) then
  27. dbms_output.put_line('Found in table - ' || tableName || ' and column - ' || columnName);
  28. exit;
  29. end if;
  30. EXCEPTION
  31. WHEN OTHERS then
  32. NULL;
  33. END;
  34. END LOOP;
  35. CLOSE valueCursor;
  36. END LOOP;
  37. CLOSE col;
  38. END LOOP;
  39. end;
  40. /
Примеры вызова:
  1. sqlplus /nolog
  2. conn system
  3.  
  4. SET SERVEROUTPUT ON
  5.  
  6. exec whereIsValue('SCOTT', '1496765408852');
  7. exec whereIsValue('SCOTT', 'CLERK');
  8. exec whereIsValue('SCOTT', '%JONES%');