Module 18 - Database Tuning
Module 18 - Database Tuning
TUNING SQL
Most SQL tuning requires the DBA to work with an application developer.
Most improvement in database processing will come from tuning SQL.
The key to SQL tuning is to minimize the search path that a database uses to find
data. For the most part this requires the creation of appropriate indexesthat the
database engine will use to find data.
Indexes
An Index enables Oracle to locate a row according to its physical location in a datafile
by going to the correct file, finding the correct block, then finding the correct row within
the block. Taken together, these values are like relative record numbers in a relative
file.
The File ID portion of ROWID can be compared to the FILE_ID column of
the DBA_DATA_FILES view to determine the file to which an index belongs.
A query with no WHERE clause normally results in a full table scan, reading every
block in a table.
A query for specific rows may cause use of an index.
o The index maps logical values in a table (key columns) to their ROWIDs which
enables location of the rows directly by their physical location.
You may index several columns together to form a concatenated index.
o A concatenated index is only used if its leading column is used in the
query's WHERE clause.
o Consider the following example:
CREATE INDEX City_state_zip_ndx
ON Employee (City, State, Zip)
TABLESPACE Indexes;
SELECT *
FROM Employee
WHERE State = 'NJ';
The index is NOT used because the WHERE clause value of State does not match the
leading column (City) of the index.
This example would only add overhead because the index would be maintained, even
if it's not used.
The index should be recreated with proper ordering of the component fields in the
index if the query is executed often.
Ordering Data
While row ordering does not matter in relational theory, it is important to order rows as
much as possible when tables are initially created, e.g. when you are porting a system
into Oracle from another platform or DBMS.
Ordered rows may enable Oracle to find needed rows while minimizing the data blocks
that are retrieved where users execute queries that specify ranges of values (recall
the BETWEEN operator in SQL for range of value queries).
Consider the following example which will require fewer data blocks to be read if the
records are physically ordered on the Empno field.
SELECT *
FROM Employee
WHERE Empno BETWEEN 1 and 100;
You can physically sort table rows by SELECTing them to another file with use of the
ORDER BY clause, then truncating the original table and loading the rows back into
the original table.
Clusters
We covered indexed clusters in an earlier module.
Another type of cluster, the hash cluster, stores rows in a specific location based on
its value in the cluster key column.
o Every time a row is inserted, its cluster key value is used to determine which
block to store the row in.
o This enables hashing directly to data blocks without use of an index.
o The hash cluster is only used with equivalence queries - where the exact value
stored in a column is to be found.
Explain Plan
The EXPLAIN PLAN command shows the execution path for a query and stores this
information to a table (PLAN_TABLE) in the database. You can then query the
table. Example:
EXPLAIN PLAN
SET Statement_id = 'TEST'
FOR
SELECT *
FROM Employee
WHERE city > 'Y%';
The query above is not actually executed; rather the plan for execution is stored to
the PLAN_TABLE.
Your account must have a PLAN_TABLE in your schema. The script to create this
table is UTLXPLAN.SQL and is located in
the$ORACLE_HOME/rdbms/admin subdirectory.
Query the table to produce the output that shows the execution path.
SELECT LPAD(' ',2*LEVEL) || operation
|| ' ' || options ||
' ' || object_name Path_Plan
FROM Plan_Table
WHERE Statement_id = 'TEST'
CONNECT BY PRIOR Id = Parent_id
AND Statement_id = 'TEST'
START WITH Id=1;
Path_Plan
-----------------------------------
TABLE ACCESS BY ROWID EMPLOYEE
INDEX RANGE SCAN CITY_ST_ZIP_NDX
The output shows that data will be accessed by ROWID through an index range scan
of the named index.
Alternatively, you can also use the SET AUTOTRACE ON command in SQL*Plus to
generate the explain plan output and trace information for every query that you run.
Evaluate the output by ensuring that the most selective (most nearly unique) indexes
are used by a query.
If you have lost hits, the system will require additional physical reads - the Hit Ratio for
this new number of data buffers is:
Hit Ratio =
(Logical Reads - Physical Reads - Lost Hits)
/ Logical Reads
Since running the database in a statistics gathering mode will slow it down due to the
additional overhead, you should comment out
theDB_BLOCK_LRU_STATISTICS parameter after you have finished tuning and
restart the database.
If a segment is fragmented, you can rebuild the object into a single segment by using
the proper size for the storage parameters. Export the data for the segment, recreate
the object, then import the data into the INITIAL extent.
END OF NOTES