Daily sql optimization commonly used shortcut scripts--by lixora


---- Get the execution plan of the last execution of sql

select * from table(dbms_xplan.display('PLAN_TABLE',NULL,'ALLSTATS'));


set linesize 230 pagesize 9999

select * from table(dbms_xplan.display_cursor(null,null,'ADVANCED note last'));

 

 /*+ gather_plan_statistics  */

select * from table(dbms_xplan.display_cursor(null,null,'allstats note last'));


---Get the sql_id of this sql statement:

 SELECT SQL_ID, SQL_TEXTFROM V$SQL WHERE SQL_TEXT LIKE '%SELECT e.last_name,%' ; 


--- From AWR: You can see all execution plans

Select * from table(dbms_xplan.display_awr('&sql_id',null,null, 'ADVANCED'));

 

---From Cursor Cache:

set linesize 230 pagesize 9999

SELECT * FROM  TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&SQL_ID',null,'ADVANCED'));

 

SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&SQL_ID',null,'ADVANCEDnote'));

 

 

--- View the row operation with the most overhead :

alter session set statistics_level=all;

 

/*+ gather_plan_statistics */ 

 

select t.*

from v$sql s

   ,table(dbms_xplan.display_cursor(s.sql_id,s.child_number,'ALL IOSTATS LAST')) t where s.sql_id = '&SQL_ID' ;

 

 

How to get the execution plan of sql:

selectcount(t.id)

  from pr_transfer t

 where t.sts = 'Y'

   and t.pop_sts in ('Y', 'W')

   and t.lastflag = 'Y'

   and t.trs_status in ('10', '30')

   and t.trs_out_dt isnull

   and t.name = :1

   and t.live_code = :2

--get sql id:

SELECT SQL_ID, SQL_TEXT FROM V$SQL WHERE SQL_TEXT LIKE ' selectcount(t.id) from pr_transfer t%‘ 

get sql_id

Substitute sql_id into the following sql to get the execution plan:

Select * from table(dbms_xplan.display_awr('&sql_id',null,null,'ALLSTATS'));

SELECT * FROM  TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&SQL_ID',null,'ALLSTATS'));

--- Through the following SQL, you can get historical bind variables :

 

---11g:

select * from ( select snap_id, to_char(sn.begin_interval_time,'MM/DD-HH24:MI')snap_time, sq.sql_id,bm.position,dbms_sqltune.extract_bind(bind_data,bm.position).value_string value_string fromdba_hist_snapshot sn natural join dba_hist_sqlstat sq,dba_hist_sql_bind_metadata bm

where sq.sql_id = bm.sql_id and sq.sql_id ='&sql'

) PIVOT (max(value_string) for position in(1,2,3,4,5,6,7,8,9,10)) order by snap_id;

---10G:

select snap_id,to_char(sn.begin_interval_time,'MM/DD-HH24:MI') snap_time,sq.sql_id,bm.position, dbms_sqltune.extract_bind(bind_data,bm.position).value_stringvalue_string from dba_hist_snapshot sn natural join dba_hist_sqlstat sq,dba_hist_sql_bind_metadata bm

where sq.sql_id = bm.sql_id and sq.sql_id ='&sql';

Save the execution plan to word or excel

select * from table(dbms_xplan.display_cursor(null,null,'ADVANCEDlast'));

execdbms_stats.gather_table_stats(ownname=>'&owner',tabname=>'&tab_name',estimate_percent=>100,cascade=>true);

execute dbms_stats.gather_table_stats

(ownname => 'LUNAR', tabname =>'LUNARTEST1',

estimate_percent =>DBMS_STATS.AUTO_SAMPLE_SIZE,method_opt => 'FOR ALL COLUMNS SIZE AUTO');

------ View the execution time of each sql plan of sql

WITH

p AS (

SELECT plan_hash_value

  FROM gv$sql_plan

 WHERE sql_id =TRIM('&&sql_id.')

   AND other_xml ISNOT NULL

 UNION

SELECT plan_hash_value

  FROMdba_hist_sql_plan

 WHERE sql_id =TRIM('&&sql_id.')

   AND other_xml ISNOT NULL ),

m AS (

SELECT plan_hash_value,

      SUM(elapsed_time)/SUM(executions) avg_et_secs

  FROM gv$sql

 WHERE sql_id =TRIM('&&sql_id.')

   AND executions> 0

 GROUP BY  plan_hash_value ),

