Skip to content

Instantly share code, notes, and snippets.

@joseporiol
joseporiol / last_sql_by_user.sql
Created January 21, 2014 10:19
Last SQL fired by the User on Database
SELECT S.USERNAME || '(' || s.sid || ')-' || s.osuser UNAME,
s.program || '-' || s.terminal || '(' || s.machine || ')' PROG,
s.sid || '/' || s.serial# sid,
s.status "Status",
p.spid,
sql_text sqltext
FROM v$sqltext_with_newlines t, V$SESSION s, v$process p
WHERE t.address = s.sql_address
AND p.addr = s.paddr(+)
AND t.hash_value = s.sql_hash_value
@joseporiol
joseporiol / cpu_usage.sql
Created January 21, 2014 10:20
CPU usage of the user
SELECT ss.username, se.SID, VALUE / 100 cpu_usage_seconds
FROM v$session ss, v$sesstat se, v$statname sn
WHERE se.STATISTIC# = sn.STATISTIC#
AND NAME LIKE '%CPU used by this session%'
AND se.SID = ss.SID
AND ss.status = 'ACTIVE'
AND ss.username IS NOT NULL
ORDER BY VALUE DESC;
@joseporiol
joseporiol / long_query_progress.sql
Created January 21, 2014 10:20
Long query progress on database
SELECT a.sid,
a.serial#,
b.username,
opname OPERATION,
target OBJECT,
TRUNC (elapsed_seconds, 5) "ET (s)",
TO_CHAR (start_time, 'HH24:MI:SS') start_time,
ROUND ( (sofar / totalwork) * 100, 2) "COMPLETE (%)"
FROM v$session_longops a, v$session b
WHERE a.sid = b.sid
@joseporiol
joseporiol / reads_per_execution.sql
Created January 21, 2014 10:21
Find Top 10 SQL by reads per execution
SELECT *
FROM ( SELECT ROWNUM,
SUBSTR (a.sql_text, 1, 200) sql_text,
TRUNC (
a.disk_reads / DECODE (a.executions, 0, 1, a.executions))
reads_per_execution,
a.buffer_gets,
a.disk_reads,
a.executions,
a.sorts,
@joseporiol
joseporiol / open_conn_by_program.sql
Created January 21, 2014 10:22
Oracle SQL query that show the opened connections group by the program that opens the connection.
SELECT program application, COUNT (program) Numero_Sesiones
FROM v$session
GROUP BY program
ORDER BY Numero_Sesiones DESC;
@joseporiol
joseporiol / search_on_packages.sql
Created January 21, 2014 10:23
Find string in package source code Below query will search for string ‘FOO_SOMETHING’ in all package source. This query comes handy when you want to find a particular procedure or function call from all the source code.
--search a string foo_something in package source code
SELECT *
FROM dba_source
WHERE UPPER (text) LIKE '%FOO_SOMETHING%'
AND owner = 'USER_NAME';
@joseporiol
joseporiol / random.sql
Created January 21, 2014 10:25
Random number Generator on Oracle
--generate random number between 0 and 100
SELECT ROUND (DBMS_RANDOM.VALUE () * 100) + 1 AS random_num FROM DUAL;
@joseporiol
joseporiol / FilesAndDirectoryListing.java
Created January 21, 2014 10:33
Files and directory listing
File dir = new File("directoryName");
String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i < children.length; i++) {
// Get filename of file or directory
String filename = children[i];
}
}
@joseporiol
joseporiol / BytesToString.java
Created January 21, 2014 14:46
Bytes to String
public class BytesToString {
public static String bytes2String(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X ", b).trim());
}
return sb.toString();
@joseporiol
joseporiol / CharFormatConverter.java
Created January 21, 2014 14:47
ASCII to EBCDIC
public class CharFormatConverter {
static byte[] ASCII2EBCDIC = new byte[256];
static byte[] EBCDIC2ASCII = new byte[256];
static {
ASCII2EBCDIC[0x00] = (byte) 0x00;
ASCII2EBCDIC[0x01] = (byte) 0x01;
ASCII2EBCDIC[0x02] = (byte) 0x02;
ASCII2EBCDIC[0x03] = (byte) 0x03;
ASCII2EBCDIC[0x04] = (byte) 0x37;
ASCII2EBCDIC[0x05] = (byte) 0x2D;