Vous êtes sur la page 1sur 2

c 




  
   


This is a one page list of simple guidelines to tune SQL without much DBA knowledge.

This is all you need to know to tune some reports from 2 hours (or more) to 20 seconds (or less).
I have compiled this list during a report tuning assignment
where I achieved that kind of runtime improvements repeatedly.

There are many more tips but they tend to be more difficult to understand and there are many
good books already written about this.

jY ¦nderstand the data. Look around table structures and data. Get a feel for the data model
and how to navigate it.
jY If a view joins 3 extra tables to retrieve data that you do not need, don't use the view!
jY ^hen joining 2 views that themselves select from other views, check that the 2 views that
you are using do not join the same tables!
jY Avoid multiple layers of view. For example, look for queries based on views that are
themselves views. It may be desirable to encapsulate from a development point of view.
But from a performance point of view, you loose control and understanding of exactly
how much task loading your query will generate for the system.
jY Look for tables/views that add no value to the query. Try to remove table joins by getting
the data from another table in the join.
jY ^  ISTS sub-queries can be better than join if can you reduce drastically the
number of records in driver query. Otherwise, join is better.
jY ^  ISTS can be better than join when driving from parent records and want to
make sure that at least on child exists. Optimizer knows to bail out as soon as finds one
record. Join would get all records and then distinct them!
jY In reports, most of the time fewer queries will work faster. ach query results in a cursor
that eports has to open and fetch. See eports ef Manual for exceptions.
jY Avoid NOT in or NOT = on indexed columns. They prevent the optimizer from using
indexes. ¦se where amount > 0 instead of where amount != 0.
jY Avoid writing where project_category is not null. nulls can prevent the optimizer from
using an index.
jY Îonsider using IN or ¦NION in place of O on indexed columns. Os on indexed
columns causes the optimizer to perform a full table scan.
jY Avoid calculations on indexed columns. ^rite ^  approved_amt > 26000/3 instead
of ^  approved_amt/3 > 26000.
jY Avoid this: S¦BST(haou.attribute1,1,LNGT (':p_otc')) = :p_otc). Îonsider this:
^  haou.attribute1 like :p_otc||'%'
jY Talk to your DBA. If you think that a column used in a ^  clause should have an
index, don't assume that an index was defined. Îheck and talk to your DBA if you don't
find any.
jY Îonsider replacing outer joins on indexed columns with ¦NIONs. A nested loop outer
takes more time than a nested loop unioned with another table access by index.
jY Îonsider adding small frequently accessed columns (not frequently updated) to an
existing index. This will enable some queries to work only with the index, not the table.
jY Îonsider NOT ISTS instead of NOT IN.
jY If a query is going to read most of the records in a table (more than 60%), use a full table
scan.
jY Try to group multiple sub queries into one.

Beyond the Simple stuff ...

jY If you want to actually understand what you are doing, here are a few things that you
need to start playing with:
jY Get into LAIN_LAN. There are multiple way of doing this. The less user friendly
is to simply issue this in SQL*lus: explain plan set statement_id = ' DD1' for <Your
DML SQL statement>;
jY Look at the trace from Oracle eports. It tells you how much time it spends on each
query. ^ith r25: Î:\OANT\BIN\25¦N32. module=p:\old\bcmtrka1_hdd.rdf
userid=opps/opps@new tracefile=p:\trace3.txt trace_opts=(trace_all)
jY ¦se the SQL Trace by issuing an alter session set sql_trace=true; then look at it with
TKOF <something>.trc <something>.lis sort=(Φ).

If you remember nothing else ...

jY Don't apply these guidelines blindly, IMNT: compare one method to another.
Do NOT expect that one trick will work all the time.
jY ducate yourself: read, read, read. It SAVS time!

Vous aimerez peut-être aussi