advertisement
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
- 3 Contents
- 9 Introduction
- 13 Installation
- 13 System requirements
- 13 Workstation installation
- 13 Server based installation
- 14 Scripted installation
- 14 Uninstalling PL/SQL Developer
- 15 Writing programs
- 15 Creating a program
- 16 Saving a program
- 17 Modifying a program
- 17 Compiling a program
- 18 Substitution variables
- 19 Testing programs
- 19 Creating a Test Script
- 20 Executing a Test Script
- 21 Variable types
- 22 Saving Test Scripts
- 23 Tracking run-time errors
- 23 Package states & Java session states
- 23 Viewing result sets
- 23 Viewing dbms_output
- 24 Viewing HTP output
- 24 Debugging
- 27 Tracing execution
- 28 Regression testing
- 29 Optimizing
- 29 Using the Explain Plan Window
- 30 Automatic statistics
- 31 The PL/SQL Profiler
- 32 SQL Trace
- 34 Ad hoc SQL
- 34 Using the SQL Window
- 35 Result grid manipulation
- 39 Query By Example mode
- 40 Linked Queries
- 40 Substitution variables
- 41 Updating the database
- 42 Viewing and editing XMLTYPE columns
- 42 Direct Query Export
- 42 Saving SQL Scripts
- 43 Creating Standard Queries
- 44 The Command Window
- 44 Entering SQL statements and commands
- 45 Developing command files
- 46 Supported commands
- 49 Creating and modifying non-PL/SQL objects
- 49 The table definition editor
- 60 The sequence definition editor
- 60 The synonym definition editor
- 61 The library definition editor
- 61 The directory definition editor
- 62 The job definition editor
- 62 The queue definition editor
- 63 The queue table definition editor
- 64 The user definition editor
- 67 The role definition editor
- 68 The profile definition editor
- 68 The database link definition editor
- 69 Diagrams
- 69 Creating a diagram
- 71 Saving and opening a diagram file
- 71 Updating a diagram
- 72 Reports
- 72 Standard reports
- 73 Custom reports
- 74 Variables
- 77 Refining the layout
- 84 The Style Library
- 84 Options
- 86 The Reports menu
- 87 Graphics
- 89 Projects
- 89 Creating a new project
- 89 Saving a project
- 90 Adding files to a project
- 90 Adding database objects to a project
- 90 Working with project items
- 91 Compiling a project
- 92 To-Do Items
- 93 Creating a To-Do Item
- 93 Editing a To-Do Item
- 94 Closing a To-Do Item
- 94 Deleting a To-Do Item
- 95 Windows, database sessions and transactions
- 95 Session mode
- 95 Execution in Multi session or Dual session mode
- 96 Browsing objects
- 96 Using the Browser
- 101 Browser Filters
- 102 Browser Folders
- 104 Preferences
- 105 Oracle – Connection
- 106 Oracle – Options
- 107 Oracle – Debugger
- 108 Oracle – Output
- 109 Oracle – Trace
- 109 Oracle – Profiler
- 110 Oracle – Logon History
- 111 Oracle – Hints
- 113 User Interface – Options
- 115 User Interface – Toolbar
- 115 User Interface – Browser
- 117 User Interface – Editor
- 120 User Interface – Fonts
- 121 User Interface – Code Assistant
- 122 User Interface – Key Configuration
- 123 User Interface – Appearance
- 124 User Interface – Date/Time
- 125 Window Types – Program Window
- 127 Window Types – SQL Window
- 129 Window Types – Test Window
- 129 Window Types – Plan Window
- 129 Tools – Differences
- 130 Tools – Data Generator
- 131 Tools – To-Do List
- 131 Tools – Recall Statement
- 132 Files – Directories
- 133 Files – Extensions
- 134 Files – Format
- 134 Files – Backup
- 135 Files – HTML/XML
- 136 Other – Printing
- 137 Other – Updates & News
- 138 Preference sets
- 140 Tools
- 140 Browser
- 140 Find Database Objects
- 141 Compile Invalid Objects
- 142 Export Tables
- 145 Import Tables
- 147 Export User Objects
- 148 Text Importer
- 151 ODBC Importer
- 153 Data Generator
- 156 Compare User Objects
- 158 Compare Table Data
- 160 Event Monitor
- 161 Sessions
- 163 User Defined Tools
- 167 Test Manager
- 170 The Editor
- 170 Selection functions
- 170 Column selection
- 171 Code Assistant
- 172 Recalling statements
- 172 Special Copy
- 173 Context sensitive help
- 174 Database object popup menu
- 174 Explain Plan
- 174 Macros
- 175 Bookmarks
- 176 Color marks
- 177 Code Contents
- 178 Code Hierarchy
- 179 Code Folding
- 179 Split Editing
- 180 Hyperlink navigation
- 180 Navigation buttons
- 181 Refactoring
- 181 Search Bar
- 183 The Large Data Editor
- 184 Editing plain text
- 184 Editing RTF
- 185 Editing XML
- 186 Editing images
- 186 Editing Hex data
- 187 Invoking an external viewer or editor
- 189 The Query Builder
- 189 Creating a new select statement
- 192 Modifying an existing select statement
- 192 Manipulating the query definition
- 193 Query Builder Preferences
- 194 Query Builder Plug-Ins
- 195 The PL/SQL Beautifier
- 195 Defining the options
- 196 Defining the rules
- 197 Using the beautifier
- 198 Templates
- 198 The Template Window
- 199 Using a template
- 200 Creating and modifying templates
- 205 Window List
- 206 Dockable and Floating Tools
- 207 Authorization
- 207 Enabling authorization
- 208 Defining authorization
- 209 Disabling authorization
- 210 Oracle File System (OFS)
- 210 OFS Manager
- 212 OFS Usage
- 214 Help systems
- 214 MS Help files
- 214 HTML Manuals
- 218 Customization
- 218 Preferences
- 218 Window layout
- 218 On-line documentation
- 220 Command-line parameters
- 222 SQL, PL/SQL, Command, Java and XML keywords
- 222 Plug-Ins