independent consultant available for consulting in-house workshops cost-based optimizer performance...

23
ORACLE COST-BASED OPTIMIZER ADVANCED Randolf Geist http://oracle- randolf.blogspot.com [email protected]

Upload: simon-meadowcroft

Post on 28-Mar-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

ORACLE COST-BASED OPTIMIZER ADVANCED

Randolf Geisthttp://oracle-randolf.blogspot.com

[email protected]

Page 2: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

ABOUT ME

Independent consultant Available for consulting In-house workshops

Cost-Based Optimizer

Performance By Design

Performance Troubleshooting Oracle ACE Director

Member of OakTable Network

Page 3: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

OPTIMIZER BASICS

Three main questions you should ask when looking for an efficient execution plan:

How much data? How many rows / volume?

How scattered / clustered is the data?

Caching?

=> Know your data!

Page 4: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

OPTIMIZER BASICS

Why are these questions so important?

Two main strategies:

One “Big Job” => How much data, volume?

Few/many “Small Jobs” => How many times / rows?=> Effort per iteration? Clustering / Caching

Page 5: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

OPTIMIZER BASICS

Optimizer’s cost estimate is based on:

How much data? How many rows / volume?

How scattered / clustered? (partially)

(Caching?) Not at all

Page 6: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

BASICS’ SUMMARY

Cardinality and Clustering determine whether the “Big Job” or “Small Job” strategy should be preferred

If the optimizer gets these estimates right, the resulting execution plan will be efficient within the boundaries of the given access paths

Know your data and business questions

Page 7: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

AGENDA

Clustering Factor

Statistics / Histograms

Datatype issues

Page 8: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HOW SCATTERED / CLUSTERED?

1,000 rows => visit 1,000 table blocks: 1,000 * 5ms = 5 s

Page 9: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HOW SCATTERED / CLUSTERED?

1,000 rows => visit 10 table blocks: 10 * 5ms = 50 ms

Page 10: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HOW SCATTERED / CLUSTERED?

There is only a single measure of clustering in Oracle: The index clustering factor

The index clustering factor is represented by a single value

The logic measuring the clustering factor by default does not cater for data clustered across few blocks (ASSM!)

Page 11: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HOW SCATTERED / CLUSTERED?

Challenges

Getting the index clustering factor right

There are various reasons why the index clustering factor measured by Oracle might not be representative Multiple freelists / freelist groups (MSSM) ASSM Partitioning SHRINK SPACE effects

Page 12: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HOW SCATTERED / CLUSTERED?

Re-visiting the same recent table blocks

Page 13: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

STATISTICS

Don’t use ANALYZE … COMPUTE / ESTIMATE STATISTICS anymore

Basic Statistics:

- Table statistics: Blocks, Rows, Avg Row LenNothing to configure there, always generated

- Basic Column Statistics: Low / High Value, Num Distinct, Num Nulls=> Controlled via METHOD_OPT option ofDBMS_STATS.GATHER_TABLE_STATS

Page 14: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

STATISTICS

Controlling column statistics via METHOD_OPT

If you see FOR ALL INDEXED COLUMNS [SIZE > 1]:Question it! Only applicable if the author really knows what he/she is doing! => Without basic column statistics Optimizer is resorting to hard coded defaults!

Default in previous releases:FOR ALL COLUMNS SIZE 1: Basic column statistics for all columns, no histograms

Default from 10g on:FOR ALL COLUMNS SIZE AUTO: Basic column statistics for all columns, histograms if Oracle determines so

Page 15: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HISTOGRAMS

Basic column statistics get generated along with table statistics in a single pass (almost)

Each histogram requires a separate pass

Therefore Oracle resorts to aggressive sampling if allowed => AUTO_SAMPLE_SIZE

This limits the quality of histograms and their significance

Page 16: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HISTOGRAMS

Limited resolution of 255 value pairs maximum

Less than 255 distinct column values => Frequency Histogram

More than 255 distinct column values =>Height Balanced Histogram

Height Balanced is always a sampling of data, even when computing statistics!

Page 17: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

FREQUENCY HISTOGRAMS

SIZE AUTO generates Frequency Histograms if a column gets used as a predicate and it has less than 255 distinct values

Major change in behaviour of histograms introduced in 10.2.0.4 / 11g

Be aware of new “value not found in Frequency Histogram” behaviour

Be aware of edge case of very popular / unpopular values

Page 18: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

SELECT SKEWED_NUMBER FROM T ORDER BY SKEWED_NUMBER

HEIGHT BALANCED HISTOGRAMS

1,000

2,000

3,000

4,000

5,000

1

70,000

140,000

210,000

280,000

350,000

10,000,000 rows

100 popular valueswith 70,000 occurrences

250 buckets each covering40,000 rows (compute)

250 buckets each covering approx. 22/23 rows (estimate)

40,000

80,000

120,000

160,000

200,000

240,000

280,000

320,000

360,000

400,000

1,000

2,000

2,000

3,000

3,000

4,000

4,000

5,000

6,000

6,000

RowsRowsEndpoint

Page 19: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HEIGHT BALANCED HISTOGRAMS

1,000

100,000

7,000,000

10,000,000

Page 20: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

HEIGHT BALANCED HISTOGRAMS

1,000

100,000

7,000,000

10,000,000

Page 21: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

SUMMARY

Check the correctness of the clustering factor for your critical indexes

Oracle does not know the questions you ask about the data

You may want to use FOR ALL COLUMNS SIZE 1 as default and only generate histograms where really necessary

You may get better results with the old histogram behaviour, but not always

Page 22: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

SUMMARY

There are data patterns that don’t work well with histograms when generated via Oracle

=> You may need to manually generate histograms using DBMS_STATS.SET_COLUMN_STATS for critical columns

Don’t forget about Dynamic Sampling / Function Based Indexes / Virtual Columns / Extended Statistics

Know your data and business questions!

Page 23: Independent consultant Available for consulting In-house workshops Cost-Based Optimizer Performance By Design Performance Troubleshooting Oracle ACE Director

QUESTIONS & ANSWERS

Q & A