using stored outlines & profiles chris lawson. tip 1: easy sql hints using stored outlines no...

35
Using Stored Outlines & Profiles Chris Lawson

Upload: charlotte-leadbeater

Post on 16-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Using Stored Outlines &

Profiles

Chris Lawson

Page 2: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

TIP 1: EASY SQL HINTS USING STORED OUTLINES

• No reason to avoid outlines.• Despite threats, are reportedly still in

Oracle 12.

So, let’s check ‘em out.

Page 3: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

A stored outline preserves an execution plan:

1. You turn-on outline capture.2. You run the sql. 3. Oracle captures sql and exec plan.4. Oracle stores sql hints.5. If sql is run again, Oracle use those sql

hints.

What is a Stored Outline?

Page 4: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Instead of preserving,

Let’s change plans.

But WaitThat’s Not All!

Page 5: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

• Create two outlines:▫ One the “regular” sql. ▫Another using a sql hint.

• We now have 2 stored outlines: Sql 1: No hint >> Outline 1 (bad plan) Sql 2: hint >> Outline 2 (good plan)

• Now, reverse hints! • Oracle will apply hints from Sql 2 when it

sees Sql 1.

For a Different Execution Plan

Page 6: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Create outline sql1 on {sql with hint}Create outline sql2 on {regular sql}

Update outln.ol$hints set ol_name =decode(ol_name, ‘sql1’, ‘sql2’, ‘sql2’, ‘sql1’)where ol_name in (‘sql1’, ‘sql2’);

Here’s the Switch

