Sie sind auf Seite 1von 22

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

What We Cover Here


Types Of Optimizer. Types of index and there use. Optimizer Join Method. Execution Hierarchy Of SQL Query. Statics and Query Plan. Explain Plan Hints

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

Transform plan into executable

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.

OPTIMIZER_MODE Initialization Parameter


CHOOSE:-If data dictionary content statics about any of the table , or content any statics about the query then it will use CBO otherwise RBO. ALL_ROWS FIRST_ROWS_n FIRST_ROWS RULE Exe:- ALTER SESSION SET OPTIMIZER_MODE = FIRST_ROWS_1;

What Does Optimizer Do?


Transformation:- The main objective of the query transformer is to determine if it is advantageous to change the form of the query so that it enables generation of a better query plan. View Merging Predicate Pushing Subquery Unnesting Query Rewrite with Materialized Views Estimation:- estimator is to estimate the overall cost of a given plan. If statistics are available, then the estimator uses them to compute the measures. Selectivity Cardinality Cost Plan Generating:- The main function of the plan generator is to try out different possible plans for a given query and pick the one that has the lowest cost.

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.

Optimizer Join Method


Nested Loop Join:-A nested loop join always take place between a small row set and large row set, usually small row set is accessed by sorted index scan and large table is scan by rowid pointer Hash Join:-Hash join is used for two large set of rows. A temporary hash table is created for both table to keep matching rows. Short Merge Join:-It execute in three steps first two steps sorting both tables and third steps merge in sorted order . Semi Join:-It uses In and Exist method to join two table.

Understanding Access Paths for the CBO


Full Table Scans :- Full table scans are cheaper than index range scans when accessing a large fraction of the blocks in a table. This is because full table scans can use larger I/O calls, and making fewer large I/O calls is cheaper than making many smaller calls. Rowid Scans :-The rowid of a row specifies the datafile and data block containing the row and the location of the row in that block. Locating a row by specifying its rowid is the fastest way to retrieve a single row, because the exact location of the row in the database is specified. Index Scans:-A row is retrieved by traversing the index, using the indexed column values specified by the statement Index Unique Scans:-. Oracle performs a unique scan if a statement contains a UNIQUE or a PRIMARY KEY constraint that guarantees that only a single row is accessed. Index Range Scans:-An index range scan is a common operation for accessing selective data.

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.

Statics and Query Plan


Statics and Query plan is two most significant factors which allow effective tuning of Oracle SQL. Statics:-statics have to be present and they have to be up to date because Optimizer decision are based on statics. It is a computation or estimation of the exact size and placement of data in tables and indexes. Statics can be generated using the ANALYZE command or DBMS_STATS Syntax:-Analyze {table|index|cluster} OBJECT_NAME Compute statics Dynamic Samplings:- If the statics are too old then we can also use DYNAMIC_SAMPLE hints to find the statics.

Classifying Hints
Influence the Optimizer Alter Table Scans Alter Index Scans Alter join Cause Parallel SQL Execution Alter Queries And Subqueries

Influence The Optimizer


All_Rows:-Suppress indexes and favour full table scan First_rows(n):-Favoring Few Rows Choose:-Choose CBO when statics are present else use RBO Dynamic_sampaling:-If statics are not present or out of date Ex:- Select /*+First_rows(10)*/ ename,sal,empno from Emp;

Altering Index Scans


INDEX[_ASC|_DESC]:-Forces use of an index with lowest cost index. The selected index is scanned as it sorted order. INDEX_FFS:-Fast full index scan, reading the index in fast physical order. NO_INDEX:-Ignored the name indexes. USE_NL_WITH_INDEX:-Force a query to use a specific table as inner loop of a nested loops join , with the option of using a particular index.

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

Causes Parallel Execution


[NO]_PARALLEL:-Parallel execution , preferably on multiple CPUs or servers, or a computer capable of parallel processing PQ_DISTRIBUTE:-Improve Parallel Join. [NO]_PARALLEL_INDEX:-Processes index scans for partition in parallel

Altering Queries And Subqueries


[NO]CACHE:-used to force data into a most recently used list , NOCACHE force to used list recently used list ORDERED_PREDICATES:-Force subqueries into calling query. PUSH_SUBQ:-Resolve the subqueries first.

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.

Syntax For Explain Plan


Create the plan table in your schema EXPLAIN PLAN SET STATEMENT_ID= FOR SELECT * FROM EMP Display the explain plan Select * from table(DBMS_XPLAN.DISPLAY);

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.

Das könnte Ihnen auch gefallen