PL/SQL Developer 7.1 User`s Guide

Add to My manuals
222 Pages

advertisement

PL/SQL Developer 7.1 User`s Guide | Manualzz

PL/SQL Developer 7.1 User’s Guide 29

5. Optimizing

To optimize the SQL statements in your program units, Oracle's Explain Plan utility can be a big help by showing the execution path of a statement. Therefore it is integrated into PL/SQL Developer's IDE.

To view the actual resource use of a SQL statement or PL/SQL program unit, PL/SQL Developer can display statistics about its execution. You can configure which statistics you wish to display, and can include elapsed time, CPU time, logical reads, physical reads, physical writes, and so on.

To determine the execution time of each individual line of PL/SQL code, you can use the PL/SQL

Profiler. This feature is not available on Oracle 8.0 or earlier.

Finally, you can use Oracle's tkprof utility to get resource use information about all executed SQL statements in a program unit by enabling SQL Trace.

5.1 Using the Explain Plan Window

To use Oracle's Explain Plan utility, press the New button on the toolbar and select Explain Plan

Window. An empty Explain Plan Window appears. In the top half of the window, you can type the SQL statement you wish to analyze. After pressing the Execute button on the toolbar the execution plan is displayed in the bottom half of the window:

You can now change the SQL statement and press the Execute button again to see the impact of the changes. For more information about execution plans, you can read Oracle’s Server Tuning manual.

To see the effect of different optimizer goals on the query plan, select the corresponding entry from the

Optimizer goal list. The plan will immediately be updated to reflect these changes.

Use the First, Previous, Next, and Last operation buttons to navigate through the query plan in order of operation. After the plan has been determined, the first operation will be highlighted.

The Explain Plan utility uses a so-called plan table to store the execution plan. If such a table is not available to the current user, PL/SQL Developer will ask you if it should create this table in the schema of the current user. To define which columns from the plan table you want to see, and in which order, press the Preferences button. This will bring up the corresponding preference page (see chapter 16.21).

Note: If you are using Oracle Server 7.2 or earlier, the cost and cardinality are not available in the plan table. If you are using Oracle Server 7.3 or higher and cost and cardinality are not displayed, you probably need to upgrade the plan table.

If you right-click on the Object name column, this will bring up the popup menu for the selected object.

30 PL/SQL Developer 7.1 User’s Guide

Analyzing SQL in a program unit

Most of the time, the SQL statements you wish to analyze are contained in the source of a program unit.

To do this, you can select the SQL statement in the Program Editor and select Explain Plan from the

Help menu (or press F5). A new Explain Plan Window is created for the selected SQL statement, and all PL/SQL variables are replaced by bind variables.

If you explicitly copy & paste a SQL statement that contains PL/SQL variables from the program unit and you execute the Explain Plan window, you will receive an "ORA-00904: Invalid column name" error. The Explain Plan utility mistakenly assumes all these variables to be columns. Each variable must therefore be prefixed with a colon. If we wanted to analyze the SQL statement in the

employee.deptname function, the result and p_empno variables would have to be altered in the following way:

Now the Explain Plan utility knows which identifier is a column, and which identifier is a PL/SQL variable.

5.2 Automatic statistics

When you execute a statement in a SQL Window or in a Test Window, PL/SQL Developer will automatically generate a statistic report of this execution. One condition is that you need to have select privileges on the dynamic performance tables v$session, v$statname and v$sesstat (provided through the standard plustrace role).

You can view the statistic report by changing to the Statistics tab at the top of the SQL Window or Test

Window. The statistics for the execution of the employee.deptname function might look as follows:

PL/SQL Developer 7.1 User’s Guide 31

For each statistic you see the value of the last execute, and the total for the current session. You can configure if and which statistics are displayed by setting a preference, as described in chapter 16.2. The default set of statistics is relevant to the tuning of your SQL and PL/SQL, and is described here:

Statistic Meaning

CPU used by this session The CPU usage in hundredths of a second

Physical reads

Physical writes

The number of blocks read from disk

The number of blocks written to disk session logical reads sorts (disk) sorts (memory) sorts (rows)

The number of blocks read from the block buffer or from disk

The number of sorts performed in a temporary segment

The number of sorts performed in memory

The number of rows that were sorted table fetch by rowid The number of rows fetched by rowid, usually as a result of index accesses table scan blocks gotten The number of blocks read for full table scans table scan rows gotten The number of rows read for full table scans table scans (long tables) The number of full table scans on long tables table scans (short tables) The number of full table scans on short tables

