SQL Statement Optimization Guide
SQL Statement Optimization Guide
SQL Statements
Analyzing
SQL
Statements
Contents:
1. Recognizing expensive statements
4. Table statistics
Objectives:
1. At the end of this unit you will be able to:
2. Determine if and why a statement is expensive, using the:
1. Shared SQL Area
2. Explain Function
n The WHERE clause specifies which records are searched from these tables. In the case of joins,
these records are also restricted by the join condition. The fie lds specified in the WHERE clause
also determine which indexes are used.
n The search area is the set of data (records) that are searched to evaluate the query. This set is not
explicitly specified in the query. It is determined by the optimizer when the statement is evaluated,
using the indexes of the tables.
n The goal of the database optimizer is to reduce the search area as much as possible. This can be
achieved by SQL statement tuning or by technical tuning such as creating indexes or changing
view definitions.
n When you analyze a statement, classify the statement first, to find the possible tuning methods.
n There are two different types of statements:
Type 1 statements search through a lot of data for only a few qualified records. Here the
optimizer chooses an unsuitable path to access the data.
Type 2 statements request a lot of records. Here the optimizer chooses a suitable access
path.
n In this example, the application requests the records of table ZVBAK where MANDT (the
client) equals 001 and the field ERNAM (the name of person who created the record or object)
is 'ERNIE'. Many records are read, although only two are requested. Therefore, this statement is
of Type 1.
n Creating an index
n In this example, the application requests all records of table ZVBAK where MANDT = 001.
Because the database returns all the records that are read, this statement is of type 2.
n A statement of type 2, that returns many records, cannot be accelerated by using an index. To
accelerate this type of statement, you can:
• Tune the ABAP report
• Tune the business process
• Optimize the user input
n Before you analyze the Shared SQL Area, check that there have been well over a million user
calls. Then the database has processed enough statements to make the analysis meaningful.
n A statement that causes a lot of buffer gets is expensive with respect to system resource
consumption. Such statements can reduce overall database performance.
n A statement that causes a lot of disk reads can critically affect the system I/O performance and
reduce overall system performance.
n A statement that reads a lot of records can reduce the performance of both the database and
application servers. Statements of this type may be coded inefficiently.
n Every SQL statement has its own hit ratio.
n Terminology:
• Buffer gets are called reads in the Database Overview Monitor
• Disk reads are called physical reads in the Database Overview Monitor
Few executions
Many with many buffer
executions gets per execution
Application Application
server server
n To display the statements that cause the highest database load, sort by buffer gets.
n The statements at the top of the list cause the highest consumption of database resources. These
statements are either executed very often, with a small number of buffer gets per execution, or
they have a very high number of buffer gets per execution. These statements can reduce the
global database performance.
n An R/3 System has expensive statements if:
• The number of buffer gets for the topmost statement exceeds 5% of the total number of
reads.
• The ratio of reads to user calls is greater than 15.
n Check any statements for which the number of buffer gets exceeds 5% of the total reads.
n If the ratio of reads to user calls is much greater than 15, check at least the topmost statements.
n If there are many expensive statements, each statement has only a small fraction of the total
number of reads. In this case, the ratio of user calls to reads is the best indicator of expensive
statements.
Application
server
n The statements that cause a high database load while being executed have a high number of
buffer gets per execution. These statements can reduce the performance of all other statements
running at the same time. If these statements are are executed during background operation,
when online users are not logged on to the system, they are less critical.
n To determine when the statement was executed for the first time, choose Next info from the
Shared SQL Area and check column 1. Load Time.
Records specified by
the WHERE clause Database
buffer
Search area
(the data that is
searched through
for the requested
data)
Application
server
n Statements with a high number of buffer gets per record (compared to the size of a record) use
an unsuitable access path. This means that the search area is unnecessarily large.
n However, if the records returned by the statement are often zero, the number of buffer gets per
record can be very large although the statement is executed with a suitable access path.
Therefore, always compare the number of records processed to the number of executions. If the
buffer gets per execution is low, this statement uses a suitable access path.
n Our experience shows that statements, which often return no records, can be avoided by coding
changes.
Example:
ABAP coding:
Application
server
n Statements with a high number of records per execution may be coded inefficiently.
n In this example, the complete table MARA is read from the database to the application server
when only one record is required.
n Check if column SQL Sort is greater than 0. Sort operations must be avoided for statements that
process many records.
Symptoms:
n High number of disk reads
→ Degraded I/O performance Database server
n Hit rate for the statement is low DBMS processes Database buffer
Causes:
n Large tables
n Low number of rows per block
n Data is aged out of the buffer
n Many full table scans performed for this table
Disk reads
n To display the statements that critically affect system I/O performance, sort by disk reads.
n If the statement cannot be tuned by any other means, it may be advantageous to create
additional tablespaces for the tables that have expensive disk accesses and to locate these
tablespaces on separate disks.
n To display the full SQL statement, double -click the statement in the Shared SQL Area.
n Bind variables are passed from R/3 to Oracle during the parsing of the statement. The values
for the statement are passed to the database at a later point in time when the data is retrieved.
Using bind variables allows Oracle to reuse statements in the Shared SQL Area for queries with
the same structure but different values. If the values of the query were passed to the database
without using bind variables, the statement could not be reused.
SQL Statement
SQL Statements
n To display table fields or indexes, use the ABAP Dictionary (Transaction SE12) or choose DDIC
Info from the Shared SQL Area screen.