Page 7: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Create outline sqlz for category tune onMerge into tab1_gt gt using ( Select /*+index(a) */ … Create outline sqlz_fix for category tune onMerge into tab1_gt gt using ( Select …

Update outln.Ol$hints set ol_name =decode(ol_name, ‘sqlz_fix’, ‘sqlz’, ‘sqlz’, ‘sqlz_fix’)where ol_name in (‘sqlz’, ‘sqlz_fix’);

Outline Example

Page 8: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

• If a good exec plan is in pool, lock-in that plan

exec DBMS_OUTLN.CREATE_OUTLINE (hash_value =>'123', child_number=> 0)

Super Easy Way

Page 9: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

• Get the precise sql. • Recommend: Use sql in AWR/Sql

report

Some Traps!

Page 10: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Pitfall 1: AWR Spaces

What if the extra blanks occur as part of the functionality of the sql?

AWR report removes extra whitespace

Yea, yea, so what?

Page 11: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

• My code had a clause like this: orig: where col1 like '% abc %' altered: where col1 like '%abc%‘

• Missing the blanks means the outline won’t work!

• I had to alter outline script to add back extra blanks.

Don’t Change

My Code!

Page 12: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

• Hints stored in outline are not simple hints.

• Outlines typically have many more hints.

• More complicated syntax—”extended” format.

• This ensures that the plan is consistent.

Pitfall 2: Hint Count

Who cares about number of hints?

Page 13: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

When Outline Created, Oracle Saves Number Of Hints

• If outline imported, secret process counts hints

• If #hints <> Hintcount, outline dropped!• My sad tale

Page 14: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

• When you do the "hint switch“ you will likely end up with a different number of hints.

• You might have started with 10 hints, but after switch, you might have 12 hints.

• So good idea to verify Hintcount.

Let’s see an easy way to check the actual number of hints per outline.

Hint Count

Page 15: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Update Hintcount

Select ol_name, count(*) from Outln.ol$hintsGroup by ol_name; Select ol_name, Hintcount from Outln.ol$  

Page 16: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Enable Outlines after instance restart!

Alter System set Use_Stored_Outlines=TRUE

Page 17: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

• We know: hints override parallelism set at table.

• Hey--use outline to override table degree?

• NO--Outline removes any Parallel Hint

Use Outline to Fix

Parallelism?

Page 18: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Questions on Outlines?

Page 19: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Tip 2: Easy Sql Profiles

• Not reliable, but easy.• We have 127 stored outlines, and 3 sql

profiles.• For example, an OEM query had odd hint that

would not work with stored outline.

Page 20: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Tell me More!

• A sql profile is not actually an execution plan.

• It’s a set of sql hints ready for next time sql is run.

• Like stored outlines, but hints are different.

How different?

Page 21: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

How do Sql Profiles Work?

• They use large amounts of cpu time.• They can postulate lots of different

plans.

They use same optimizer that produced poor execution plan in the first place!

Page 22: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Hints, Hints Everywhere

• Outline uses sql hints to preserve execution plan

• Like “full” or “index.”• Profile hints give optimizer extra

information.

Outline tends to lock-in a plan, profile makes optimizer smarter.

Page 23: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

What do I do?

• Step 1: Create the Tuning Task• Step 2: Run The Tuning Task• Step 3: Get Recommendations• Step 4: Apply The Profile• Step 5: Confirm Profile is

Enabled

Page 24: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Step 1 

Create the Tuning Task• Call

Dbms_Sqltune.Create_Tuning_Task• Provide a specific sql_id• Give time-limit

Page 25: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Step 2: Run Tuning Task

Begin Dbms_Sqltune.Execute_Tuning_Task( Task_Name => 'chris1' );End; Now, get Recommendations

Page 26: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Step 3 Recommendations

Set Long 32000 [to get full listing]Set Longchunksize 1000Set Linesize 100Select Dbms_Sqltune.Report_Tuning_Task( 'chris1') From Dual;  

This long report will include a summary of recommendations

Page 27: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Step 3 Look for this:

SQL Profile Finding (see explain plans section below)

A potentially better execution plan was found for this statement.

Recommendation (estimated benefit: 67.35%)------------------------------------------- Consider accepting the recommended SQL profile.

Page 28: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Step 4 Apply Profile

Execute dbms_sqltune.accept_sql_profile(task_name => 'chris1', replace => TRUE);

Page 29: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Step 5: Confirm Enabled

Select Name, Created, Type, StatusFrom Dba_Sql_ProfilesWhere Last_Modified > Sysdate – 1; NAME CREATED TYPE STATUS--------------------- -------------------- ------- --------SYS_SQLPROF_01313de6 18-JUL-11 08.38.44.AM MANUAL ENABLED 

Page 30: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Behind the Scenes

 

• What is Oracle doing with a sql profile?• What kinds of sql hints are being applied? • You can see the hints by joining

Sys.Sqlobj$Data Od, Sys.Sqlobj$

What do the hints look like?

Page 31: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Sql Profiles

 

Actual Hints

OPT_ESTIMATE(@"SEL$AF73C875", TABLE, "S"@"SEL$4", SCALE_ROWS=3024)

OPT_ESTIMATE(@"SEL$26", TABLE, "X$KSQEQ"@"SEL$26",SCALE_ROWS=8208.205)

OPT_ESTIMATE(@"SEL$34", TABLE, "X$KTCXB"@"SEL$34",SCALE_ROWS=162.5641)

• Opt_Estimate hint supplies cardinality information.

• Scale_Rows parameter scales the estimate of the rows

Page 32: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Sql Profiles: A Big “Plus”

 Profile can handle changing literals (unlike stored outline)

Use Force_Match, like this:dbms_sqltune.accept_sql_profile(task_name => 'chris1', - replace => TRUE, force_match => TRUE);

Page 33: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Sql Profiles“Minus”

 

• Syntax awkward if you need to supply sql.

• For instance, if database has recently started.

• Especially awkward if sql has quotation marks.

• Stored outline is trivial to create for specific sql.

Page 34: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

A Funny Quirk

 

In Dba_Hist_SqlstatSql_Profile is listedOutline_Category is not.

Page 35: Using Stored Outlines & Profiles Chris Lawson. TIP 1: EASY SQL HINTS USING STORED OUTLINES No reason to avoid outlines. Despite threats, are reportedly

Questions?