Sunteți pe pagina 1din 21

SQL Performance Tuning

An insight for Beginners Anil Kumar Ghorakavi

1 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Agenda

• Overview of SQL Processing


• Performance Diagnostics
• Understanding Explain Plans
• Basic Hints and their Usage
• Case Studies
• Writing performance efficient code

2 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Session 1: Overview of SQL Processing Review

3 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Session 1:Overview of SQL Processing Review
• Without enough information, Optimizer can't optimize
its tasks.

In the world of SQL, we have a very easy way to give


Oracle that information: statistics.

Statistics tell Oracle things about the objects you are


querying, like number of distinct values in a column, avg
number of rows per data block, avg row size, number of
leaf blocks in an index, cpu utilization

4 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Session 2: Performance Diagnostics Review
• Close to 90% of the performance issues worked on by
EBS development are related to SQL processing.

• Most of these SQL processing issues are issues due to


execution plan. So it is important for us to understand the
execution plan for an SQL statement.

• Preliminary Diagnostic Data Set:


– SQL Trace - Raw Trace and TKPROF
– SQLT XTRACT Report for bad performing SQLs in tkprof/AWR
– AWR report for the same period
– OS Stats for the same period

5 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Session 3: Understanding the execution Review
plan
• Cardinality– Estimate of the number of rows coming out of
each of the operations.
• Access method – The way in which the data is being
accessed, via either a table scan or index access.
• Join method – The method (e.g., hash, sort-merge, etc.)
used to join tables with each other.
• Join type – The type of join (e.g., outer, anti, semi, etc.).
• Join order – The order in which the tables are joined to
each other.

6 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Session 4: Basic Hints and their
Usage

• Hints give specific information that we know about our


data and application.

• A way to override the default query optimization in the


DBMS

• Influence the execution plan of query

7 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Why use Hints?

• Oracle comes with an optimizer that promises to optimize


a query's execution plan. When this optimizer is really
doing a good job, no hints should be required at all.
• Sometimes, however, the characteristics of the data in the
database are changing rapidly, so that the optimizer (or
more accurately, its statistics) are out of date
• Oracle optimizer may not always choose the best
execution plan
• Using hints may improve the performance by changing the
execution plan oracle takes.

8 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Using Hints

• Hints can be used in the Select, Delete, and Update


clauses.
• In each statement, the hint goes directly after the Select,
Delete, or Update keyword. A few hints use Insert.
• Hints are placed in the /*+ */ tag, where the hint goes after
the + sign
• Ex: SELECT /*+ ALL_ROWS */ From…
• Hints must use aliases if alias names are used for a table
names

9 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Hints by Category

• Hints for Optimization Approaches and Goals


• Hints for Access Paths
• Hints for Query Transformations
• Hints for Join Orders
• Hints for Join Operations
• Hints for Parallel Execution
• Additional Hints