a AS (

SELECT plan_hash_value,

      SUM(elapsed_time_total)/SUM(executions_total) avg_et_secs

  FROMdba_hist_sqlstat

 WHERE sql_id =TRIM('&&sql_id.')

   ANDexecutions_total > 0

 GROUP BY  plan_hash_value )

SELECT p.plan_hash_value,

      ROUND(NVL(m.avg_et_secs, a.avg_et_secs)/1e6, 3) avg_et_secs

  FROM p, m, a

 WHEREp.plan_hash_value = m.plan_hash_value(+)

   ANDp.plan_hash_value = a.plan_hash_value(+)

 ORDER BY  avg_et_secs NULLS LAST;


Related


Top 3 Services Used Daily - SQL

Milau The query below gives me the top 3 services for June 6, 2021. SELECT products_name AS product, COUNT(products_name) AS NumberOfBookings FROM bookings WHERE date = "06-06-2021" GROUP BY products_name, ORDER BY NumberOfBookings DESC LIMIT 3; I want

How to automate daily SQL scripts (Oracle) using python?

Pompita How to automate daily SQL scripts (Oracle) using python? -is it possible? - If possible, how to automatically export query results to excel? Andrew You'll need a script to execute it python cronevery day , see the documentation https://pypi.python.org/

How to automate daily SQL scripts (Oracle) using python?

Pompita How to automate daily SQL scripts (Oracle) using python? -is it possible? - If possible, how to automatically export query results to excel? Andrew You'll need a script to execute it python cronevery day , see the documentation https://pypi.python.org/

How to automate daily SQL scripts (Oracle) using python?

Pompita How to automate daily SQL scripts (Oracle) using python? -is it possible? - If possible, how to automatically export query results to excel? Andrew You'll need a script to execute it python cronevery day , see the documentation https://pypi.python.org/

How to automate daily SQL scripts (Oracle) using python?

Pompita How to automate daily SQL scripts (Oracle) using python? -is it possible? - If possible, how to automatically export query results to excel? Andrew You'll need a script to execute it python cronevery day , see the documentation https://pypi.python.org/

How to automate daily SQL scripts (Oracle) using python?

Pompita How to automate daily SQL scripts (Oracle) using python? -is it possible? - If possible, how to automatically export query results to excel? Andrew You'll need a script to execute it python cronevery day , see the documentation https://pypi.python.org/

Most commonly used bid type T-SQL per store

fish head I need some help building this MSSQL query. I have a table with payment information. In the table "payment_entry" are the fields for store number and bid type. Every new purchase creates a new row in the "payment_entry" table with "store" and "tender

Is this commonly used in Python?

Artemix So while digging into some logic behind state changes, I came across this snippet in my code: def doSomething(cls, state): state &= ~CLOSED_STATE if (state & OPEN_STATE) == 0: state |= ANOTHER_STATE return state (其中CLOSED_STATE =

Commonly used practical methods

Siva Kumar In our organization we will start new development projects in .net core API/Angular/SQL. We considered adding utilities such as logging, file manipulation, mail functionality, generic data table functionality, list extension methods. Can someone hel

Commonly used practical methods

Siva Kumar In our organization we will start new development projects in .net core API/Angular/SQL. We considered adding utilities such as logging, file manipulation, mail functionality, generic data table functionality, list extension methods. Can someone hel

What is commonly used in XNA?

Baklap4 I'm taking an XNA course (yes, I know it's almost outdated). My teacher gave some perspective on multiple 3D objects in your world just by importing one FBX file. So, is it advisable to create a full game world in just one FBX file, or is it more commo

Is this commonly used in Python?

Artemix So while digging into some logic behind state changes, I came across this snippet in my code: def doSomething(cls, state): state &= ~CLOSED_STATE if (state & OPEN_STATE) == 0: state |= ANOTHER_STATE return state (其中CLOSED_STATE =

What is commonly used in XNA?

Baklap4 I'm taking an XNA course (yes, I know it's almost outdated). My teacher gave some perspective on multiple 3D objects in your world just by importing one FBX file. So, is it advisable to create a full game world in just one FBX file, or is it more commo

What is commonly used in XNA?

