SlideShare ist ein Scribd-Unternehmen logo
1 von 71
Introduction to
Oracle Optimizer
Heribertus Bramundito
Agenda
 Our Environment
 Heap Table

 Histogram

 Oracle Optimizer
 Cost
 Selectivity & Cardinality
 Clustering Factor

 Access Method
 Partitioning
 Join Method
 Sub-query
 Hint
Our Environment
Heap Table
 This is default table when we issue the CREATE TABLE

statement
 Data is stored in random fashion, no specific sort of order
(best fit algorithm)

3
Our Environment
Heap Table (result from table access)

4
Our Environment
Heap Table (result from index access)

5
Our Environment
IOT Table (a comparison)

6
Our Environment
Histogram
 Collection of information about data distribution in

specific column
 Oracle maintains 2 types of histogram: frequency and
height-balanced
 Oracle use histogram as additional information when
deciding whether to use index scan or table scan

7
Our Environment
Histogram (column without histogram)

8
Our Environment
Histogram (create histogram)

9
Our Environment
Histogram (column with histogram)

10
Agenda
 Our Environment
 Heap Table

 Histogram

 Oracle Optimizer
 Cost
 Selectivity & Cardinality
 Clustering Factor

 Access Method
 Partitioning
 Join Method
 Sub-query
 Hint
Cost
 Jonathan Lewis: “The cost represents the optimizer‟s best estimate of the

time it will take to execute the statement”
 A result of the calculation performed by optimizer
 Few conditions that make CBO produces wrong result”
 No statistics on the underlying objects or statistics are obsolete
 Performance characteristics of hardware or current workloads are not known
 Bug

 From Oracle Performance Tuning Guide and Reference

Cost = (#SRds * sreadtim + #MRds * mreadtim +
#CPUCycles / cpuspeed) / sreadtim
#SRds: number of single data block reads
#MRds: number of multi data block reads
#CPUCycle: number of CPU cycles
sreadtim: single block read time
mreadtim: multi block read time
cpuspeed: CPU cycles per second
12
Cost (the calculation)
 #SRds = blevel + #leaf_block * idx_sel + clustering_factor * tbl_sel (index





scan)
#MRds = #tbl_blocks under HWM / mbrc (full table/ index scan)
sreadtim = ioseektim + block_size * iotfrspeed (SREADTIM)
mreadtim = ioseektim + mbrc * block_size * iotfrspeed (MREADTIM)
mbrc = db_file_multiblock_read_count (MBRC)

 Going back to slide #4 (let‟s try to calculate cost of table

scan)
 Cost = (24 / 16) * (10 + 16 * 8192 / 4092) / (10 + 8192 / 4092) = 1.5 * 42 /

12 = 5.25

 Slide #5 (index scan only)
 Cost = 0 + ceil(1 * 0.0588) = ceil(0.0588) = 1

 Slide #10 (index scan with access to the table)
 Cost = 0 + 1 + ceil((4 / 17) * 20) = 1 + ceil(4.705) = 1 + 5 = 6
13
Selectivity & Cardinality
 Selectivity is what fraction of row the predicate is supposed to fetch/ return

Selectivity = 1 / num_distinct (no histogram)

Selectivity = density (histogram)
 Cardinality is total number of row the predicate is supposed to return

Cardinality = num_rows * density
 Going back to slide #4


Cardinality = num_rows = 20

 Slide #5


Selectivity = (number of bucket with data / number of total bucket) = 17 / 17 = 1



Cardinality = selectivity * num_rows = 1 * 20 = 20

 Slide #10


14

Selectivity = 4 / 17 = 0.235



Cardinality = 0.235 * 20 = 4.7
Clustering Factor
 Represents the degree to which data is randomly

distributed through a table
 Number of blocks <= clustering factor <= number of rows
 Index has better selectivity if clustering factor is close to
number of data block, means Oracle can do multi block
read on the table for several index‟s key

15
Clustering Factor (block_id from rowid)
 Going back to NORMAL_HASH table example, due to

