Professional Documents
Culture Documents
Oracle Tuning
Oracle Tuning
Tuning Roles
Business analyst 1. Tune business function
Designer 2. Tune data design
3. Tune process design
Application developer 4. Tune SQL statements
5. Tune physical structure
Database administrator
6. Tune memory allocation
7. Tune I/O
8. Tune memory contention
Operating system administrator 9. Tune operating system
Network administrator 10. Networking issues
SQL PROCESSING
ARCHITECTURE
• The SQL processing architecture contains the following
main components:
– Parser
– Optimizer
– Row Source Generator
– SQL Execution Engine
SQL Processing Architecture
Tuning SQL Statements
Use performance analysis tools to check the results of
the following steps:
1. Tune the schema.
– Add indexes as required
– Create clusters if needed
2. Choose the language: SQL, PL/SQL, or Java.
3. Design for the reuse of SQL optimization.
4. Design and tune SQL statements.
5. Maximize performance with the optimizer
SQL STATEMENT PROCESSING
System Global Area (SGA)
The Shared Pool
Shared SQL Areas
The Program Global Area (PGA)
SQL Statement Processing Phases
Sharing Cursors: Benefits
Sharing Cursors: Requirements
Writing SQL to Share Cursors
Bind Variables and Shared Cursors
Writing SQL to Share Cursors
Monitoring Shared Cursors
V$LIBRARYCACHE Columns
V$SQLAREA Columns
System Global Area (SGA)
Instance
SGA Shared pool
DB buffer Redo log
Server cache buffer
process
PGA
Shared SQL
SGA
Open Close
Parse
– Searches for the statement in the shared pool
– Checks syntax
– Checks semantics and privileges
– Merges view definitions and subqueries
– Determines execution plan
Bind
– Scans the statement for bind variables
– Assigns (or reassigns) a value
SQL Statement Processing Phases
Execute
– Applies the execution plan
– Performs necessary I/O and sorts for data manipulation language
(DML) statements
Fetch
– Retrieves rows for a query
– Sorts for queries when needed
– Uses an array fetch mechanism
Sharing Cursors: Benefits
Reduces parsing
Dynamically adjusts memory
Improves memory usage
Sharing Cursors: Requirements
Cursors can be shared only by SQL statements that have
the following identical elements:
Text
– Uppercase and lowercase
– White space (spaces, tabs, carriage returns)
– Comments
Referenced objects
Bind-variable data types
Writing SQL to Share Cursors
If the case or the amount of white space is different,
then the statements are not identical.
EXPLAIN PLAN
SET STATEMENT_ID
= 'text'
FOR statement
EXPLAIN PLAN Example
SQL> EXPLAIN PLAN
2 set statement_id = 'demo01' for
3 select *
4 from products
5 where prod_category = 'Men'
6 and prod_subcategory = 'Jeans - Men';
Explained.
Displaying the Execution Plan
SQL> column "Query Plan" format a60
SQL> select id
2 , lpad(' ', 2*level)||operation
3 ||decode(id,0,' Cost = '||position)
4 ||' '||options
5 ||' '||object_name as "Query Plan"
6 from plan_table
7 where statement_id = 'demo01'
8 connect by prior id = parent_id
9 start with id = 0;
Interpreting the Execution Plan
SQL> select id , lpad(' ',2*level)||operation|| decode(id, 0,' Cost
= '||position)||' '||options ||' '||object_name as "Query Plan“
from plan_table where statement_id = 'demo01‘ connect
by prior id = parent_id start with id=0;
ID Query Plan
----- -----------------------------------------
0 SELECT STATEMENT Cost =
1 TABLE ACCESS BY INDEX ROWID PRODUCTS
2 AND-EQUAL
3 INDEX RANGE SCAN PRODUCTS_PROD_CAT_IX
4 INDEX RANGE SCAN PRODUCTS_PROD_SUBCAT_IX
Using V$SQL_PLAN
Provides a way of examining the execution
plan for cursors that were recently executed.
Information in this view is very similar to the
output of an EXPLAIN PLAN statement:
– EXPLAIN PLAN shows a theoretical plan that
can be used if this statement were to be
executed.
– V$SQL_PLAN contains the actual plan used.
V$SQL_PLAN Columns
ADDRESS
Address of the handle to the parent of the cursor
OBJECT# Object number of the table or the index
Name of the table or index
OBJECT_NAME Name of the internal operation performed in this
step; for example, TABLE ACCESS
OPERATION A variation on the operation described in the
OPERATION column; for example, FULL
OPTIONS Current mode of the optimizer for the first row in
the plan (statement line)
OPTIMIZER
Cost of the operation as estimated by the
COST optimizer's cost-based approach
V$SQL_PLAN Columns
OFF
SET AUTOTRACE ON
TRACE[ONLY]
EXPLAIN
STATISTICS
SHOW AUTOTRACE
SQL*Plus AUTOTRACE Examples
To start tracing statements using AUTOTRACE:
set autotrace on
To hide statement output:
set autotrace traceonly
To display execution plans only:
set autotrace traceonly explain
Control the layout with column settings.
SQL*Plus AUTOTRACE Statistics
set autotrace traceonly statistics
select * from dual;
Statistics
---------------------------------------------------
0 recursive calls
2 db block gets
1 consistent gets
0 physical reads
0 redo size
367 bytes sent via SQL*Net to client
430 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL TRACE AND TKPROF
SQL Trace Facility
How to Use the SQL Trace Facility
Initialization Parameters
Switching On SQL Trace
Finding Your Trace Files
Formatting Your Trace Files
TKPROF Command Options
Output of the TKPROF Command
TKPROF Output Example: No Index
TKPROF Output Example: Unique Index
Some TKPROF Interpretation Pitfalls
SQL Trace Facility
Is set at the instance or session level
Gathers statistics for SQL statements
Produces output that can be formatted by
TKPROF
Trace
SQL Trace file
Report
TKPROF file
Database
How to Use the SQL Trace Facility
1. Set the initialization parameters.
2. Switch on tracing.
3. Run the application.
4. Switch tracing off and format the trace file.
5. Interpret the output.
Trace
SQL Trace file
Report
TKPROF file
Database
Initialization Parameters
TIMED_STATISTICS = {false|true}
MAX_DUMP_FILE_SIZE = {n|500}
USER_DUMP_DEST = directory_path
MAX_DUMP_FILE_SIZE is measured in
operating system blocks
Default USER_DUMP_DEST is the location
of the background dump destination
Switching On SQL Trace
For an instance, set the following parameter:
SQL_TRACE = TRUE
For your current session:
SQL> ALTER SESSION SET sql_trace = true;
SQL> EXECUTE dbms_session.set_sql_trace(true);
For any session:
SQL> EXECUTE
dbms_system.set_sql_trace_in_session (session_id,
serial_id, true);
Finding Your Trace Files
Look in the directory specified by
USER_DUMP_DEST.
If only a few people are running SQL Trace,
look for the most recent time stamp.
Otherwise, consider writing a script.
SQL> @readtrace.sql
SQL> EXECUTE gettrace;
PL/SQL procedure successfully completed.
Formatting Your Trace Files
OS> tkprof tracefile outputfile [options]
TKPROF command examples:
OS> tkprof
OS> tkprof ora_901.trc run1.txt
OS> tkprof ora_901.trc run2.txt sys=no
sort=execpu print=3
TKPROF Command Options
SORT = option
PRINT = n
EXPLAIN = username/password
INSERT = filename
SYS = NO
AGGREGATE = NO
RECORD = filename
TABLE = schema.tablename
Output of the TKPROF Command
Text of the SQL statement
Trace statistics (for statement and recursive calls)
separated into three SQL processing steps:
PARSE Translates the SQL statement into an
execution plan
Index entry
Root
Branch
Root block
JONES
Branch
blocks ALLEN MARTIN
FORD SMITH
Leaf
blocks
ADAMS BLAKE - rowid JAMES KING MILLER TURNER
ALLEN CLARK - rowid JONES MARTIN SCOTT WARD
FORD - rowid SMITH
Data segment
ROWID 7782 CLARK Next/previous
lookup 7698 BLAKE leaf pointers
7902 FORD
CREATE INDEX Syntax
Composite Index Guidelines
SELECT
INSERT /*+ hint */
UPDATE comment
text
DELETE
SELECT
INSERT --+ hint
UPDATE comment
text
DELETE
Optimizer Hint Example
SQL>update --+ INDEX(p PROD_CATAGORY_IDX)
products p
set p.prod_min_price =
(select
(pr.prod_list_price*.95)
from products pr
where p.prod_id = pr.prod_id)
where p.prod_category = 'Men'
and p.prod_status = 'available, on stock'
/
Setting the Optimizer Mode
ON CUSTOMERS(upper(cust_last_name));
SQL> SELECT *
FROM customers
WHERE upper(cust_last_name) = 'SMITH';