SQL Performance Tuning
- Narendar V
Submitted to
4iapps.com
Agenda
SQL TUNING..………….
………………………………………………………………………………………………………... 3
WHEN TO TUNE?
………………………………………………………………………………………………………………
.4
HOW IS AN SQL STATEMENT PROCESSED?....…………..................………………….
…………………..……5
OPTIMIZER…….….....................................................................................................
……………………….......... 6
EXPLAIN AND EXCEUTION PLAN..............................................
………………..........................…...………..8
TECHNIQUES……………………………................................................
………………………………………….………9
INDEX / HOW TO PERFORM INDEX.....................................................................
4iapps.com 2
…………………….……11
SQL Tuning
o SQL Tuning is the process of enhancing SQL Queries to speed up their
performance.
o Is the combination of some techniques to improve the performance of query.
Purpose of SQL Tuning
A SQL Statement becomes a problem when it fails perform according to
standard.
Main GOAL’s
» Reduce user response time, which means decreasing the time for the
user to receive the result.
» Prevent the excessive consumption of resources, to process all rows
accessed by statement.
4iapps.com 3
When to Tune?
• While creating a new SQL Code.
• When the SQL Code take so much time to retrieve data from database.
4iapps.com 4
How is an SQL Statement Processed ?
4iapps.com 5
Optimizer
4iapps.com 6
Explain and Execution Plan.
• Explain Plan Statement displays the execution plans chosen by the
optimizer for SELECT, UPDATE, INSERT, DELETE Statement.
Example : EXPLAIN FOR
SELECT * FROM DUAL;
select * from
To view the Explained plan:
table(dbms_xplan.display);
• Execution Plan shows the detailed steps necessary to execute a SQL
Statement.
4iapps.com 7
Cardinality and Cost.
• The Cardinality is number of rows returned by each operation in a execution
plan.
For Example : If the optimizer estimate for the number of rows
returned by the step is 100 then the cardinality estimate for this operation
is 100.
• Cost is the estimates amount of work the plan will do. It calculates the
usage of disk I/O , CPU,
Memory usage.
4iapps.com 8
Techniques used to optimize SQL queries.
1. SELECT Fields instead of using SELECT *.
2. Avoid SELECT DISTINCT.
3. To Prevent CARTESIAN Join, Use Inner Join.
Example: If we had a 1000 customers and 1000 in total sales,
the query would first produce 10,00,000 results before filtering
for the 1000 entries.
4. Use wildcards at end of the phrase.
5. Avoid using unwanted tables and joins.
• If you are not showing any data column from the table, then
avoid using those tables. (Note: In some cases, join between
two table is not directly possible then you need third table to
join indirectly, you can use it).
4iapps.com 9
Index
• Index provide quick access to rows in a table.
• It increase the speed of queries.
Index Type
• B-Tree Index
I. Normal Index
II. Composite Index
III. Function-Based Index
Syntax:
create index <index_name> on <table_name> (column_name);
4iapps.com 10
How to Perform Indexing
• We can perform indexing by using an indexed column in where clause.
• Using Optimizer hints.
• Forcing the index in where clause by following techniques.
• For Number datatype Column, Use “+ 0 “
• For varchar2 datatype Column, Use “||‘’ ‘’
4iapps.com 11
Create a Composite Index.
• If the query of cost is high on a specific table and it has two or three
columns used in joins. We need to create a one composite index for those
three columns.
Syntax:
create index <index_name> on <table_name> (column1, column2,
column3, column4) nologging;
Optimizer Hints
Optimizer Hints
Optimizer Hints Optimizer Hints Types
EIPL
4iapps.com 12
View Usage
• Do not use the views out of their purpose, which means a complex view.
• Don’t join the complex view with table or another views.
• Avoid using views inside of views.
• You can create a new view and use of it.
Materialized Views
• In Oracle 12.1, you can create a materialized view as below.
Create or Replace Materialized View view_name_mv As select column
from table;
• But In 12.2, you want to create as in below document.
Create
Materialized View R12.2
4iapps.com 13
REFERENCES
https://docs.oracle.com/database/121/TGSQL/tgsql_intro.htm#TGSQL118
https://www.geeksforgeeks.org/sql-performance-tuning/
https://docs.oracle.com/en/database/oracle/oracle-database/21/tgsql/sql-tunin
g-guide.pdf
SQL Tuning Guide
4iapps.com 14
THANK YOU
4iapps.com 15
Contact us
INDIA UAE QATAR OMAN LONDON USA CANADA MALAYSIA SINGAPORE
AUSTRALIA
1-1-1 Tecci Park. 102, RKM Building, Al Matar 2275 Research Blvd Ontario Office Level 35-02 (East 10 Jalan Besar Level 9, 636 St Kilda
Commercial Center Knowledge Oasis Office-46, Flat-61,
285, Rajiv Gandhi Hor Al Anz East, Abu Ste 500 Rockville 1145 Midland Avenue, Wing), Q Sentral, # 10 - 09 Sim Lim Tower Road, Melbourne,
Muscat Praed Street,
Hail, Al Matar Street MD 20850 Unit 9 2A, Jalan Stesen Sentral Victoria 3004
Salai PO BOX:236830, Sultanate of Singapore 208787
Doha, Qatar London 2, Tel +649 845 956
Sholinganallur, Dubai Oman Tel +1-240-243-1393 Scarborough ON Tel +65 971 556 72
Tel +974 4432 6634 W21NS KL Sentral, Kuala
Chennai - 600 119 Tel +971 55 Tel +968 M1K 4H2
Lumpur 50470
2632420 95072507 Tel +1 714 983 5640
Tel +91 44 6638 0000 Tel +60 17 7820682
Sharjah
Po Box No: 42693
Hamriyah Free
Zone, Sharjah
Tel +971 52 998
9126
4iapps.com
4iapps.com 16