the anomaly configuration of pctfree and pctused, every
block contains single row only

16
Access Method
 There are 2 access methods: table and index

access
 Index access can be: Fast Full Scan, Full Scan,
Unique Scan, Range Scan, Range Scan
(MIN/MAX) and Skip Scan

17
Access Method
Full Table Scan
 Oracle is reading all rows from the table
 Not suitable for OLTP system with high volume of

transaction and usually only access small fraction of data
 Suitable for DSS system with batch reporting query
 Not good for Nested Loop (NL) for the outer table (huge
table)
 Usually we see it in Hash Join (HJ)

18
Access Method
Index Fast Full Scan

 Oracle is reading all rows from the index to get the

result (doesn‟t required table access since the index
contains all columns required to resolve the query)
 To be able to use Index FFS, the column should be
defined as NOT NULL or at least one column in a
composite index is NOT NULL, the reason is that
NULL values are not included in the index creation,
so when the column is defined as NOT NULL Oracle
knows that all values are available in the index
 Index FFS will be available as an option if we put “IS
NOT NULL” in the WHERE clause explicitly
 Cost will be only for accessing the index
19
Access Method
Index Fast Full Scan (forced by hint)

20
Access Method
Index Fast Full Scan (cost calculation)
 If we remove the HINT, we will have Index Full Scan
 Cost for Index Fast Full Scan
 Cost = (8 / 16) * (10 + 16 * 8192 / 4092) / (10 + 8192 / 4092) = 0.5 * 32 / 12 =

ceil(1.333) = 2
 Cost for Index Full Scan
 Cost = 0 + ceil(1 * 1 / 14) = ceil(0.0714) = 1

21
Access Method
Index Fast Full Scan (building the example)

 Another figure from index with more leaf block

(another anomaly configuration in the pctfree of
the index)

22
Access Method
Index Fast Full Scan (the result)

23
Access Method
Index Full Scan

 Oracle is reading all rows from the index, and

may be accessing these rows in the underlying
table
Without table
access

24
Access Method
Index Full Scan (with table access)
Unique index

25
Access Method
Index Unique Scan
 Oracle is reading 0 or 1 rows from the index, only on

unique index
 Equality operator in the predicate (=), ay be seen in AND,
OR, IS NULL operator

26
Access Method
Index Unique Scan (the result)

27
Access Method
Index Range Scan

 Oracle is reading 0 or more contiguous rows

from the index
 Non unique index with range operator in the
predicate (>, <, >=, <=)

28
Access Method
Index Range Scan (MIN/MAX)

 Oracle is identifying 0 or more contiguous rows

in the index, but is reading only one (the first or
the last) in order to satisfy a MIN or MAX
aggregate function

29
Access Method
Index Skip Scan

 Oracle is reading 0 or more rows from different

parts of the index (composite index), and may be
accessing these rows in the underlying table
 Skip Scan will be happened when we access a
composite index with second column in the
index‟s order. It will not be happened for the third
column, forth, etc.
 It will works only if first column in the index has
low cardinality (few distinct value) otherwise full
table scan will be better in most of the case
30
Access Method
Index Skip Scan (building the example)

31
Access Method
Index Skip Scan (the result)
First index‟s
column with low
cardinality

32
Access Method
Index Skip Scan (a comparison)

First index‟s
column with high
cardinality

33
Partitioning
 There are some consideration when we are

working with partition table. One of its is
regarding access path. We introduce partitioning
on the table usually to reduce the number of
rows which will affected by any query, since we
know that not all of those rows are being used in
the query.
 There are 4 kinds of access method for
partitioned table: Partition Range Single,
Partition Range Iterator, Partition Range All and
Partition Range Sub-query
34
Partitioning (building the example)

35
Partitioning
Partition Range Single

 Exactly only single partition of the table involves

in the query. Access path on this partition
depends on the query, can be Table Scan or any
Index Scan

36
Partitioning
Partition Range Iterator

 We will see this kind of access path whenever

we have several partitions in the query

