how a developer can troubleshoot a sql performing poorly on a production db

61
How a Developer Can Troubleshoot a SQL Performing Poorly on a Production DB Carlos Sierra

Upload: carlos-sierra

Post on 20-Aug-2015

4.931 views

Category:

Software


3 download

TRANSCRIPT

Page 1: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

How a Developer Can Troubleshoot a SQL Performing

Poorly on a Production DBCarlos Sierra

Page 2: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

Enkitec (c) 2014 2

• Oracle Performance and SQL Tuning• Consultant/Developer/DBA• eDB360 and eAdam• SQLT and SQLHC• Exadata

Carlos Sierra

Page 3: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 3

Motivation• Developers might need to do SQL Tuning• Diagnostics Collection is key for SQL Tuning• Developers have limited access to DB server– OEM is usually off limits

• Developers need reliable SQL Tuning tools• Developers need to learn about these tools

Page 4: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 4

Common SQL Troubleshooting Steps

1. Implement proper code instrumentation2. Find application SQL performing poorly3. Gather diagnostics on SQL of concern4. Determine root cause of poor performance5. Fix root cause

Page 5: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 5

Code Instrumentation• Data elements available– Service– Client Identifier– Client Info– Module– Action

• Data type and size: VARCHAR2(64)

Page 6: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 6

Query Session Values

Page 7: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 7

Service (1)• Database logical name(s) set per instance• GV$SYSTEM_PARAMETER2 “service_names”

Page 8: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 8

Service (2)• Service SYS$BACKGROUND– Background Processes

• Service SYS$USERS– Default when session is not associated to Service

• Package DBMS_SERVICE– Start, stop, create, delete, modify, disconnect

Page 9: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 9

PL/SQL Instrumentation APIs• DBMS_SESSION.SET_IDENTIFIER– Sets CLIENT_IDENTIFIER

• DBMS_APPLICATION_INFO.SET_CLIENT_INFO– Sets CLIENT_INFO

• DBMS_APPLICATION_INFO.SET_MODULE– Sets MODULE and ACTION

Page 10: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 10

API usage Example for PL/SQL• Set before application code• Reset after application code and error

handling

Page 11: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 11

Query V$ to finding SQL• GV$SESSION– Session Status– Service, Module, Action, Client, User

• GV$SQL– SQL Text– Elapsed Time

Page 12: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 12

Active SQL Statements

Page 13: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 13

Some “safe” Diagnostics Tools• SQL Trace and TKPROF• Execution Plan and DBMS_XPLAN• planx.sql, sqlmon.sql and sqlash.sql• snapper.sql• SQLHC and SQLTXPLAIN

Page 14: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 14

Tracing Methods• ALTER SESSION– SQL_TRACE parameter– Event 10046

• DBMS_SESSION• DBMS_MONITOR (preferred)• ORADEBUG– Event 10046

Page 15: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 15

SQL_TRACE Syntax• ALTER SESSION SET SQL_TRACE = TRUE;• ALTER SESSION SET SQL_TRACE = FALSE;• Own Session

Page 16: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 16

Event 10046 Syntax• ALTER SESSION SET EVENTS '10046 TRACE

NAME CONTEXT FOREVER, LEVEL N';• ALTER SESSION SET EVENTS '10046 TRACE

NAME CONTEXT OFF';• Own Session

Page 17: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 17

Event 10046 Levels• 1: Performance metrics and Execution Plan metrics

– 10g: Execution Plan metrics for all executions– 11g: Execution Plan metrics for 1st execution

• 4: Binds• 8: Waits• 16: Execution Plan metrics for each execution (11.1+)• 64: Execution Plan metrics for 1st then each with DB

Time > 1min (11.2.0.2+)

Page 18: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 18

Event 10046 Level Examples• Execution Plan metrics + Binds + Waits– Level 4 + 8 = 12 • 11.1+: Execution Plan metrics for 1st execution • 10g: Execution Plan metrics for all executions

• Binds + Waits + 1st Exec + >1m Execs– Level 4 + 8 + 64 = 76• 11.2.0.2+

Page 19: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 19

DBMS_SESSION APIs• SESSION_TRACE_ENABLE– waits (TRUE|FALSE)– binds (FALSE|TRUE)– plan_stat (FIRST_EXECUTION|ALL_EXECUTIONS)

• SESSION_TRACE_DISABLE• Own Session

Page 20: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 20

DBMS_MONITOR APIs• Enable SQL Trace– SERV_MOD_ACT_TRACE_ENABLE– CLIENT_ID_TRACE_ENABLE– SESSION_TRACE_ENABLE

• ENABLE and DISABLE versions• TRACE and STAT versions

Page 21: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 21

SERV_MOD_ACT_TRACE_ENABLE• service_name (required)• module_name (optional)• action_name (optional)• waits (TRUE|FALSE)• binds (FALSE|TRUE)• plan_stat (FIRST_EXECUTION|

ALL_EXECUTIONS)

Page 22: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 22

CLIENT_ID_TRACE_ENABLE• client_id (required)• waits (TRUE|FALSE)• binds (FALSE|TRUE)• plan_stat – FIRST_EXECUTION|ALL_EXECUTIONS

Page 23: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 23

