Tuning Presentation July 18
Tuning Presentation July 18
Data
Actual data
Redo
Undo
Consistency
How oracle engine works
Data files
Log files
Control files
Archive files
Init file
Data Storage---Logical Structures
Table space
Segments
Extents
Blocks
Oracle Memory components
Oracle RDBMS Architecture
Background Processes
SGA
Oracle
Server Processes
Client Processes
Oracle Architecture
System Global Area
SMON Instance Lock Area Large Pool Redo Log Buffer Pnnnn
DATABASE
SERVER
DATA Archived
Redo Redo Logs
PGA USER
Logs Logs
INDEX
TEMP Group 1 Group 2 Alert
Datafiles ROLLBACK File
Parameter Password
SYSTEM Control
USER File File
Files
INIT.ORA orapwSID Trace
Files
Oracle memory structures
Shared Pool
Smon
Pmon
Dbwr
Lgwr
Arch
SNPn
LCKnn
Pnn
Snn
Dnn
How Oracle engine works
What happens when you press “Enter”?
Steps in Parsing
Syntax Check
Object Resolution
Security Check
Build Parse Tree & Execution Plan
Store Parse Tree & Execution Plan in the
Shared SQL Area (Lib. Cache)
In 9i and up, bind variable value peeking is performed
Hard vs. Soft Parse
SQL Statement Processing - Define
Resolve and map data types on the client and the server
SQL*Net is involved
ARRAYSIZE negotiated
SQL Statement Processing - Bind
All bind variables(:v_id, :v_name etc.) are bound with their current
values
SQL is not hard-parsed for varying values
Facilitates re-use of SQL
SQL Statement Processing - Execute
possible
SQL*Plus - SET ARRAYSIZE n
Forms - Set the Records Fetched Block Property Sheet
Parsing
Selecting a record
Branch Branch
Leaf Leaf
Branch
Updating a record
Writes a plan to know how to get the old value if the user gives rollback
Access paths
Indexes
Optimizer
Joins
Sorting
Hints
Parallel
Index scan
Rowid scan
• More often than not, full-table scans were the cause of bad
query performance
FTS effects on buffer cache
•db_file_multi_block_read_count
“If you can’t measure it, you can’t manage it.” —Peter Drucker
Tkprof
V$scripts
Statspack
Create plan_table
@$ORACLE_HOME/rdbms/admin/utlxplan.sql
Query
@$ORACLE_HOME/rdbms/admin/utlxpls.sql
@$ORACLE_HOME/rdbms/admin/utlxplp.sql
Explain plan
Output
Reading the Execution Plan
Query Plan
--------------------------------------------------------------------------------
1.0 SELECT STATEMENT Statement1 Cost = 36 (For analyzed only)
2.1 SORT ORDER BY (7th)
3.1 FILTER (6th)
4.1 NESTED LOOPS OUTER (5th)
5.1 TABLE ACCESS BY ROWID BK (2nd)
6.1 INDEX RANGE SCAN I_BK_06 NON-UNIQUE (1st)
5.2 TABLE ACCESS BY ROWID EE (4th)
6.1 INDEX UNIQUE SCAN I_EE_01 UNIQUE (3rd)
Operations
Unique emp(empno)
Unique emp(empno)
Emp(job,hiredate)
Emp(job,hiredate)
user_dump_dest.
TKPROF performance
database server
TKPROF program
*******************************************************************************
*
count = number of times OCI procedure was executed
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 SORT (AGGREGATE)
2 1 INDEX (RANGE SCAN) OF 'EMP7_I1' (NON-UNIQUE)
Autotrace Output
Statistics
0 recursive calls
0 db block gets
1 consistent gets
1 physical reads
0 redo size
223 bytes sent via SQL*Net to client
274 bytes recd via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed
Setting Timing On
COUNT(NAME)
--------------
100
Elapsed: 00:00:00.84
V$scripts
V$session
V$sql
V$process
V$sysstat
V$library cache
V$rollstat
V$sqlarea
Statspack
SQL> @$ORACLE_HOME/rdbms/admin/spcreate
@$ORACLE_HOME/rdbms/admin/profload.sql
@$ORACLE_HOME/rdbms/admin/proftab.sql
PLSQL_PROFILER_RUNS
PLSQL_PROFILER_UNITS
PLSQL_PROFILER_DATA
Questions?
Indexes—Walk Through
The Concept of an Index
When an index block cannot store pointers to all data, a second index block is
created and a pointer is placed in the root block to the newly created index
block
Index
Type Introduced
Bitmap Indexes 7.3.2
Index Organised Table 8.0
Partitioned Indexes 8.0
Reverse Key 8.0
LOB Index 8.0
Compressed 8.1.5
Function-Based Indexes 8.1.5
Descending 8.1.5
Virtual Indexes 8.1.5
Bitmap Join Indexes 9.0.1
INDEXES
B*tree
Insertion
Before After
ENG
SCO
USA
Index initially has single empty leaf block at root. Rows are
inserted into leaf block
Insertion
Inserting 'BEL'
Before After
ENG U
SCO
USA
BEL USA
ENG
SCO
Root node is now a branch block. Two new leaf nodes created.
Leaf rows split between the two new blocks
Insertion
Inserting 'SPA'
Before After
U S U
New leaf node created. Leaf rows split between the two leaf
nodes. New leaf block pointer added to branch block
When Are Bitmap Indexes Not Useful?
When row level locking is required - locking on the bitmap index is done
at the bitmap category level.
A full table scan is often more efficient if only a single bitmap value is
filtered
Restrictions
CREATE_BITMAP_AREA_SIZE
Determines the amount of memory allocated for bitmap creation.
Default is 8MB. If cardinality is small, this value can be reduced
significantly.
BITMAP_MERGE_AREA_SIZE
Amount of memory to use for merging bitmap strings. Default value
is 1MB. Larger value can improve performance since the bitmap segments
must be pre-sorted before being merged into a single bitmap.
Function based index:
Function-based indexes introduced in Oracle release 8.1 provides an efficient
mechanism to evaluate predicates involving functions
Init.ora to set:
optimizer_goal=choose|first_rows|all_rows
query_rewrite_enable = true
query_rewrite_integrity = trusted
Function based indexing is only available in the Enterprise Edition and is not
available in the Standard Edition.
Function based index:
Restrictions
1.Function-based indexes cannot be created on
a) LOB columns,
d) It can only be used with Cost Based optimizer. The rule based optimizer will
never use function-based indexes.
e) Since function cannot return NULL you cannot store null values
Indexing ---Case when
Selective Uniqueness
You have a table with versioned information in it
A project table with status "ACTIVE" and "INACTIVE"
When status is "ACTIVE", some set of columns must be unique
When status is "INACTIVE", those columns may contain any values -- any
number of duplicates
How can you do it?
Indexing—Case when
REVERSE indexes
causes monotonically-ascending data values to become more random
by simply reversing data
“123456” becomes “654321”
“123457” becomes “754321”, etc...
data is transparently converted and unconverted upon insert and
retrieval
data is re-fitted to fit Oracle’s original design decision
Impact:
only equivalence operations will use the index
“=“, “!=“, “<>”, “IN”, and “NOT IN”
range-scans will not use the index
“>”, “>=“, “<“, “<=“, “LIKE”, “BETWEEN”
What Columns to Index
When the query will return less then 15% of the rows in a
large table.
Columns not to Index
Index1 Index2
Index3 Index4
Index5 Index6
Oracle9i
select index_name,monitoring,used,start_monitoring,
end_monitoring from v$object_usage;
Finding index usage
Monitor Index Usage (9i)
Start/Reset: ALTER INDEX <index> MONITORING USAGE.
Stop: ALTER INDEX <index> NOMONITORING
Views
User_indexes
User_ind_columns
V$object_usage (9i)
Index_stats
Oracle9i Key Index Features
First Part
Of Index
(YEARS)
Binary height-----User_indexes
FIRST_ROWS_1
FIRST_ROWS_10
FIRST_ROWS_100
FIRST_ROWS_1000
alter system set optimizer_mode = first_rows_100;
alter session set optimizer_mode = first_rows_100;
select /*+ first_rows(100) */ from student;
Oracle and the Optimizers
Basic information about the Tables, clusters, and views in the FROM clause
Unlike the RBO, the CBO determines the cost of all possible
execution plans. It tries all possible permutations of
execution plan, up to the limit imposed by the initialization parameter,
OPTIMIZER_MAX_PERMUTATIONS, which defaults to a value of 80,000.
It could receive bad data as input (the old garbage-in, garbage-out problem)
Causes swapping if to little memory is left
Sorting
Statements generating Temporary
Segments:
Create Index
Select .... Order By
Select .... Distinct
Select .... Group By
Select .... Union
Select .... Intersect
Select .... Minus
Questions?
HINTS
Hints
The Syntax must be correct or the Hint will be ignored, and no error
message is issued.
Hints only apply to the statement they are in. Nested statements are
treated as totally different statements, requiring their own Hints.
When using an alias for a table in the statement, the alias needs to be in
the Hint.
Key” Hints for Optimization
Hash Use_hash
Questions?
Parallel Query Option
The Parallel Query Option
Short jobs will usually suffer from this option because of the time
required to divide and reassemble the query.
Cost-based Optimization
Benefits of Parallel Execution
CPU CPU
scan scan
CPU CPU
idle scan
CPU CPU
idle scan
CPU CPU
idle scan
User Query
Query Coordinator
Query
A-G Server
Query
Query H- M Server
Coordinator
Query
N-T Server
Query
U-Z Server
Parallel Query Operations
With Hints
Select /*+ Full(table) Parallel(table [degree]) */
from time_history
PARALLEL_AUTOMATIC_TUNING (8i)----False
LARGE_POOL_SIZE (8i)
Tuning Parallel Execution
PARALLEL_AUTOMATIC_TUNING
Default = FALSE
V$ views:
v$pq_sysstat
v$pq_sesstat
v$pq_tqstat
v$pq_slave
Explain Plan
Hash join
Join costs
Cost of accessing outer table+ (cardinality of outer table*cost of accessing inner table)
(Cost of accessing outer table+outer sort cost)+(Cost of accessing Inner table+Inner sort cost)
Hash join
(Cost of accessing outer table)+(Cost of building hash table)+ (Cost of accessing Inner table)
Understanding Driving Tables
This is the table that dictates how the data is retrieved from the database
Oracle parses queries from right to left through the FROM clause
Select T1.A, T1.B, T2.X, T2.Y from T1, T2 Where T1.A = 10 and T1.B = T2.X;
The table that returns the fewest number of rows the fastest
Oracle compares each row of an inner set with Each row of the outer set
and returns those rows That satisfy the condition
Dept—100 rows---50%
Nested Loop Join ...
Each row from the outer table that satisfies its local predicates is
joined with rows from the inner table.
This process is repeated for every qualifying row of the outer table.
Index, preferably a unique index exists on the join column of the inner table.
A hash table is built for each partition that fits into memory.
Partitions that don’t fit into memory are placed onto disk.
Oracle reads, filters, and usually sorts, the qualifying rows in each of the
tables independently.
The two intermediate result tables are sorted in the same sequence based
on the join predicate column(s).
Oracle generates a subset each index containing the columns referenced from
that index which meet the WHERE conditions by performing a Range Scan or a
Full Scan on each index
Benefit
A Full Table Scan can be avoided without creating an additional index
I hear and I forget
I do and I understand
Questions?
Feel Free to Contact Me
98840----70711
110038