37
Partitioning
Partition Range All
 In this kind of access method, all partitions in the table

will be scanned. This is a bad example of table design 
(create a partition table without taking any benefit of it)

38
Partitioning
Partition Range Sub-query (building an example)

 This method is new in 10g. If the partitioned table

is bigger compare to the other join table and the
expected number of the records (result) is
significantly less, Oracle will perform dynamic
partition pruning using sub-query
 The partitioned table will be having 200,000
blocks and the other join only 200 blocks

39
Partitioning
Partition Range Sub-query (the result)

Only 7 rows compare
to 1000 rows from
partitioned table

40
Partitioning
Partition Range Sub-query (a comparison)

41
Join Method
 There are 3 join methods: Nested Loop (NL),

Hash Join (HJ) and Sort Merge Join (SM)
 Most of the time we see only „standard‟ join
between 2 tables, but in rare case we will see
Anti-Join and Semi-Join variation for all above 3
methods. Anti-Join will be appear when we are
working with NOT IN clause while Semi-Join will
be appear when we are working with EXISTS
clause

42
Join Method
Nested Loop

 The Nested means an iteration. Pseudo-code for

this kind of join will be like below:
for x in (select [col] from outer_table) loop
for y in (select [col] from inner_table where outer_table.join_col =
inner_table.join_col) loop
return the rows from outer and inner table
end loop;
end loop;

 Suitable for small “size” for the outer (driving)

table. For the inner table, it should be accessed
using index scan
 Starting from 9i, Oracle introduces new „table
prefetching‟ method which will reduce logical I/O
43
Join Method
Nested Loop (building the example)

44
Join Method
Nested Loop (table prefetching method)

note down the
logical I/O

45
Join Method
Nested Loop (legacy method)

recreate with
UNIQUE index
46
Join Method
Nested Loop (the result)

47
Join Method
Hash Join

 In this method, first Oracle will choose 1 dataset

(build table – this is outer table in Nested Loop), and
then create hash table in memory using generated
hash-key from join column. Once completed, second
table (probe table – this is inner table in Nested
Loop) will be scanned using the same hash function
(probing the hash table)
 Applicable for join with equality operator (=)
 There are 3 level of effectiveness: optimal, one-pass
and multi-pass. Optimal when the size of tables is
matched with hash_area_size. One-pass or Multipass when the tables is not enough to be hash-ed in
the memory (requires disk operation)
 Event 10104 for tracing Hash Join operation
48
Join Method
Hash Join (cont.)

 Check hash_area_size and

workarea_size_policy database parameter
 Check v$sysstat for relevant system statistics
SELECT name, value, case when sum(value) over() = 0 then 0
else round(value*100/sum(value) over(),2) end as pct
FROM v$sysstat

WHERE name LIKE 'workarea executions%'

49
Join Method
Hash Join (components)

50
Join Method
Hash Join (build the example)

51
Join Method
Hash Join (the result – optimal)

workarea_size_policy = AUTO
test on SMALL table

52
Join Method
Hash Join (the result – optimal, part 2)

workarea_size_policy = AUTO
test on BIG table

53
Join Method
Hash Join (with index scan on the tables)

54
Join Method
Hash Join (event 10104)

55
Join Method
Sort Merge
 There are 2 operation in this method: sort and merge. So






56

it is application for any query which requires sorting (on
the join column): Order By clause, Group By clause, Set
operation, Distinct operator, Analytical function, Index
creation, Connect By query and etc
Similar to Hash Join, there are 3 level of effectiveness for
sorting operation: optimal, one-pass and multi-pass.
Optimal when the size sort_area_size is enough to
handle sort operation. One-pass or Multi-pass when
Oracle requires disk operation for the sorting
Event 10032 for tracing sort operation and 10033 for
tracing sort I/O operation
Check sort_area_size and workarea_size_policy
database parameter
Check v$sysstat for relevant system statistics and
v$tempstat for sorting statistics
Join Method
Sort Merge (cont.)

 Merging part can be one of the following