10 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Hints for Optimization Approaches and
Goals
• ALL_ROWS: Uses cost based approach for the best
throughput. This is the default approach in 11G
SELECT /*+ ALL_ROWS */ trx_number
FROM ra_customer_trx_all;
• FIRST_ROWS(n): Uses cost based approach for the best
response time.
SELECT /*+ FIRST_ROWS(30) trx_number
FROM ra_customer_trx_all
ORDER BY customer_trx_id;
• CHOOSE: If statistics are available use CBO, otherwise
use RBO

11 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Hints for Access Paths
• FULL: This tells the optimizer to do a full scan of the
specified table
SELECT /*+ FULL(ct) */ trx_number
FROM ra_customer_trx_all ct;
• INDEX: Forces an index scan of the specified table using
the specified index
SELECT /*+ INDEX(ct RA_CUSTOMER_TRX_N1)
trx_number
FROM ra_customer_trx_all ct;
• NO_INDEX: This tells the optimizer not to use a specific
index

12 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Hints for Query Transformations
• MERGE and NO_MERGE
View merging simply means the query is rewritten without
the view. It is as if instead of: 
select * from table, VIEW;
we actually typed in the VIEW text.
• select e.first_name, e.last_name, dept_locs_v.street_address,
dept_locs_v.postal_code from employees e,
(select d.department_id, d.department_name, l.street_address, l.postal_code
from departments d, locations l where d.location_id = l.location_id)
dept_locs_v
where dept_locs_v.department_id = e.department_id and e.last_name =
'Smith';
• When the MERGE hint is used without an argument, it
should be placed in the view query block.
When MERGE is used with the view name as an
argument, it should be placed in the surrounding query.
13 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Hints for Query Transformations
• UNNEST and NO_UNNEST
UNNEST allows a subquery to be rewritten as a join, so that 
select * from t1
where exists (select null from t2 where t2.key = t1.key) 
can be rewritten by unnesting the subquery and joining to it 
select t1.* from t1, t2 where t2.key = t1.key; 
if that is deemed more efficient. 
• Ex. UPDATE ar_cash_receipt_history_all
SET …….
WHERE posting_control_id = -3
AND (cash_receipt_id, event_id) IN
(SELECT /*+ UNNEST) ev. source_id_int_1, ev.event_id
FROM xla_post_acctg_events_v ev
…………….. );

14 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Hints for Join Orders
• LEADING: Give this hint to indicate the leading table in a
join.
SELECT /*+ LEADING(adj) */ ps.customer_trx_id
FROM ar_adjustments adj,
ar_payment_schedules ps
WHERE ……..
• ORDERED: The ORDERED hint causes Oracle to join
tables in the order in which they appear in the FROM
clause
SELECT /*+ ORDERED */ ps.customer_trx_id
FROM ar_adjustments adj,
ar_payment_schedules ps
WHERE ……..

15 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Hints for Join Operations
• USE_NL: The USE_NL hint causes Oracle to join each
specified table to another row source with a nested loops
join using the specified table as the inner table.
SELECT /*+ USE_NL(adj, ps) */ ps.customer_trx_id
FROM ar_adjustments adj,
ar_payment_schedules ps
WHERE ……..
• USE_HASH: The USE_HASH hint causes Oracle to join
each specified table with another row source with a hash
join.
SELECT /*+ USE_HASH(adj, ps) */ ps.customer_trx_id
FROM ar_adjustments adj,
ar_payment_schedules ps
WHERE ……..

16 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Hints for Parallel Execution
• PARALLEL: The PARALLEL hint allows you to specify the
desired number of concurrent query servers that can be
used for the query.
INSERT /*+ parallel(a) append */ into ar_autorec_interim a
………………

17 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Additional Hints
• APPEND: When the APPEND hint is used with the
INSERT statement, data is appended to the table. Existing
free space in the block is not used.
INSERT /*+ parallel(a) append */ into ar_autorec_interim a
………………
• PUSH_PRED: The PUSH_PRED hint forces pushing of a
join predicate into the view or an inline view.
SELECT /*+ PUSH_PRED(trx_extn) */ …..
FROM ar_cash_receipts cr,
(SELECT ….
FROM iby_fndcpt_tx_operations
WHERE …..) trx_extn
WHERE ….
AND cr. payment_trxn_extension_id = trxn_ext.trxn_extension_id(+) ……

18 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Additional Hints
• PUSH_SUBQ: The PUSH_SUBQ hint causes nonmerged
subqueries to be evaluated at the earliest possible place in
the execution plan.

SELECT L.INTERFACE_LINE_ID, ….
FROM RA_INTERFACE_LINES_GT L
WHERE …..
AND NOT EXISTS
(SELECT /*+ PUSH_SUBQ NO_UNNEST */ 'X'
FROM MTL_ITEM_UOMS_VIEW UOM
…………..)
…………;

19 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
When to use Hints?
• Hints are the path of last resort.

• Hints are hints. A valid, well formed hints should be


considered by the optimizer.

• Most of hints should be automatically taken care by the


optimizer if it is provided with necessary statistical details
about the data.

• Few hints like FIRST_ROWS, ALL_ROWS,


DYNAMIC_SAMPLING etc, provide more information to
the optimizer and these can be used in the code directly.

20 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential
Session 4: Basic Hints and their
Usage
• Try to understand the intention behind the suggestions
provided by the performance team when they ask us to
add Hints.

• Go through existing AR code files and understand the


usage of Hints

21 |Copyright © 2011 Oracle Corporation. Oracle and/or its affiliates. All rights reserved. Oracle Proprietary and Confidential

S-ar putea să vă placă și