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
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
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
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
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
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
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
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
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
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
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
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
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
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