Which other statistics you can include depends on the version of the Oracle Server and are not described in this manual. If you wish to get information about them, there are many Oracle tuning books available that address this topic. The Oracle Server Reference also briefly describes these statistics.

The statistics can be exported to a CSV file (Comma Separated Values) that can be opened in a spread sheet application later. Just right-click on the statistics, select the Export item and choose the CSV file item. You can alternatively select a TSV, XML or HTML format, or copy it to the clipboard.

5.3 The PL/SQL Profiler

The PL/SQL Profiler is a very powerful tool to help you optimize your PL/SQL code. For each executed line of code, the total time, maximum time, minimum time, average time, and the number of occurrences will be determined.

The Profiler is easily accessible in the Test Window. Before executing a Test Script, simply press the

Create Profiler report button on the toolbar of the Test Window. If you subsequently execute the script, you can switch to the Profiler page to view the report.

32 PL/SQL Developer 7.1 User’s Guide

The following example report shows that in the employee.deptname function, the select statement took

149 milliseconds, and the 3 dbms_output calls took 57 milliseconds:

By default the Profiler page will display the report of the last run. You can also select previous runs from the Run list. The Unit list allows you to zoom in on a specific program unit of a run.

The Total time column shows a graphical representation of the relative time of the line, compared to the line with the highest time. This allows you to quickly identify the lines that are most expensive. The report can be sorted by pressing on the sort-buttons in the heading of the columns.

If a source line is displayed in red, this means that the program unit has been changed since the profile report was created. Therefore the displayed line of code can now be different than when the profile was created.

You can change various layout aspects of the Profiler report by pressing the Preferences button. This will bring up the corresponding preference page, as described in chapter 16.6.

For more information about the PL/SQL Profiler, see the dbms_profiler chapter in the “Oracle8i

Supplied Packages Reference” manual.

Note: not all platforms provide equally accurate timing information.

5.4 SQL Trace

You can enable SQL Trace by pressing the SQL Trace button on the toolbar. After this, all server processing caused by a SQL Window or Test Window will be logged in a trace file on the database server. One condition is that the timed statistics parameter of the database instance you are using must be set to true.

You can disable SQL Trace by pressing the SQL Trace button again.

You can view the information in the trace file by using Oracle's tkprof utility on the database server. It will generate a report about elapsed time, CPU time, I/O, and so on for each SQL statement. Therefore, it can give you some insight into which statements are the most costly in a program unit. To learn more about Oracle's tkprof utility, you might read the "Oracle 7 Server Tuning" manual.

The tkprof report for the execution of the employee.deptname function might look as follows:

PL/SQL Developer 7.1 User’s Guide

TKPROF: Release 7.2.2.3.1 - Production on Fri Sep 26 14:59:08 1997

Copyright (c) Oracle Corporation 1979, 1994. All rights reserved.

Trace file: ora07087.trc

Sort options: default

******************************************************************************** count = number of times OCI procedure was executed cpu = cpu time in seconds executing elapsed = elapsed time in seconds executing disk = number of physical reads of buffers from disk query = number of buffers gotten for consistent read current = number of buffers gotten in current mode (usually for update) rows = number of rows processed by the fetch or execute call

******************************************************************************** begin

:deptname := employee.deptname(:empno); end; call count cpu elapsed disk query current rows

------- ------ -------- ---------- ---------- ---------- ---------- ----------

Parse 1 0.00 0.00 0 0 0 0

Execute 1 0.01 0.01 0 0 0 1

Fetch 0 0.00 0.00 0 0 0 0

------- ------ -------- ---------- ---------- ---------- ---------- ---------- total 2 0.01 0.01 0 0 0 1

Misses in library cache during parse: 0

Optimizer hint: CHOOSE

Parsing user id: 16

********************************************************************************

SELECT DEPT.DNAME

FROM

DEPT,EMP WHERE EMP.EMPNO = :b1 AND DEPT.DEPTNO = EMP.DEPTNO call count cpu elapsed disk query current rows

------- ------ -------- ---------- ---------- ---------- ---------- ----------

Parse 1 0.00 0.00 0 0 0 0

Execute 1 0.00 0.00 0 0 0 5

Fetch 1 0.06 0.06 4 4 0 1

------- ------ -------- ---------- ---------- ---------- ---------- ---------- total 3 0.06 0.06 4 4 0 6

Misses in library cache during parse: 0

Optimizer hint: CHOOSE

Parsing user id: 16 (recursive depth: 1)

********************************************************************************

33

advertisement

Related manuals

advertisement

Table of contents