Query Optimization & How to interpret query execution plan

31
1 Query Optimization & How to interpret query execution plan Amol Barewar Email:[email protected]

description

Efficient methods of processing unanticipated queries are a crucial prerequisite for the success of generalized database management systems. A wide variety of approaches to improve the performance of query evaluation algorithms have been proposed: logic-based and semantic transformations, fast implementations of basic operations, and combinatorial or heuristic algorithms for generating alternative access plans and choosing among them. These methods are presented in the framework of a general query evaluation procedure using the relational calculus representation of queries

Transcript of Query Optimization & How to interpret query execution plan

Page 1: Query Optimization & How to interpret query  execution plan

1

Query Optimization &How to interpret query

execution plan

Amol BarewarEmail:[email protected]

Page 2: Query Optimization & How to interpret query  execution plan

2

Contents

Query Optimization

Types of Optimization

Optimization Problem and Solutions with practical examples

Understanding The Execution Plan

References

Page 3: Query Optimization & How to interpret query  execution plan

3

Rule BasedCost Based

Types Of Optimizations

Page 4: Query Optimization & How to interpret query  execution plan

4

Rule Based Optimization

• RBO uses predefined set of precedence rules(golden rules) to figure out the optimal path

• These rules used to choose one index over another index and when full table scan

• Oracle 9i has 20 “golden rules” for optimal execution path

ALTER SESSION SET OPTIMIZER_MODE = RULE/CHOOSE;

Page 5: Query Optimization & How to interpret query  execution plan

5

Rule Based Optimization

Page 6: Query Optimization & How to interpret query  execution plan

6

Rule Based Optimization

Page 7: Query Optimization & How to interpret query  execution plan

7

Emp_Id Ename EAddr Emp_Id Emp_dept Emp_sal

Driving Table…..?

EMPLOYEE DEPARTMENT

Join in SQL

Page 8: Query Optimization & How to interpret query  execution plan

8

Unique or primary key index

No of =‘s in index

Higher % of columns

One 2-col index vs. two 1-col

Same no of col in index?

Index1(A1,B,C); Table 1Index2(A2,F,G); Table 2

Select *From Table1,Table2Where A1=1 and B=2 and G=2 and F like ’%something’ and A1=A2

Two 1-Col Index: Table 1Index1(A)Index2(B);

One 2-Col Index: Table 2Index3(E,F);

Solutions

Page 9: Query Optimization & How to interpret query  execution plan

9

Rule Based Optimization

Rule-Based Optimizer Problems and Solutions

Page 10: Query Optimization & How to interpret query  execution plan

10

Rule Based Optimization

SELECT COUNT(*) FROM acct a, trans bWHERE b.cost_center = 'MASS'AND a.acct_name = 'MGA'AND a.acct_name = b.acct_name;

Response = 19.722 seconds

SELECT COUNT(*) FROM trans b, acct aWHERE b.cost_center= 'MASS'AND a.acct_name = 'MGA'AND a.acct_name = b.acct_name;

Response = 1.904 seconds

Problem 1: Incorrect Driving Table

Page 11: Query Optimization & How to interpret query  execution plan

11

Rule Based Optimization

SELECT COUNT(*)FROM transWHERE cost_center = 'MASS'AND bmark_id = 9;Response Time = 4.255 seconds

SELECT COUNT(*)FROM transWHERE bmark_id = 9AND cost_center = 'MASS';Response Time = 1.044 seconds

Problem 2: Incorrect Driving Index

Page 12: Query Optimization & How to interpret query  execution plan

12

Rule Based Optimization

SELECT fod_flag, account_no FROM account_masterWHERE (account_code like 'I%')ORDER BY account_no;

Index_1 UNIQUE (ACCOUNT_NO) Index_2 (ACCOUNT_CODE)

Index (ACCOUNT_CODE, ACCOUNT_NO)

Problem 3: Using the ORDER BY Index and not the WHERE Index

Page 13: Query Optimization & How to interpret query  execution plan

13

Cost Based Optimization

ANALYZE

DBMS_STATS

RULE BASED

ANALYZE & DBMS_STATS

RULE BASED

Collect statistics about tables, clusters and indexes, and store those statistics in the data dictionary

If table not analyzed CBO apply Rule Based Logic to select best path

Page 14: Query Optimization & How to interpret query  execution plan

14

ANALYZE

• ANALYZE COMPUTE & ANALYZE ESTIMATE

• Table and it’s index

• Index dropped..?

• DBMS_STATS.GATHER_SCHEMA_STATS &

GATHER_TABLE_STATS : CASCADE=>TRUE

