Skip to content

Instantly share code, notes, and snippets.

@davidhooey
davidhooey / oracle_segment_advisor_for_all_segments_in_schema.sql
Created September 24, 2013 20:11
Oracle Segment Advisor for All Segments in Schema
--
-- Segment Advisor for All Segments in Schema
--
-- 1. Grant the ADVISOR and SELECT ANY DICTIONARY roles to the schema owner.
-- 2. Execute the script as the schema owner.
-- 3. Revoke the ADVISOR and SELECT ANY DICTIONARY roles from the schema owner.
set echo off
set feedback off
@davidhooey
davidhooey / oracle_segment_advisor_for_individual_segments.sql
Last active January 26, 2016 21:23
Oracle Segment Advisor for Individual Segments
--
-- Segment Advisor for Individual Segments
--
-- 1. Replace all occurances of [OWNER] with the owner of the segments.
-- 2. Replace [TABLE_NAME] or [INDEX_NAME] with the segment to be analyzed.
set echo off
set feedback off
set verify off
@davidhooey
davidhooey / oracle_execution_plan_for_sqlid.sql
Last active November 3, 2017 09:50
Oracle Execution Plan for SQL_ID
-- Shared Pool
select * from table(dbms_xplan.display_cursor('SQL_ID',null,'ALL'));
-- AWR
select * from table(dbms_xplan.display_awr('SQL_ID',null,null,'ALL'));
select * from table(dbms_xplan.display_awr('SQL_ID',null,DBID,'ALL'));
@davidhooey
davidhooey / oracle_top_wait_events.sql
Created September 18, 2013 13:50
Oracle Top Wait Events
select
name,
round(time_secs, 2) time_secs,
case when time_secs = 0 then
0
else
round(time_secs*100 / sum(time_secs) Over(), 2)
end pct
from
(
@davidhooey
davidhooey / oracle_wait_events_for_sqlid.sql
Created September 17, 2013 13:58
Oracle Wait Events for SQL_ID
-- Shared Pool
select
event,
time_waited "time_waited(s)",
case when time_waited = 0 then
0
else
round(time_waited*100 / sum(time_waited) Over(), 2)
end "percentage"
from
@davidhooey
davidhooey / oracle_create_skewed_table_with_id.sql
Created September 5, 2013 14:51
Oracle Create Skewed Table With ID
drop table skewed;
drop sequence skewed_sequence;
create table skewed
(
id number,
type varchar2(50),
constraint skewed_id_pk primary key (id)
);
@davidhooey
davidhooey / oracle_create_million_row_table_with_id.sql
Created September 5, 2013 14:38
Oracle Create Million Row Table With Unique ID
drop table million;
drop sequence million_sequence;
create table million
(
id number,
type varchar2(50),
constraint million_id_pk primary key (id)
);
@davidhooey
davidhooey / oracle_sql_id_from_sql_text.sql
Last active December 22, 2015 07:18
Oracle Determine SQL_ID given SQL text.
select dbms_sqltune_util0.sqltext_to_sqlid('SQL_TEXT_HERE'||chr(0)) sql_id from dual;
@davidhooey
davidhooey / oracle_elapsed_time_for_sql.sql
Last active December 2, 2016 16:20
Oracle Elapsed Time Per Execution for SQL
-- Shared Pool
select *
from
(
select
case when executions = 0 then 0
else round(elapsed_time/executions, 3)
end "ElapsedPerExec(ms)",
elapsed_time "ElapsedTime (ms)",
executions "Executions",
@davidhooey
davidhooey / oracle_bind_variable_plan.sql
Last active December 21, 2015 00:48
Oracle PL/SQL script to simulate bind variables, time queries and generate execution plans.
set timing on;
set linesize 120;
--
-- Bind Variable Setup
--
variable Bind1 number;
variable Bind2 varchar2(2000);