Baklap4 I'm taking an XNA course (yes, I know it's almost outdated). My teacher gave some perspective on multiple 3D objects in your world just by importing one FBX file. So, is it advisable to create a full game world in just one FBX file, or is it more commo

Kill and restart Python scripts daily

student I have a Python code that pulls Twitter data via the streaming API. I want to use a separate file for each day, so I want the script to run for 24 hours, then kill it and restart it, since restarting the program, the name of the file changes. How can I

7.1.1 - Check if SHORTCUT is used

Alexandru Topală So basically what I'm trying to achieve is: Know when the activity is accessed via the shortcut, and when the activity is not accessed via the shortcut. I am using static shortcuts. Went through the documentation but no luck. Maybe it has some

SQL or a shortcut?

Gates Wan SELECT * FROM nobel WHERE winner = 'Theodore Roosevelt' or winner = 'Woodrow Wilson' or winner = 'Jed Bartlet' or winner = 'Jimmy Carter'; Is there a way to shorten it? Maybe a way to use regular expressions? Gordon Linoff You want inthe operator: S

SQL or a shortcut?

Gates Wan SELECT * FROM nobel WHERE winner = 'Theodore Roosevelt' or winner = 'Woodrow Wilson' or winner = 'Jed Bartlet' or winner = 'Jimmy Carter'; Is there a way to shorten it? Maybe a way to use regular expressions? Gordon Linoff You want inthe operator: S

SQL or a shortcut?

Gates Wan SELECT * FROM nobel WHERE winner = 'Theodore Roosevelt' or winner = 'Woodrow Wilson' or winner = 'Jed Bartlet' or winner = 'Jimmy Carter'; Is there a way to shorten it? Maybe a way to use regular expressions? Gordon Linoff You want inthe operator: S

SQL or a shortcut?

Gates Wan SELECT * FROM nobel WHERE winner = 'Theodore Roosevelt' or winner = 'Woodrow Wilson' or winner = 'Jed Bartlet' or winner = 'Jimmy Carter'; Is there a way to shorten it? Maybe a way to use regular expressions? Gordon Linoff You want inthe operator: S

SQL or a shortcut?

Gates Wan SELECT * FROM nobel WHERE winner = 'Theodore Roosevelt' or winner = 'Woodrow Wilson' or winner = 'Jed Bartlet' or winner = 'Jimmy Carter'; Is there a way to shorten it? Maybe a way to use regular expressions? Gordon Linoff You want inthe operator: S

Assign shortcut keys to run scripts

M0εiπ I have written a script to change the brightness of the screen. This is useful when I'm working in text mode and the keyboard shortcuts don't change the brightness. Is it possible to run this script just by pressing certain keys on the keyboard as shortc

Assign shortcut keys to run scripts

M0εiπ I have written a script to change the brightness of the screen. This is useful when I'm working in text mode and the keyboard shortcuts don't change the brightness. Is it possible to run this script just by pressing certain keys on the keyboard as shortc

What are the commonly used detail namespaces

pig In some larger projects or libraries (e.g. Eigen) you can see namespaces like internalor detail. I understand what's the internalbenefit, but what's the use in detailgeneral ? Are there any general conventions for distributing code into such namespaces (es

Commonly used std unary functions

Mircea Ispas Is there a function that cannot use the unary version from the function 's binary function ? I know it's easy to create a unary version using std::bind, but the code looks ugly: std::find_if(std::begin(v), std::end(v), std::bind(std::less_equal<in

Most Commonly Used Words in Python

George Richmont I am trying to implement a code that will allow me to find the 10 most common words in text. I'm new to python and more used to languages like C#, java or even C++. Here is what I did: f = open("bigtext.txt","r") word_count = {} Basically, my

Most commonly used strings in IntelliJ

Andrei Suvorkov: Is there a way to detect if any hardcoded string is repeated more than x times throughout the project ? Suppose I have a project with multiple folders and classes, and I have a separate class called Constants, where I store all the project con

The most commonly used query in mysql

Massin I am confused on how to find the most frequently used queries in mysql. I am making this query because I want to use memcache facility in my php application. The only way to really find them is to log every query to a log file and then process it? Navee

Most commonly used strings in IntelliJ

Andrei Suvorkov: Is there a way to detect if any hardcoded string is repeated more than x times throughout the project ? Suppose I have a project with multiple folders and classes, and I have a separate class called Constants, where I store all the project con