Page 15: Query Optimization & How to interpret query  execution plan

15

ANALYZE Examples

1. ANALYZE TABLE EMP ESTIMATE STATISTICS SAMPLE 5 PERCENT FOR ALL INDEXED COLUMNS;

2. ANALYZE INDEX EMP_NDX1 ESTIMATE STATISTICS SAMPLE 5 PERCENT FOR ALL INDEXED COLUMNS;

3. ANALYZE TABLE EMP COMPUTE STATISTICS FOR ALL INDEXED COLUMNS;

4. ANALYZE TABLE EMP DELETE STATISTICS;

Page 16: Query Optimization & How to interpret query  execution plan

16

Cost Based Optimization Steps

• Check Syntax• Object Privilege

Parse

Page 17: Query Optimization & How to interpret query  execution plan

17

Cost Based Optimization Steps

Cost-Based Optimizer Problems and Solutions

Page 18: Query Optimization & How to interpret query  execution plan

18

Cost Based Optimization

SELECT acct_no, customer, product, trans_date, amtFROM transWHERE status='O';Response time = 16.308 seconds

> ANALYZE TABLE TRANS COMPUTE STATISTICS FOR ALL INDEXED COLUMNS

Response time reduces to 0.259 seconds

Problem 1: The Skewness Problem

Page 19: Query Optimization & How to interpret query  execution plan

19

Cost Based Optimization

The cost-based optimizer requires accurate information, including accurate data volumes, to have any chance of creating efficient execution plans otherwise performance will degrade a large scale

Example: Before analyze the cardinality is 1000(10000/10) on 10000 rowsAnd now we insert the 10000 new rows with distinct values so what will be the new cardinality?

Problem 2: Analyzing with Wrong Data

Page 20: Query Optimization & How to interpret query  execution plan

20

Cost Based Optimization

where business_unit = :5and ledger = :6and fiscal_year = :7and accounting_period = :8and affiliate = :9and statistics_code = :10and project_id = :11and account = :12and currency_cd = :13and deptid = :14and product = :15

Problem 4: Choosing an Inferior Index

Page 21: Query Optimization & How to interpret query  execution plan

21

Cost Based Optimization

where business_unit = :5and ledger = :6and fiscal_year = :7and accounting_period between 1 and 12and affiliate = :9and statistics_code = :10and project_id = :11and account = :12and currency_cd = :13and deptid = :14and product = :15

Problem 4: Choosing an Inferior Index

Page 22: Query Optimization & How to interpret query  execution plan

22

Cost Based Optimization

business_unitledgerfiscal_yearaffiliatestatistics_codeproject_idaccountcurrency_cddeptidProductaccounting_period

Problem 4: Choosing an Inferior Index

Page 23: Query Optimization & How to interpret query  execution plan

Understanding Execution Plan

Page 24: Query Optimization & How to interpret query  execution plan

GET SALES

GET PRODUCT

S

Apply Join

Apply Group

BY(Sort)

Return Result

SELECT prod_category, AVG(amount_sold)FROM o_sales s, o_products pWHERE p.prod_id = s.prod_idGROUP BY prod_category;

How data fetched for query…?

Page 25: Query Optimization & How to interpret query  execution plan

GROUP BY

JOIN

O_SALES

O_PRODUCT

Level 3

Level 2

Level 1

Interpreting Execution plan

Page 26: Query Optimization & How to interpret query  execution plan

Cardinality

Understanding the Execution Plans

Page 27: Query Optimization & How to interpret query  execution plan

1. Estimate no of rows coming out of the operation2. Complex formula used for this calculations3. Incorrect Cardinality

Total Rows: 18Different val: 3

Cardinality

Page 28: Query Optimization & How to interpret query  execution plan

Table

Full Table Scan

Row ID

Access Method

Page 29: Query Optimization & How to interpret query  execution plan

29

SQL Hints

Hints can be placed in SQL to force optimizer to follow our desired retrieval path rather then calculated by the optimizer

Select /* +RULE */ From emp , deptWhere…..

Select statement instructs the optimizer to use the rule based optimizer rather than the cost based optimizer.Delete /*+RULE*/ . . . . . . . . Update /*+RULE*/ . . . . . . . .

Page 30: Query Optimization & How to interpret query  execution plan

30

References

1. Data Warehousing using Oracle

Dr. P.S.Deshpande

2. The oracle optimizer explain the explain plan Oracle.com3. Oracle SQL Tuning Pocket Reference Mark Gurry

Page 31: Query Optimization & How to interpret query  execution plan

31

Thank You…….