SESSION_TRACE_ENABLE• session_id (if null then own else sid)• serial_num (if null then use sid only)• waits (TRUE|FALSE)• binds (FALSE|TRUE)• plan_stat – FIRST_EXECUTION|ALL_EXECUTIONS

Page 24: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 24

Some related Views• Active Tracing– DBA_ENABLED_TRACES

• Collected Statistics– V$SERV_MOD_ACT_STATS– V$CLIENT_STATS– V$SESSTAT

Page 25: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 25

Trace Location• 11g– V$DIAG_INFO– Name = 'Diag Trace'

• 10g– V$PARAMETER2– Name = 'user_dump_dest'

Page 26: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 26

TKPROF Syntax• tkprof tracefile outputfile [sort=option]• sort=prsela fchela exeela

Page 27: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 27

TKPROF Sample

Page 28: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 28

TKPROF Execution Plan Sample

Page 29: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 29

DBMS_XPLAN Table Functions• DISPLAY• DISPLAY_AWR• DISPLAY_CURSOR• DISPLAY_PLAN• DISPLAY_SQL_PLAN_BASELINE• DISPLAY_SQLSET

frequently used

also used

Page 30: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 30

DBMS_XPLAN.DISPLAY_CURSOR• sql_id (default: last executed session cursor)• cursor_child_no (default: 0)• format (default: typical)– allstats (io and mem stats for all executions)– allstats last (ditto for last execution)– advanced (includes outline) both undocumented

Page 31: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 31

advanced

sql_idchild number

SQL text

Execution Plan

Page 32: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 32

Query Blocks

Outline

Bind Variables

Page 33: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 33

Predicates

Column Projection

Page 34: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 34

allstats last

actual rows

Page 35: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 35

planx.sql• Installs nothing• 10g and 11g• Parameters– Oracle Diagnostics Pack License? Y | N– SQL_ID

• Free

Page 36: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 36

planx.sql Execution Plan• DBMS_XPLAN.DISPLAY– GV$SQL_PLAN_STATISTICS_ALL– ADVANCED ALLSTATS LAST

• DBMS_XPLAN.DISPLAY_AWR– ADVANCED

Page 37: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 37

planx.sql Views (1)• GV$SQLSTATS (extended retention)• GV$SQLSTATS_PLAN_HASH• GV$SQL• GV$ACTIVE_SESSION_HISTORY

Page 38: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 38

planx.sql Views (2)• DBA_HIST_SQLSTAT• DBA_HIST_SQL_PLAN• DBA_HIST_ACTIVE_SESS_HISTORY

Page 39: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 39

planx.sql Views (3)• DBA_TABLES• DBA_INDEXES• DBA_TAB_COLS• DBA_IND_COLS

Page 40: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 40

Page 41: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 41

Page 42: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 42

Page 43: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 43

Page 44: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 44

sqlmon.sql• Installs nothing• 10g and 11g• Parameters– Oracle Tuning Pack (required) License? Y | N– SQL_ID

• Free

Page 45: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 45

sqlmon.sql Output• SQL Monitor Reports– HTML– Text– List– Detail

Page 46: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 46

Page 47: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 47

SQL Monitor Execution Plan

Page 48: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 48

Page 49: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 49

sqlash.sql• Installs nothing• 10g and 11g• Parameters– Oracle Diagnostics Pack (required) License? Y | N– SQL_ID

• Free

Page 50: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 50

sqlash.sql Output• ASH Reports for one SQL– Format• HTML• Text

– Source• Memory• AWR

Page 51: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 51

Page 52: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

Enkitec © 2014 52

Snapper• Session Centric• Installs nothing• 10g and 11g• 4 Parameters• Does not require Diagnostics or Tuning Pack• Free

Page 53: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 53

Snapper Syntax• Scope (‘all’ includes ASH and Stats) • seconds_in_snap (30 is common)• snap_count (between 1 and 5 are common)• sid (session id)• Execution Sample– @snapper.sql all 30 1 50

Page 54: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 54

Page 55: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 55

SQLHC 1366133.1• Installs nothing• 10g and 11g• Parameters– Oracle Pack License? T | D | N– SQL_ID

• Free (requires MOS account)

Page 56: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 56

Page 57: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 57

SQLTXPLAIN (SQLT) 215187.1• Installs two schemas and many objects• 10g and 11g• Methods– SQLT XTRACT (inputs SQL_ID)– SQLT XECUTE (inputs SQL Text)

• Free (requires MOS account)

Page 58: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 58

Page 59: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 59

Summary1. Implement proper code instrumentation2. Find application SQL performing poorly3. Gather diagnostics on SQL of concern4. Determine root cause of poor performance5. Fix root cause

Page 60: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

04/18/2023 Enkitec © 60

References• Oracle Documentation• http://blog.tanelpoder.com/files/scripts/

snapper.sql• http://carlos-sierra.net/ (look for cscripts)• SQL Health-Check (SQLHC): MOS 1366133.1• SQLXPLAIN (SQLT): MOS 215187.1

Page 61: How a Developer can Troubleshoot a SQL performing poorly on a Production DB

Enkitec (c) 2014 61

Contact Information• [email protected]• carlos-sierra.net• @csierra_usa