You are on page 1of 30

2

Introduction to SQL Tuning

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Objectives

After completing this lesson, you should be able to:


• Describe what attributes of a SQL statement can make it
perform poorly
• List the Oracle tools that can be used to tune SQL
• List the tuning tasks

2-2 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Reasons for Inefficient SQL Performance

• Stale or missing optimizer statistics


• Missing access structures
• Suboptimal execution plan selection
• Poorly constructed SQL

2-3 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Inefficient SQL: Examples

SELECT COUNT(*) FROM products p


1 WHERE prod_list_price <
1.15 * (SELECT avg(unit_cost) FROM costs c
WHERE c.prod_id = p.prod_id)

SELECT * FROM job_history jh, employees e


2 WHERE substr(to_char(e.employee_id),2) =
substr(to_char(jh.employee_id),2)

3 SELECT * FROM orders WHERE order_id_char = 1205

4 SELECT * FROM employees


WHERE to_char(salary) = :sal

SELECT * FROM parts_old


5 UNION
SELECT * FROM parts_new

2-5 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Performance Monitoring Solutions
Automatic
Fore- 60 min
In-memory
ground MMON
statistics
SGA AWR
ASH

Snapshots
Snapshots ADDM

Alerts AST

Statspack
ADDM
results AWR report

ASH: Active Session History


AWR: Automatic Workload Repository
ADDM: Automatic Database Diagnostic Monitor

2-7 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Monitoring and Tuning Tools: Overview

Services

EM Metric ADDM Direct


Alert SQL Perf AWR Hang
Statspack perf base SPA and SGA analyzer
log traces views reports pages line advisors monitor

tkprof trcsess AWR SQL


baseline report

Base/ Service
Optimizer SQL
Segment Histograms Metrics ASH
statistics statistics statistics Compared
statistics periods

System
Wait Time OS ASH
Session Alerts
model model statistics report
statistics

2-9 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
EM Performance Pages for Reactive Tuning

Home
ASH Run
Report ADDM
Performance

Nonidle Top Top Duplicate Blocking Instance Instance AWR


Hang
wait Activity Consu- SQL Sessions Analysis Locks Activity Baselines
classes mers

Wait
class Top Top Top Top Top Top Top
details SQL Sessions Services Modules Actions Files Objects

SPA

Wait SQL SQL


Enable/Disable View System
event Tuning Tuning Enable/Disable
Advisor aggregation SQL trace file statistics
histograms Sets SQL trace

2 - 10 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Tuning Tools: Overview

• Automatic Database Diagnostic Monitor (ADDM)


• SQL Tuning Advisor
• SQL Tuning Sets
• SQL Access Advisor
• SQL Performance Analyzer
• SQL Monitoring
• SQL Plan Management

2 - 11 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


SQL Tuning Tasks: Overview

• Identifying high-load SQL


• Gathering statistics
• Generating system statistics
• Rebuilding existing indexes
• Maintaining execution plans
• Creating new index strategies

2 - 13 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


CPU and Wait Time Tuning Dimensions

Scalability is a system’s ability to process more workload with a


proportional increase in system resource use.

CPU
time Possibly
needs SQL Scalable
tuning application

Scalable Needs No gain achieved


application instance/RAC by adding
tuning CPUs/nodes

Wait
time

2 - 14 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Scalability with Application Design,
Implementation, and Configuration
Applications have a significant impact on scalability.
• Poor schema design can cause expensive SQL that does
not scale.
• Poor transaction design can cause locking and
serialization problems.
• Poor connection management can cause unsatisfactory
response times.

2 - 15 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Common Mistakes on Customer Systems

1. Bad connection management


2. Bad use of cursors and the shared pool
3. Excess of resources consuming SQL statements
4. Use of nonstandard initialization parameters
5. Poor database disk configuration
6. Redo log setup problems
7. Excessive serialization
8. Inappropriate full table scans
9. Large number of recursive SQL statements related to
space management or parsing activity
10. Deployment and migration errors

2 - 16 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Proactive Tuning Methodology

• Simple design
• Data modeling
• Tables and indexes
• Using views
• Writing efficient SQL
• Cursor sharing
• Using bind variables

2 - 18 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Simplicity in Application Design

• Simple tables
• Well-written SQL
• Indexing only as required
• Retrieving only required information

2 - 19 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Data Modeling

• Accurately represent business practices


• Focus on the most frequent and important business
transactions
• Use modeling tools
• Appropriately normalize data (OLTP versus DW)

2 - 20 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Table Design

• Compromise between flexibility and performance:


– Principally normalize
– Selectively denormalize
• Use Oracle performance and management features:
– Default values
– Constraints
– Materialized views
– Clusters
– Partitioning
• Focus on business-critical tables

2 - 21 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Index Design

• Create indexes on the following:


– Primary key (can be automatically created)
– Unique key (can be automatically created)
– Foreign keys (good candidates)
• Index data that is frequently queried (select list).
• Use SQL as a guide to index design.

2 - 22 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Using Views

• Simplifies application design


• Is transparent to the developer
• Can cause suboptimal execution plans

2 - 23 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


SQL Execution Efficiency

• Good database connectivity


• Minimizing parsing
• Share cursors
• Using bind variables

2 - 24 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Writing SQL to Share Cursors

• Create generic code using the following:


– Stored procedures and packages
– Database triggers
– Any other library routines and procedures
• Write to format standards (improves readability):
– Case
– White space
– Comments
– Object references
– Bind variables

2 - 26 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Performance Checklist

• Set initialization parameters and storage options.


• Verify resource usage of SQL statements.
• Validate connections by middleware.
• Verify cursor sharing.
• Validate migration of all required objects.
• Verify validity and availability of optimizer statistics.

2 - 27 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Development Environments: Overview

• SQL Developer
• SQL*Plus

SQL Developer

2 - 28 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


What Is Oracle SQL Developer?

• Oracle SQL Developer is a free graphical tool that


enhances productivity and simplifies database
development tasks.
• You can connect to any target Oracle database schema
using standard Oracle database authentication.
• You will use SQL Developer in this course.
• Appendix C contains details on using SQL Developer

SQL Developer

2 - 29 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Coding PL/SQL in SQL*Plus

2 - 30 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Quiz

_________automatically identifies bottlenecks within Oracle


Database and makes recommendations on the options
available for fixing these bottlenecks.
a. ASH
b. AWR
c. ADDM
d. Snapshots

2 - 31 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Quiz

Normalizing data results in a good data model, which ultimately


means that your queries are written more efficiently.
a. True
b. False

2 - 32 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Quiz

Views should be used carefully because, though they provide


clean programming interfaces, they can cause suboptimal,
resource-intensive queries when nested too deeply.
a. True
b. False

2 - 33 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Quiz

Identify the characteristics that must be supported by an


application designed for SQL execution efficiency.
a. Use concurrent connections to the database.
b. Use cursors so that SQL statements are parsed once and
executed multiple times.
c. Use bind variables.

2 - 34 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Describe what attributes of a SQL statement can make it
perform poorly
• List the Oracle tools that can be used to tune SQL
• List the tuning tasks

2 - 35 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.


Practice 2: Overview

This practice covers the following topics:


• Rewriting queries for better performance
• Rewriting applications for better performance

2 - 36 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.

You might also like