possibilities:

57
Join Method
Sort Merge (build the example)

58
Join Method
Sort Merge (sorting only example)

59
Join Method
Sort Merge (sorting only example – cont.)

60
Join Method
Sort Merge (the example)

61
Join Method
Sort Merge (Merge Join Cartesian)

62
Join Method
Sort Merge (event 10032)

63
Sub-query
 There 2 main types of sub-query: Nested Sub-

query and Correlated Sub-query
 Nested sub-query when the sub-query (inner
query) need to be completed first and then the
result will be passed to the main query
 Correlated sub-query when the main query
should be executed first in order to execute the
inner query
 In some cases we can rewrite sub-query into join
form for performance improvement
64
Sub-query (building the example)

65
Sub-query
Nested

 The select statement against “sub_q” is executed

first and the outputs will be used by main query

66
Sub-query
Correlated

 We can see from Predicate Information,

“sub_q” is executed for every value from
“main_q”

67
Sub-query
Correlated – rewrite into join

68
Sub-query
Correlated – comparing the statistics

Check
logical I/O
69
Hint
 Some of famous Oracle hints
 PARALLEL, PARALLEL_INDEX
 FULL
 INDEX, INDEX_SS, INDEX_FFS
 LEADING
 ORDERED
 DRIVING_SITE
 USE_NL, USE_HASH, USE_MERGE
 APPEND
 USE_CONCAT

 Other hints
 MERGE_AJ, HASH_AJ
 MERGE_SJ, HASH_SJ
 UNNEST, NO_UNNEST

70
References
 Jonathan Lewis, Cost-Based Oracle






71



Fundamentals
Thomas Kyte, Expert Oracle Database
Architecture – 9i and 10g Programming
Techniques and Solutions
http://www.orafaq.com/tuningguide/
http://oraclerandolf.blogspot.com/2011/10/paralleldowngrade.html
http://asktom.oracle.com/pls/asktom/f?p=100:1:0::
NO::
http://jonathanlewis.wordpress.com/

Weitere ähnliche Inhalte

Was ist angesagt?

Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
Sql Queries
Sql QueriesSql Queries
Sql Querieswebicon
 
IBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinIBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinAjay Gupte
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and searchEstiak Khan
 
Discover the power of Recursive SQL and query transformation with Informix da...
Discover the power of Recursive SQL and query transformation with Informix da...Discover the power of Recursive SQL and query transformation with Informix da...
Discover the power of Recursive SQL and query transformation with Informix da...Ajay Gupte
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationProf Ansari
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
MySQL index optimization techniques
MySQL index optimization techniquesMySQL index optimization techniques
MySQL index optimization techniqueskumar gaurav
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Circular linked list
Circular linked listCircular linked list
Circular linked listdchuynh
 
Circular linked list
Circular linked listCircular linked list
Circular linked listmaamir farooq
 

Was ist angesagt? (20)

linked list
linked list linked list
linked list
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Linked List
Linked ListLinked List
Linked List
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
linked list
linked listlinked list
linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
IBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinIBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash Join
 
Migration
MigrationMigration
Migration
 
Linklist
LinklistLinklist
Linklist
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
 
Discover the power of Recursive SQL and query transformation with Informix da...
Discover the power of Recursive SQL and query transformation with Informix da...Discover the power of Recursive SQL and query transformation with Informix da...
Discover the power of Recursive SQL and query transformation with Informix da...
 
Linked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory AllocationLinked List Static and Dynamic Memory Allocation
Linked List Static and Dynamic Memory Allocation
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
MySQL index optimization techniques
MySQL index optimization techniquesMySQL index optimization techniques
MySQL index optimization techniques
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
single linked list
single linked listsingle linked list
single linked list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 

Ähnlich wie Introduction to oracle optimizer

Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuningAnil Pandey
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
Database Performance
Database PerformanceDatabase Performance
Database PerformanceBoris Hristov
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsDave Stokes
 
Database index
Database indexDatabase index
Database indexRiteshkiit
 
Oracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive PlansOracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive PlansFranck Pachot
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
Top 10 sap abap faqs-www.bigclasses.com
Top 10 sap abap faqs-www.bigclasses.comTop 10 sap abap faqs-www.bigclasses.com
Top 10 sap abap faqs-www.bigclasses.combigclasses.com
 
Maryna Popova "Deep dive AWS Redshift"
Maryna Popova "Deep dive AWS Redshift"Maryna Popova "Deep dive AWS Redshift"
Maryna Popova "Deep dive AWS Redshift"Lviv Startup Club
 
Indexing Strategies
Indexing StrategiesIndexing Strategies
Indexing Strategiesjlaspada
 

Ähnlich wie Introduction to oracle optimizer (20)

hashing.pdf
hashing.pdfhashing.pdf
hashing.pdf
 
Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuning
 
Ora faq
Ora faqOra faq
Ora faq
 
Ora faq
Ora faqOra faq
Ora faq
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Database Performance
Database PerformanceDatabase Performance
Database Performance
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
 
Database index
Database indexDatabase index
Database index
 
Oracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive PlansOracle Join Methods and 12c Adaptive Plans
Oracle Join Methods and 12c Adaptive Plans
 
Bo4301369372
Bo4301369372Bo4301369372
Bo4301369372
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Top 10 sap abap faqs-www.bigclasses.com
Top 10 sap abap faqs-www.bigclasses.comTop 10 sap abap faqs-www.bigclasses.com
Top 10 sap abap faqs-www.bigclasses.com
 
Optimizing MySQL queries
Optimizing MySQL queriesOptimizing MySQL queries
Optimizing MySQL queries
 
Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
 
Hash join
Hash joinHash join
Hash join
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
Maryna Popova "Deep dive AWS Redshift"
Maryna Popova "Deep dive AWS Redshift"Maryna Popova "Deep dive AWS Redshift"
Maryna Popova "Deep dive AWS Redshift"
 
MySQL Indexes
MySQL IndexesMySQL Indexes
MySQL Indexes
 
Etl2
Etl2Etl2
Etl2
 
Indexing Strategies
Indexing StrategiesIndexing Strategies
Indexing Strategies
 

Kürzlich hochgeladen

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 

Kürzlich hochgeladen (20)

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 

