This presentation features the fundamentals of SQL tunning like SQL Processing, Optimizer and Execution Plan, Accessing Tables, Performance Improvement Consideration Partition Technique. Presented by Alphalogic Inc : https://www.alphalogicinc.com/
3. Introduction
SQL Tuning is the iterative process of improving SQL statement performance to
meet specific, measurable, and achievable goals. SQL tuning implies fixing
problems in deployed applications. In contrast, application design sets the
security and performance goals before deploying an application.
Prerequisites for SQL Tuning:
● Familiarity with database architecture
● Knowledge of SQL and PL/SQL
● Familiarity with database-provided SQL tuning tools
4. Introduction
Purpose of SQL Tuning:
1) Reduce user response time, which means decreasing the time between
when a user issues a statement and receives a response
2) Improve throughput, which means using the least amount of resources
necessary to process all rows accessed by a statement
5. SQL Processing
Syntax Check: It make sure SQL statement is right?
Semantic Check: It make sure that it is valid
statement and the statement is in the database.
Shared Pool Check: If The statement is previously
executed in SQL, SQL tries to find the similar match
and if found it will directly execute.
Optimization: If SQL does not find the similar
match it will Generate multiple execution plans.
Row Source Generation: It will decide which
execution plan is to execute.
Execution: It will execute the query and generate
the result.
6. SQL Processing
Soft Parse:
During the parse the database performs a stored pool check to see if
existing parse statement already exist and then determines whether it
can skip resource intensive steps of statement processing .
Hard Parse:
If oracle statement can not reuse existing code then it must be build a
new executable version of application code this is known as hard parse.
7. Optimizer and Execution Plan
Cost Based Optimization / Rule Based Optimization:
It is designed to determine most efficient way to carry out SQL Statement , up-to
date statical information on data being accessed.
Main Optimizer factors are: columns, data_type ,Row Count , Row Length,
Percentage of Distinct values per column, Index , Constraints Available on Table
What is Execution Plan?
It is a list of steps that Oracle will follow in order to execute SQL Statements.
each step is of one of finite number of basic operation known database server.
8. Optimizer and Execution Plan
Explain Plan: It enables you to view Execution Plan.
Autotrace : It generates the performance of the Query , this command provides
the statistics such as disk and memory space.
V$SQL Plan : Information about Executed SQL Statement and their Execution
plan that are still in shared poo.
9. Accessing Tables
Types of Table Access:
● Table Access FULL: Full table scan sequentially read each row from the
table , It reads in blocks.
● Table Access By Row ID: Oracle reads the row by their ROWID , It contain
physical location of row
● Index Unique Scan: It searches the Index for specified key
● Index Range Scan: It is used when there is NON-Unique Index or you are
searching for non rage of values.
Example:-
SELECT * FROM SALES WHERE SALES_ID BETWEEN 0 AND 1;
10. Simplest Tunning Rules
SELECT: Select the data only you need do not select all tha data.
Example:
Select SALES_DATE,SALES_AMOUNT,SALES_ID from Sales;
Use Table Aliases: Uset the Alias for retrieving the data.
Example:
Select s.sales_id, s.sales_amount, s.product_name, p.product_name,
p.product_code
from SALES s, Product p
where s.product_id, p.product_id
11. Simplest Tunning Rules
Use Where rather than Having:
– Having clause filter the the row only after its has been fetched, grouped and
sorted.
– Where clause reduces the overload of the database
Example:
Don't:
SELECT SalesOrderID, SUM(UnitPrice * OrderQty) AS TotalPrice FROM
Sales.SalesOrderDetail GROUP BY SalesOrderID HAVING SalesOrderID >
50000;
Do:
SELECT SalesOrderID, SUM(UnitPrice * OrderQty) AS TotalPrice FROM
Sales.SalesOrderDetail WHERE SalesOrderID > 50000 GROUP BY
SalesOrderID;
12. Index Suppression
SUBSTR Function:
Using SUBSTR function will disable index.
Example:
Don't:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE SUBSTR(PRODUCT_NAME,1,3)='SAM'
Do:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE PRODUCT_NAME LIKE 'SAM%'
13. Index Suppression
Following are the possible reasons why Index get suppressed.
Index can tell you what is "ON" table but not what is unavailable in table <> It will
disable index usages.
Use of <> operator in Indexed column
Example:
Don't:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID<>12345;
Do:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID>12345 AND
ORDER_ID<12345;
14. Index Suppression
Use of Arithmetic Operator (+,*,/,-,)
If you use following Arithmetic Operator it will suppress the Index
Example:
Don't:
SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID+1000<5000;
Do:
SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID<4000;
15. Index Suppression
Use of | | Operator (Concatenation Operator)
Concatenation Operator
Don't:
SELECT * FROM PRODUCT WHERE COLOR | |
PRODUCT_CATEGORY='BlackMobile'
Do:
SELECT * FROM PRODUCT WHERE COLOR ='BLACK' AND
PRODUCT_CATOGERY='MOBILE'
16. Index Suppression
Comparing Character to Number Value
Following example will show the comparison that will suppress the index
Example
Don't:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE PRODUCT_NAME=123;
In this example database will get confused and tries to avoid the Index
Do:
SELECT SALES_DATE, ORDER_ID, TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE PRODUCT_NAME='123'
In this query single quotation will be used so Index will be used.
17. Index Suppression
Use of IS NULL / IS NOT NULL Operator
Relational Database Ignores the NULL Values because it assumes NULL means
nothing.
Example:
SELECT SALES_DATE, ORDER_ID,TOTAL_AMOUNT_PRODUCT_NAME
FROM SALES_HISTORY WHERE ORDER_ID IS NULL;
Better Option is use following values
Update the NULL DATA With Default Values
● If it is character column , update with "x"
● If it is numeric column , update with "0"
18. Index Suppression
Use of function based Index:
Whenever possible try to avoid function on Indexed column, but if there is
business Requirement you can not avoid , then Instead of using function on
column try to use function based Index.
For example use following SQL query
CREATE INDEX PRODUCT_IDX ON SALES_HISTORY(SUBSTR
(PRODUCT_NAME,1,3));
19. Performance Improvement Consideration
Use UNION ALL Instead of UNION:
Union Performs Sort , Unique Operation its overload and time consuming
process.
Example of Union ALL
SELECT SALES_ID FROM SALES
UNION ALL
SELECT PRODUCT_AMOUNT FROM PRODUCT;
20. Performance Improvement Consideration
Minimize the Table Look up in the Query:
Using Sub-queries into the query it is time consuming process,
Instead you can use this
SELECT PRODUCT_ID, PRODUCT_NAME
WHERE
PRODUCT_NAME IN(SELECT PRODUCT_NAME FROM SALES);
21. Performance Improvement Consideration
Use of DISTINCT Keyword Instead of EXISTS:
- It will easily search the Value from the table and displays the results
SELECT DISTINCT(SALES_CATEGORY) FROM SALES;
Avoid Searching same table Multiple Times
- It slow down the database and the execution time
USE TRUNCATE Instead of DELETE
- Truncate is Faster than DELETE it does not store UNDO Information.
Example:
TRUNCATE TABLE SALES;
22. Consideration While Using SQL Statement
● Reduce the No. of database access Oracle needs to perform all internal
steps to display the result
● Use of Commit Command After any Operation.
For Example
INSERT INTO SALES (SALES_ID, SALES_AMOUNT,SALES_PERSON,)
VALUES (100, 5000, JOHN);
COMMIT;
Information Held in Roll back SEGMENT to UNDO the Transaction is freed up.
Locks on Tables are Released.
23. Consideration While Using SQL Statement
Bulk Collect:
A bulk collect is a method of fetching data where the PL/SQL engine tells the
SQL engine to collect many rows at once and place them in a collection. The
SQL engine retrieves all the rows and loads them into the collection and
switches back to the PL/SQL engine.
24. Consideration While Using SQL Statement
For Example
DECLARE TYPE two_cols_rt IS RECORD
(employee_id employees.employee_id%TYPE, salary employees.salary
%TYPE);
TYPE employee_info_t IS TABLE OF two_cols_rt;
l_employees employee_info_t;
BEGIN SELECT employee_id, salary
BULK COLLECT INTO l_employees
FROM employees
WHERE department_id = 10; END;
25. Join Method
Nested Loop Join:
The nested loops join, also called nested iteration, uses one join input as the
outer input table and one as the inner (bottom) input table. The outer loop
consumes the outer input table row by row.
SELECT C.CustomerID, c.TerritoryID FROM Sales.SalesOrderHeader oh
JOIN
Sales.Customer c ON c.CustomerID = oh.CustomerID WHERE
c.CustomerID IN (10,12) GROUP BY C.CustomerID, c.TerritoryID
26. Join Method
Sort Merge Join:
The sort-merge join (also known as merge join) is a join algorithm and is used in
the implementation of a relational database management system. The basic
problem of a join algorithm is to find, for each distinct value of the join attribute,
the set of tuples in each relation which display that value.
Steps to Perform Sort Merge Join
1. Check if the rows match. If they do produce output row
2. If the value in input A is smaller than the value in input B, attempt to read
next row from input A and skip next step
3. Attempt to read next row from Input B If read attempt was successful,
loop
27. Join Method
Hash Join:
The HASH join is similar to a NESTED LOOPS join in the sense that there is a
nested loop that occurs—Oracle first builds a hash table to facilitate the
operation and then loops through the hash table. When using an ORDERED
hint, the first table in the FROM clause is the table used to build the hash table.
SELECT OC.FirstName, OC.LastName, OH.SalesOrderID FROM
Sales.SalesOrderHeader AS OH
JOIN
Person.Contact AS OC ON OH.ContactID = OC.ContactID WHERE
OC.FirstName LIKE 'John%'
28. Hints The Data
Hint is an addition to the SQL standard that instructs the database engine on
how to execute the query. For example, a hint may tell the engine to use or not
to use an index (even if the query optimizer would decide otherwise).
OR
Hint is an Instruction passed to the optimizer through in SQL. hints enable you to
make decision normally made automatically by the optimizer. It is very useful for
testing the performance of specific accesses path.
Example
SELECT /*+ INDEX (s,sales_prd_idx)/ ORDER_ID,SALES_DATE,
TOTAL_AMOUNT, PRODUCT_NAME, FROM SALES_HISTORY s WHERE
CUSTOMER_ID=1000;
29. House Keeping
Invalid Optimizer Statistics:
We know how important statistics are for optimizer to come up with good
Execution plan. Some optimizer statistics might stale or missing.
Solution:
Check Analyzed Date ,
Ensure Table Indexes , Columns and partitions have statistics
SELECT * FROM DBA_TAB_STATISTICS;
SELECT * FROM DBA_IND_STATISTICS;
SELECT * FROM DBA_COL_STATISTICS;
30. House Keeping
How Do I Know DBA Performing BAD:
Oracle storage arranged into blocks of a given size. Table are indexes are made
up of series of blocks on the disk when these blocks are in the memory they
occupy buffer.
When Oracle requires a block first it check to see if it already has blocks it needs
in memory if so , the in- memory version is used. If it does not have block in
memory then it will read it from disk into memory.
So buffer get represents no.of time the oracle had to access the block.
31. Design Constraints
Effective Schema Design
● It Plays Very Important Role in SQL Tunning.
● Choose the data type for all Primary and Unique key carefully and remain
consistent through schema.
● Create Indexed whenever necessary if possible try to create index on
Multiple column.
● Normalize the Tables.
● Put Necessary constraints on Tables
32. Design Constraints
Separate Tables for Data and Index:
● It is Important to create separate table space for Tables, Indexes, Temporary
segment and rollback segment.
● Separating tables for Indexes is key to Balancing I/O and reducing
contention.
Index Organized Tables
● Index Organized Tables have their Primary Key Data and and non key
column data stored within same binary Tree. Effectively data is stored in
Primary Key Index.
● Row are Not Much Longer than their Index Key.
● Small Look Up Tables
33. Design Constraints
Table Partitions
Table partitioning is a way to divide a large table into smaller, more manageable
parts without having to create separate tables for each part. Data in a partitioned
table is physically stored in groups of rows called partitions and each partition
can be accessed and maintained separately. It Improves the Query
Performance.
Following are the Table partitions Methods
● Range Partitioning
● List Partitioning
● Hash Partitioning
● Composite Partitioning
35. Range Partitioning:
Range partitioning is a partitioning technique where ranges of data is stored
separately in different sub-tables. MAXVALUE is provided as a catch-all for
values that exceed all ranges specified. Note that Oracle sorts NULLs greater
than all other values, except MAXVALUE.
Range partitioning divides the information into a number of partitions depending
on ranges of values of the particular partitioning keys for every partition of data.
It is a popular partitioning scheme which is normally used with dates. For
example, representing the days of the May month, it will have a table with the
column name as May and rows with dates from 1st of May to 31st of May.
36. List Partitioning
List partitioning allows to openly organize the rows, which are divided into
partitions by spelling out a roll of distinct standards for the partitioning key in an
account for every division. Using this scheme of partitioning, even dissimilar and
shuffled information tables can be managed in a comfortable approach.
In order to avoid the errors during the partition of rows in the giant database, the
addition of the probable terms into the table formed by the list partitioning
method can be avoided by using the default partition process.
37. Hash Partitioning
Oracle has got a hash algorithm for recognizing the partition tables. This
algorithm uniformly divides the rows into various partitions in order to make all
the partitions of identical dimensions. The process carried on by using this hash
algorithm to divide the database tables into smaller divisions is termed as the
hash partitioning.
Hash partitioning is the perfect means for sharing out data consistently among
different devices. This method of partitioning is an user-friendly partitioning
system, particularly when the information to be detached has no apparent
partitioning key.
38. Composite Partitioning
The Composite Partitioning method includes a minimum of two partitioning
procedures on the data. Initially, the database table will be divided by using one
partition procedure and then the output partition slices are again partitioned
further by using another partitioning procedure.
39. Bitmap Indexes
A Bitmap Index is a special kind of database index that uses bitmaps. Bitmap
indexes have traditionally been considered to work well for low-cardinality
columns, which have a modest number of distinct values, either absolutely, or
relative to the number of records that contain the data.