SQL Tuning: Vinay Singh Tata Consultancy Services
SQL Tuning: Vinay Singh Tata Consultancy Services
SQL Tuning: Vinay Singh Tata Consultancy Services
Performance Tuning
Performance Tuning:-Its not a code writing skills, its the smartness in your code writing. It reduce response time for SQL processing. To find a more efficient way to process workload Improve search time by using indexes Join data efficiently between 2 or more tables
Type Of Optimizer
RBO:-The optimizer chooses an execution plan based on the access paths available and the ranks of these access path. Access path is a set of rules that is used by RBO , this is the default mode of optimizer prior to 10g. CBO:-The CBO determines which execution plan is most efficient by considering available access paths and by factoring in information based on statistics for the schema objects (tables or indexes) accessed by the SQL statement.
Query Processing
Check syntax + semantics Generate plan description
Goal Of CBO
By default the goal of CBO is best throughput i.e. it chooses the least amount of resources necessary to process all rows accessed by the statement. We can also change the optimizer mode to best response time i.e. it uses the least amount of resources necessary to process the first row accessed by a SQL statement. The execution plan produced by the optimizer can vary depending on the optimizer's goal. Optimizing for best throughput is more likely to result in a full table scan rather than an index scan, or a sort merge join rather than a nested loop join. Optimizing for best response time usually results in an index scan or a nested loop join.
Types Of Index
Btree Index:-By default oracle create a b-tree index, in btree you walk through branches until you get the node that has the data you want to use. Bitmap Index:-a two dimensional array is created with one column for every row in table being indexed. Function Index:- A function-based index includes columns that are either transformed by a function , such as the UPPER function, or included in an expression. Ex-CREATE INDEX uppercase_idx ON employees (UPPER(last_name)); Reverse Key Index:-Oracle store the index entries as there bytes reversed. EX -Create index rev_ind on emp(ename) Reverse.
Cont
Cluster Scans:-A cluster scan is used to retrieve, from a table stored in an indexed cluster, all rows that have the same cluster key value. In an indexed cluster, all rows with the same cluster key value are stored in the same data block. Hash Scans :-A hash scan is used to locate rows in a hash cluster, based on a hash value. Sample Table Scans:-A sample table scan retrieves a random sample of data from a table. This access path is used when a statement's FROM clause includes the SAMPLE clause or the SAMPLE BLOCK clause.
Classifying Hints
Influence the Optimizer Alter Table Scans Alter Index Scans Alter join Cause Parallel SQL Execution Alter Queries And Subqueries
Altering Join
ORDERED:-Make the Optimizer join tables in the sequence that those tables appear in the form clause of an SQL statement. LEADING:-Makes the optimizer use a named table as the first table in the join , regardless of which table occurs first in the FORM claus. [NO_]USE_NL, [NO_] USE_HASH, [NO_] USE_MERGE:-Force nested loops , hash join , or sort merge join respectively
Explain Plan
The optimizer creates a query plan for every SQL statement . A query plan selected by the optimizer as a method of best performance for the execution of a SQL statement. The EXPLAIN PLAN command simply creates readable description of the steps in the query plan and insert those steps as entries in to a table called PLAN_TABLE. Following are the information which is stored in EXPLAIN PLAN The sequence in which table are accessed A method of accesses for each table Join method for any joins Filtering and sorting operations An estimated and guess time cost of the query plans a whole , and a cost for each.
Example
explain plan for SELECT empno, ename FROM emp WHERE ename LIKE '%SC% select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------Plan hash value: 3956160932 -------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 10 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 1 | 10 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------1 - filter("ENAME" LIKE '%SC%') 13 rows selected.