Introduction to oracle optimizer

  • 2. Agenda  Our Environment  Heap Table  Histogram  Oracle Optimizer  Cost  Selectivity & Cardinality  Clustering Factor  Access Method  Partitioning  Join Method  Sub-query  Hint
  • 3. Our Environment Heap Table  This is default table when we issue the CREATE TABLE statement  Data is stored in random fashion, no specific sort of order (best fit algorithm) 3
  • 4. Our Environment Heap Table (result from table access) 4
  • 5. Our Environment Heap Table (result from index access) 5
  • 6. Our Environment IOT Table (a comparison) 6
  • 7. Our Environment Histogram  Collection of information about data distribution in specific column  Oracle maintains 2 types of histogram: frequency and height-balanced  Oracle use histogram as additional information when deciding whether to use index scan or table scan 7
  • 8. Our Environment Histogram (column without histogram) 8
  • 10. Our Environment Histogram (column with histogram) 10
  • 11. Agenda  Our Environment  Heap Table  Histogram  Oracle Optimizer  Cost  Selectivity & Cardinality  Clustering Factor  Access Method  Partitioning  Join Method  Sub-query  Hint
  • 12. Cost  Jonathan Lewis: “The cost represents the optimizer‟s best estimate of the time it will take to execute the statement”  A result of the calculation performed by optimizer  Few conditions that make CBO produces wrong result”  No statistics on the underlying objects or statistics are obsolete  Performance characteristics of hardware or current workloads are not known  Bug  From Oracle Performance Tuning Guide and Reference Cost = (#SRds * sreadtim + #MRds * mreadtim + #CPUCycles / cpuspeed) / sreadtim #SRds: number of single data block reads #MRds: number of multi data block reads #CPUCycle: number of CPU cycles sreadtim: single block read time mreadtim: multi block read time cpuspeed: CPU cycles per second 12
  • 13. Cost (the calculation)  #SRds = blevel + #leaf_block * idx_sel + clustering_factor * tbl_sel (index     scan) #MRds = #tbl_blocks under HWM / mbrc (full table/ index scan) sreadtim = ioseektim + block_size * iotfrspeed (SREADTIM) mreadtim = ioseektim + mbrc * block_size * iotfrspeed (MREADTIM) mbrc = db_file_multiblock_read_count (MBRC)  Going back to slide #4 (let‟s try to calculate cost of table scan)  Cost = (24 / 16) * (10 + 16 * 8192 / 4092) / (10 + 8192 / 4092) = 1.5 * 42 / 12 = 5.25  Slide #5 (index scan only)  Cost = 0 + ceil(1 * 0.0588) = ceil(0.0588) = 1  Slide #10 (index scan with access to the table)  Cost = 0 + 1 + ceil((4 / 17) * 20) = 1 + ceil(4.705) = 1 + 5 = 6 13
  • 14. Selectivity & Cardinality  Selectivity is what fraction of row the predicate is supposed to fetch/ return Selectivity = 1 / num_distinct (no histogram) Selectivity = density (histogram)  Cardinality is total number of row the predicate is supposed to return Cardinality = num_rows * density  Going back to slide #4  Cardinality = num_rows = 20  Slide #5  Selectivity = (number of bucket with data / number of total bucket) = 17 / 17 = 1  Cardinality = selectivity * num_rows = 1 * 20 = 20  Slide #10  14 Selectivity = 4 / 17 = 0.235  Cardinality = 0.235 * 20 = 4.7
  • 15. Clustering Factor  Represents the degree to which data is randomly distributed through a table  Number of blocks <= clustering factor <= number of rows  Index has better selectivity if clustering factor is close to number of data block, means Oracle can do multi block read on the table for several index‟s key 15
  • 16. Clustering Factor (block_id from rowid)  Going back to NORMAL_HASH table example, due to the anomaly configuration of pctfree and pctused, every block contains single row only 16
  • 17. Access Method  There are 2 access methods: table and index access  Index access can be: Fast Full Scan, Full Scan, Unique Scan, Range Scan, Range Scan (MIN/MAX) and Skip Scan 17
  • 18. Access Method Full Table Scan  Oracle is reading all rows from the table  Not suitable for OLTP system with high volume of transaction and usually only access small fraction of data  Suitable for DSS system with batch reporting query  Not good for Nested Loop (NL) for the outer table (huge table)  Usually we see it in Hash Join (HJ) 18
  • 19. Access Method Index Fast Full Scan  Oracle is reading all rows from the index to get the result (doesn‟t required table access since the index contains all columns required to resolve the query)  To be able to use Index FFS, the column should be defined as NOT NULL or at least one column in a composite index is NOT NULL, the reason is that NULL values are not included in the index creation, so when the column is defined as NOT NULL Oracle knows that all values are available in the index  Index FFS will be available as an option if we put “IS NOT NULL” in the WHERE clause explicitly  Cost will be only for accessing the index 19
  • 20. Access Method Index Fast Full Scan (forced by hint) 20
  • 21. Access Method Index Fast Full Scan (cost calculation)  If we remove the HINT, we will have Index Full Scan  Cost for Index Fast Full Scan  Cost = (8 / 16) * (10 + 16 * 8192 / 4092) / (10 + 8192 / 4092) = 0.5 * 32 / 12 = ceil(1.333) = 2  Cost for Index Full Scan  Cost = 0 + ceil(1 * 1 / 14) = ceil(0.0714) = 1 21
  • 22. Access Method Index Fast Full Scan (building the example)  Another figure from index with more leaf block (another anomaly configuration in the pctfree of the index) 22
  • 23. Access Method Index Fast Full Scan (the result) 23
  • 24. Access Method Index Full Scan  Oracle is reading all rows from the index, and may be accessing these rows in the underlying table Without table access 24
  • 25. Access Method Index Full Scan (with table access) Unique index 25
  • 26. Access Method Index Unique Scan  Oracle is reading 0 or 1 rows from the index, only on unique index  Equality operator in the predicate (=), ay be seen in AND, OR, IS NULL operator 26
  • 27. Access Method Index Unique Scan (the result) 27
  • 28. Access Method Index Range Scan  Oracle is reading 0 or more contiguous rows from the index  Non unique index with range operator in the predicate (>, <, >=, <=) 28
  • 29. Access Method Index Range Scan (MIN/MAX)  Oracle is identifying 0 or more contiguous rows in the index, but is reading only one (the first or the last) in order to satisfy a MIN or MAX aggregate function 29
  • 30. Access Method Index Skip Scan  Oracle is reading 0 or more rows from different parts of the index (composite index), and may be accessing these rows in the underlying table  Skip Scan will be happened when we access a composite index with second column in the index‟s order. It will not be happened for the third column, forth, etc.  It will works only if first column in the index has low cardinality (few distinct value) otherwise full table scan will be better in most of the case 30
  • 31. Access Method Index Skip Scan (building the example) 31
  • 32. Access Method Index Skip Scan (the result) First index‟s column with low cardinality 32
  • 33. Access Method Index Skip Scan (a comparison) First index‟s column with high cardinality 33
  • 34. Partitioning  There are some consideration when we are working with partition table. One of its is regarding access path. We introduce partitioning on the table usually to reduce the number of rows which will affected by any query, since we know that not all of those rows are being used in the query.  There are 4 kinds of access method for partitioned table: Partition Range Single, Partition Range Iterator, Partition Range All and Partition Range Sub-query 34
  • 36. Partitioning Partition Range Single  Exactly only single partition of the table involves in the query. Access path on this partition depends on the query, can be Table Scan or any Index Scan 36
  • 37. Partitioning Partition Range Iterator  We will see this kind of access path whenever we have several partitions in the query 37
  • 38. Partitioning Partition Range All  In this kind of access method, all partitions in the table will be scanned. This is a bad example of table design  (create a partition table without taking any benefit of it) 38
  • 39. Partitioning Partition Range Sub-query (building an example)  This method is new in 10g. If the partitioned table is bigger compare to the other join table and the expected number of the records (result) is significantly less, Oracle will perform dynamic partition pruning using sub-query  The partitioned table will be having 200,000 blocks and the other join only 200 blocks 39
  • 40. Partitioning Partition Range Sub-query (the result) Only 7 rows compare to 1000 rows from partitioned table 40
  • 42. Join Method  There are 3 join methods: Nested Loop (NL), Hash Join (HJ) and Sort Merge Join (SM)  Most of the time we see only „standard‟ join between 2 tables, but in rare case we will see Anti-Join and Semi-Join variation for all above 3 methods. Anti-Join will be appear when we are working with NOT IN clause while Semi-Join will be appear when we are working with EXISTS clause 42
  • 43. Join Method Nested Loop  The Nested means an iteration. Pseudo-code for this kind of join will be like below: for x in (select [col] from outer_table) loop for y in (select [col] from inner_table where outer_table.join_col = inner_table.join_col) loop return the rows from outer and inner table end loop; end loop;  Suitable for small “size” for the outer (driving) table. For the inner table, it should be accessed using index scan  Starting from 9i, Oracle introduces new „table prefetching‟ method which will reduce logical I/O 43
  • 44. Join Method Nested Loop (building the example) 44
  • 45. Join Method Nested Loop (table prefetching method) note down the logical I/O 45
  • 46. Join Method Nested Loop (legacy method) recreate with UNIQUE index 46
  • 47. Join Method Nested Loop (the result) 47
  • 48. Join Method Hash Join  In this method, first Oracle will choose 1 dataset (build table – this is outer table in Nested Loop), and then create hash table in memory using generated hash-key from join column. Once completed, second table (probe table – this is inner table in Nested Loop) will be scanned using the same hash function (probing the hash table)  Applicable for join with equality operator (=)  There are 3 level of effectiveness: optimal, one-pass and multi-pass. Optimal when the size of tables is matched with hash_area_size. One-pass or Multipass when the tables is not enough to be hash-ed in the memory (requires disk operation)  Event 10104 for tracing Hash Join operation 48
  • 49. Join Method Hash Join (cont.)  Check hash_area_size and workarea_size_policy database parameter  Check v$sysstat for relevant system statistics SELECT name, value, case when sum(value) over() = 0 then 0 else round(value*100/sum(value) over(),2) end as pct FROM v$sysstat WHERE name LIKE 'workarea executions%' 49
  • 50. Join Method Hash Join (components) 50
  • 51. Join Method Hash Join (build the example) 51
  • 52. Join Method Hash Join (the result – optimal) workarea_size_policy = AUTO test on SMALL table 52
  • 53. Join Method Hash Join (the result – optimal, part 2) workarea_size_policy = AUTO test on BIG table 53
  • 54. Join Method Hash Join (with index scan on the tables) 54
  • 55. Join Method Hash Join (event 10104) 55
  • 56. Join Method Sort Merge  There are 2 operation in this method: sort and merge. So     56 it is application for any query which requires sorting (on the join column): Order By clause, Group By clause, Set operation, Distinct operator, Analytical function, Index creation, Connect By query and etc Similar to Hash Join, there are 3 level of effectiveness for sorting operation: optimal, one-pass and multi-pass. Optimal when the size sort_area_size is enough to handle sort operation. One-pass or Multi-pass when Oracle requires disk operation for the sorting Event 10032 for tracing sort operation and 10033 for tracing sort I/O operation Check sort_area_size and workarea_size_policy database parameter Check v$sysstat for relevant system statistics and v$tempstat for sorting statistics
  • 57. Join Method Sort Merge (cont.)  Merging part can be one of the following possibilities: 57
  • 58. Join Method Sort Merge (build the example) 58
  • 59. Join Method Sort Merge (sorting only example) 59
  • 60. Join Method Sort Merge (sorting only example – cont.) 60
  • 61. Join Method Sort Merge (the example) 61
  • 62. Join Method Sort Merge (Merge Join Cartesian) 62
  • 63. Join Method Sort Merge (event 10032) 63
  • 64. Sub-query  There 2 main types of sub-query: Nested Sub- query and Correlated Sub-query  Nested sub-query when the sub-query (inner query) need to be completed first and then the result will be passed to the main query  Correlated sub-query when the main query should be executed first in order to execute the inner query  In some cases we can rewrite sub-query into join form for performance improvement 64
  • 65. Sub-query (building the example) 65
  • 66. Sub-query Nested  The select statement against “sub_q” is executed first and the outputs will be used by main query 66
  • 67. Sub-query Correlated  We can see from Predicate Information, “sub_q” is executed for every value from “main_q” 67
  • 69. Sub-query Correlated – comparing the statistics Check logical I/O 69
  • 70. Hint  Some of famous Oracle hints  PARALLEL, PARALLEL_INDEX  FULL  INDEX, INDEX_SS, INDEX_FFS  LEADING  ORDERED  DRIVING_SITE  USE_NL, USE_HASH, USE_MERGE  APPEND  USE_CONCAT  Other hints  MERGE_AJ, HASH_AJ  MERGE_SJ, HASH_SJ  UNNEST, NO_UNNEST 70
  • 71. References  Jonathan Lewis, Cost-Based Oracle     71  Fundamentals Thomas Kyte, Expert Oracle Database Architecture – 9i and 10g Programming Techniques and Solutions http://www.orafaq.com/tuningguide/ http://oraclerandolf.blogspot.com/2011/10/paralleldowngrade.html http://asktom.oracle.com/pls/asktom/f?p=100:1:0:: NO:: http://jonathanlewis.wordpress.com/