A collection of tutorials, code and tools to help you get better using Oracle
10 June 2011
The most accessible benchmarking tool in Oracle is probably Autotrace, which allows basic statistics and execution plan information to be obtain for any query easily.
First, you need some data to test with. In an actual development scenario, you may have a test database already full of real data you can experiment with, but we can create something simple:
SQL11G> create table test1
as
select * from all_objects;
SQL11G> create index test1_uk1 on test1 (object_id);
SQL11G> exec dbms_stats.gather_table_stats(user, 'TEST1');
Now we have a fairly small table with a unique index, which gives us something to play with. On my database, the table contains about 65,000 rows.
Autotrace is a tool built into SQLPlus that helps a developer evaluate how expensive a piece of SQL is at execution time. It is probably the easiest method of getting information about an SQL statement. Depending on what mode it is run in, it can display:
The usage information in the manual for Autotrace is:
Usage: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]
This is a little confusing. The full commands are:
-- Turn everything on. Query results, stats and explain plan are returned
set autotrace on
-- Get stats and Explain plan, but suppress the query results
-- This can save a lot of time watching them 'page' past in the terminal window
set autotrace traceonly
-- Get stats only
set autotrace traceonly statistics
-- Get the query results and the explain plan
set autotrace on explain
So lets test this out in SQLPlus:
SQL11G> set autotrace on
SQL11G> select object_id from test1 where rownum = 1;
OBJECT_ID
----------
2
Execution Plan
----------------------------------------------------------
Plan hash value: 3474931915
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 6 | 1 (0)| 00:00:01 |
|* 1 | COUNT STOPKEY | | | | | |
| 2 | INDEX FULL SCAN| TEST1_UK1 | 1 | 6 | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM=1)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
516 bytes sent via SQL*Net to client
488 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Notice that the query results come first, then the execution plan, which is also known as the explain plan. Finally the query statistics. The query statistics need some more explanation, although some of them are self explanatory.
As you can see, autotrace is simple to use, and most of the stats it returns are easy to understand. The general rule of thumb for Autotrace results is:
Less is better
If you can reduce any of the stats it returns, then the query should be more efficient. Learning how to reduce the numbers is a topic for another day, as this section is focused on teaching you about the tools to compare one method to another. Query tuning skills can be developed after you learn how to measure what is better.