View
221
Download
0
Category
Preview:
Citation preview
7/30/2019 49012527 Tunning Tip and Tricks Basic
1/44
Oracle Tuning Tips And Tricks
UST Global GE Account
7/30/2019 49012527 Tunning Tip and Tricks Basic
2/44
What is Tuning?
Tuning is anartrequiring creativity
Tuning is asciencerequiring regimen
Thus,we must use both intuitionand rules
7/30/2019 49012527 Tunning Tip and Tricks Basic
3/44
Performance Pyramid
Network
Hardware
OS
DBMS
Application
7/30/2019 49012527 Tunning Tip and Tricks Basic
4/44
Where Should We Look? When we look at the applications we will find mainly two
reasons
a) Wrong Designs
b) ineffective SQLs written
Do your developers know:
Sub-queries (correlated and not)
Outer Joins
Minus
Union and Union-All
Golden Rule #1:Its the application stupid!
7/30/2019 49012527 Tunning Tip and Tricks Basic
5/44
When Can One start tuning?
When I write a Code.
When Issue is reported.
As and When Required.
Tuning as you go may be impractical, and after youre
done it may be too late. Often its best to apply general
tuning guidelines as you go, and then identify problem
areas for improvement.
Golden Rule #2:Best to tune before,during, and after!
7/30/2019 49012527 Tunning Tip and Tricks Basic
6/44
Who will find the needle first?
7/30/2019 49012527 Tunning Tip and Tricks Basic
7/44
Who will find the needle first?
7/30/2019 49012527 Tunning Tip and Tricks Basic
8/44
Who will find the needle first?
7/30/2019 49012527 Tunning Tip and Tricks Basic
9/44
Who will find the needle first?
7/30/2019 49012527 Tunning Tip and Tricks Basic
10/44
Who will find the needle first?
7/30/2019 49012527 Tunning Tip and Tricks Basic
11/44
7/30/2019 49012527 Tunning Tip and Tricks Basic
12/44
Now What
SQL Codes Must Watch
PLSQL Codes Must Watch
7/30/2019 49012527 Tunning Tip and Tricks Basic
13/44
SQL Codes Must Watch
7/30/2019 49012527 Tunning Tip and Tricks Basic
14/44
Indexes
What are indexes?
What are the type of Index available
for me?
How can they help me?
What to Index ?
What Not to Index ?
Are they really good?
7/30/2019 49012527 Tunning Tip and Tricks Basic
15/44
What are indexes?
An index is a data structure that takes the
value of one or more columns of a table
(the key) and returns all rows (or the
requested columns in that row) with thatvalue of the column quickly. The efficiency
of the index is that it lets you find the
necessary rows without having to perform a
full table scan, this leads to few I/O's.
7/30/2019 49012527 Tunning Tip and Tricks Basic
16/44
Various Types Of Indexes
B-Tree indexes
Bitmap indexes
Cluster indexes, bitmap join indexes,
function-based indexes, reverse key
indexes and text indexes are all just
variations on the two main types
What B-Stands in B-Tree?
7/30/2019 49012527 Tunning Tip and Tricks Basic
17/44
How can they help me?
To quickly find specific rows byavoiding a Full Table Scan
To avoid a table access altogether
To avoid a sort
7/30/2019 49012527 Tunning Tip and Tricks Basic
18/44
What to Index and What Not?
Should I Index each and everycolumn.
How to find which columns need to be
indexed. What kind of index one need to
create?
7/30/2019 49012527 Tunning Tip and Tricks Basic
19/44
Are they really good?
Well the answer of this is bit trick.But still I will say sometimes indexes in
oracle can get more scary then the
monster we had seen in our dreams
Golden Rule #4:Use indexes but not with blindeye
7/30/2019 49012527 Tunning Tip and Tricks Basic
20/44
Time To Watch your SQL NowRule #1: Watch Indexed WHERE Conditions
Assume index on address (city, state)
non-leading index column references cannot use indexes
where state = 'TX' [Index Not used]
where city = 'DALLAS' [Index Used]where state = 'TX' and city = 'DALLAS' [Index Used]
NOT, != and disable index use
where state not in ('TX', 'FL','OH') [Index Not used]
where state != 'TX' [Index Not used]
NULL value references can never use indexes
where state IS NULL [Index Not used]
where state IS NOT NULL [Index Not used]
expression references can never use indexes
where substr(city,1,3) = 'DAL' [Index Not used]where city like 'DAL%' [Index Used]where city || state = 'DALLASTX' [Index Not used]
where city = 'DALLAS' andstate = 'TX [Index Used]where salary * 12 >= 24000 [Index Not used]
where salary >= 2000 [Index Used]
7/30/2019 49012527 Tunning Tip and Tricks Basic
21/44
Rule #2:Watch Non-Indexed WHERE Conditions
Oracle evaluates Non-Indexed conditions linked by AND bottom up
Bad: select * from address whereareacode = 972 andtype_nr = (select seq_nr from code_table where type = HOME)
Good: select * from address where
type_nr = (select seq_nr from code_table where type = HOME) andareacode = 972
Oracle evaluates Non-Indexed conditions linked by OR top down
Bad: select * from address wheretype_nr = (select seq_nr from code_table where type = HOME) orareacode = 972
Good: select * from address whereareacode = 972 ortype_nr = (select seq_nr from code_table where type = HOME)
7/30/2019 49012527 Tunning Tip and Tricks Basic
22/44
7/30/2019 49012527 Tunning Tip and Tricks Basic
23/44
Rule #4:Consider IN or UNION in place of OR
if columns are not indexed, stick with OR
if columns are indexed, use IN or UNION in place of OR
IN example
Bad: select * from address wherestate = 'TX or
state = 'FL orstate = 'OHGood: select * from address where
state in ('TX','FL','OH')
UNION example
Bad: select * from address wherestate = TX or
areacode = 972Good: select * from address where
state = TX
unionselect * from address where
areacode = 972
7/30/2019 49012527 Tunning Tip and Tricks Basic
24/44
Rule #5:Weigh JOIN versus EXISTS Sub-Query
use table JOIN instead of EXISTS sub-query
when the percentage of rows returned from the outer sub-query is high
select e.name, e.phone, e.mailstop
from employee e, department dwhere e.deptno = d.deptno
and d.status = ACTIVE
use EXISTS sub-query instead of table JOIN
when the percentage of rows returned from the outer sub-query is low
select e.name, e.phone, e.mailstopfrom employee e
where e.deptno in (select d.deptno
from department d
where d.status != ACTIVE)
7/30/2019 49012527 Tunning Tip and Tricks Basic
25/44
Rule #6:Consider EXISTS in place of DISTINCT
avoid joins that use DISTINCT, use EXISTS sub-query instead
Bad: select distinct deptno, deptname from emp, dept whereemp.deptno = dept.deptno
Good: select deptno, deptname from dept whereexists(select X from emp where
emp.deptno = dept.deptno)
Note only has to find one match
7/30/2019 49012527 Tunning Tip and Tricks Basic
26/44
26
Rule #7: Which one is better IN vs SubQuery
SELECTEMPLOYEE_ID, FIRST_NAME, E.LAST_NAME, E.SALARY
FROM
EMPLOYEES E
WHERE EXISTS
(SELECT 1
FROM ORDERS O
WHERE E.EMPLOYEE_ID = O.SALES_REP_IDAND O.CUSTOMER_ID = 144)
SELECT
E.EMPLOYEE_ID , E.FIRST_NAME , E.LAST_NAME , E.SALARYFROM EMPLOYEES E
WHERE E.EMPLOYEE_ID IN
(SELECT O.SALES_REP_ID
FROM ORDERS O
WHERE O.CUSTOMER_ID = 144)
7/30/2019 49012527 Tunning Tip and Tricks Basic
27/44
Rule #8:Consider NOT EXISTS in place of NOT IN
avoid sub-queries that use NOT IN, use NOT EXISTS instead
Bad: select * from emp where
deptno not in (select deptno from dept wheredeptstatus = A)
Good: select * from emp wherenot exists(select X from dept where
deptstatus = A anddept.deptno = emp.deptno)
Note only has to find one non-match
7/30/2019 49012527 Tunning Tip and Tricks Basic
28/44
Rule #9:COUNT Using Indexed Column or Asterisk
when counting rows, use COUNT on indexed column or asterisk
select count(indexed_column) from table [Most Efficient]
select count(*) from table
Select count(non_indexed_column) from table
select count(1) from table
Note rule based optimizer only
7/30/2019 49012527 Tunning Tip and Tricks Basic
29/44
Rule #10:Ordering Via the WHERE Clause
a dummy WHERE clause referencing an indexed column will
retrieve all records in ascending order (descending for 8i descending index)
not perform a costly sort operation
Bad: select * from addressorder by city
Good: select * from address wherecity >
7/30/2019 49012527 Tunning Tip and Tricks Basic
30/44
Rule #11: Avoid including a HAVING clause in SELECT statements
SELECT region, AVG (loc_size)FROM location
GROUP BY regionHAVING region != 'SYDNEY'AND region != 'PERTH';
SELECT region, AVG (loc_size)
FROM location
WHERE region != 'SYDNEY'
AND region != 'PERTH';
GROUP BY region;
7/30/2019 49012527 Tunning Tip and Tricks Basic
31/44
7/30/2019 49012527 Tunning Tip and Tricks Basic
32/44
SELECT * FROM t1, t2, t3
WHERE t1.emp_id = t2.emp_id
AND t2.emp_id = t3.emp_id
SELECT * FROM t1, t2, t3
WHERE t1.emp_id = t2.emp_id
AND t2.emp_id = t3.emp_id
Make sure everything that can be joined is joined (for 3 or more tables)
add:
Instead of:
Rule #13: Add all possible Joins
7/30/2019 49012527 Tunning Tip and Tricks Basic
33/44
33
Compiler directive- impacts optimizers behavior,
not query result
Hints force the optimizer approach and goal.
Hints may not always be Used
Syntax must be accurate - if not, hint is ignored
If aliasing tables, must use alias in hint
Do not specify schema names in the hint even if they are specified inthe FROM clause
Rule #14: AddHints /*+ TUNE*/
7/30/2019 49012527 Tunning Tip and Tricks Basic
34/44
34
Mostly Used Ones
Hints for Optimization Approaches and Goals /*+ ALL_ROWS */ /*+ FIRST_ROWS */
/*+ CHOOSE */ /*+ RULE */
Hints for Access Paths
/*+ FULL(table) */ /*+ INDEX(table index) */
Hints for Join Orders ORDERED
Hints for Join Operations DRIVING_SITE
CACHE
7/30/2019 49012527 Tunning Tip and Tricks Basic
35/44
Rule #15:Use PL/SQL to reduce network traffic
Utilize PL/SQL to group related SQL commands and thereby reduce network traffic
Bad:select city_name, state_code
into :v_city, :v_sate
from zip_codes where zip_code = 75022;
insert into customer (Bert Scalzo,75022, :v_city, v_state);
Good:begin
select city_name, state_code
into :v_city, :v_sate
from zip_codes where zip_code = 75022;
insert into customer (Bert Scalzo,75022, :v_city, v_state);
end;/
Golden Rule #5: Know your data, Know whatyoure doing,Test and find the best way
7/30/2019 49012527 Tunning Tip and Tricks Basic
36/44
PL\SQL Codes Must Watch
7/30/2019 49012527 Tunning Tip and Tricks Basic
37/44
Rule #1:Use proper size of Variables defined.
V_NAME VARCHAR2(2000);
V_AGE NUMBER(1000);
V_ADD NVARCHAR2(4000);
V_NAME EMPLOYEE.NAME%TYPE;
V_AGE NUMBER(3);
V_ADD EMPLOYEE.ADDRESS%TYPE;
7/30/2019 49012527 Tunning Tip and Tricks Basic
38/44
Rule #2 : Passing Large Data Structures with NOCOPY
The PL/SQL runtime engine has two different methods for
passing parameter values between stored procedures and
functions, by valueand by reference.
procedure
get_customer_orders
( p_customer_id in number,
p_orders out nocopy orders_coll );
theorders orders_coll;
get_customer_orders(124, theorders);
7/30/2019 49012527 Tunning Tip and Tricks Basic
39/44
If the amount of data to be processed or utilized from your PL/SQLprocedure is too large to fit comfortably in a PL/SQL table, use a
GLOBAL TEMPORARY table rather than a normal table. A GLOBAL
TEMPORARY table has a persistent definition but data is not
persistent and the global temporary table generates no redo or
rollback information. For example if you are processing a large
number of rows, the results of which are not needed when thecurrent session has ended, you should create the table as a
temporary table instead:
create global temporary table results_temp (...)
on commit preserve rows; The on commit preserve rows clause
tells the SQL engine that when a transaction is committed the table
should not be cleared.
The global temporary table will be created in the users temporary
tablespace when the procedure populates it with data and the
DIRECT_IO_COUNT will be used to govern the IO throughput (this
usually defaults to 64 blocks).
Rule #3 : Use Temporary Tables
7/30/2019 49012527 Tunning Tip and Tricks Basic
40/44
The bulk operation takes less than half the time to populate thecollection from the query.
BULK COLLECT INTO record;
BULK COLLECT INTO record LIMIT V_NUM;
Rule #3 : Reduce Network Traffic With use of bulk collect
7/30/2019 49012527 Tunning Tip and Tricks Basic
41/44
Rule #4 : Use FORALL for DML Operations
EXECUTE IMMEDIATE TRUNCATE TABLE forall_test;
FOR i IN l_tab.first .. l_tab.last LOOP
INSERT INTO forall_test (id, code, description)
VALUES (l_tab(i).id, l_tab(i).code, l_tab(i).description);
END LOOP;
EXECUTE IMMEDIATE TRUNCATE TABLE forall_test;
FORALL i IN l_tab.first .. l_tab.last
INSERT INTO forall_test VALUES l_tab(i);
7/30/2019 49012527 Tunning Tip and Tricks Basic
42/44
FOR i IN l_id_tab.first .. l_id_tab.last LOOPUPDATE forall_test
SET id = l_id_tab(i),
code = l_code_tab(i),
description = l_desc_tab(i)
WHERE id = l_id_tab(i);
END LOOP;
FORALL i IN l_id_tab.first .. l_id_tab.last
UPDATE forall_test
SET id = l_id_tab(i),
code = l_code_tab(i),description = l_desc_tab(i)
WHERE id = l_id_tab(i)
RETURNING id, description BULK COLLECT INTO l_out_tab;
Rule #5 : Use RETURNING Clause in FORALL Operations
R l #6 T i D i SQL ith EXECUTE IMMEDIATE d
7/30/2019 49012527 Tunning Tip and Tricks Basic
43/44
Rule #6 : Tuning Dynamic SQL with EXECUTE IMMEDIATE andCursor Variables
DECLARETYPE EmpCurTyp IS REF CURSOR; emp_cv EmpCurTyp;
v_ename VARCHAR2(15);
v_sal NUMBER := 1000;
table_name VARCHAR2(30) := 'employees';
BEGIN
OPEN emp_cv FOR 'SELECT last_name, salary FROM ' ||
table_name || ' WHERE salary > :s' USING v_sal;
CLOSE emp_cv;
END;
/
7/30/2019 49012527 Tunning Tip and Tricks Basic
44/44
Any Questions ?
Thanks for your time
Recommended