DBA-T2.C6-Database Optimization
DBA-T2.C6-Database Optimization
2
3. JOIN and SORT Optimization
● JOIN optimization
● SORT optimization
3
3.1 JOIN operation optimization
4
Practice C6_P1
5
Nested-LOOP Join [T1 (t1.a=t2.b) T2]
* 6
Sort-MERGE Join: [T1 (t1.a=t2.b) T2]
-- Step 1:
FOR rec_t1 IN (SELECT * FROM t1) LOOP
Save_Sorted(sort_result_of => ‘T1_S’, sort_key => ‘T1.A’, sort_val => rec_t1.a
sorted_record => rec_t1);
END LOOP;
FOR rec_t2 IN (SELECT * FROM t2) LOOP
Save_Sorted(sort_result _of => ‘T2_S’, sort_key => ‘T2.B’, sort_val => rec_t2.b
sorted_record => rec_t2);
END LOOP;
-- Step 2
WHILE T1_S.COUNT > 0 AND T2_S.COUNT > 0 LOOP
head_t1_s := t1_s(1).a;
head_t2_s := t2_s(1).a;
IF head_t1_s > head_t2_s THEN
Remove_Head_Of(‘T2_S’);
ELSE IF head_t1_s < head_t2_s THEN
Remove_Head_Of(‘T1_S’);
ELSE IF Join_Predicate(head_t1_s , head_t1_s )
Save_Joined(t1_s(1), t2_s(1));
Remove_Head_Of(‘T1_S’);
Remove_Head_Of(‘T2_S’);
END;
END LOOP;
7
HASH Join [T1 (t1.a=t2.b) T2]
8
HASH Join [T1 (t1.a=t2.b) T2]
-- Step 1:
-- Hash driving table: optional, only if not fit in hash_area_size
FOR rec_t1 IN (SELECT * FROM t1) LOOP
hash_value := hash_partitioning_function(rec_t1.a);
IF NOT Exists_Partition (partition_hash = > hash_value, source => ‘T1’) THEN
Create_Partition(partition_hash = > hash_value, source => ‘T1’);
END IF;
Add_rec_Into_Hash_Partition(hash_key => hash_value, source=> ‘T1’, record => rec_t1);
END LOOP;
9
HASH Join [T1 (t1.a=t2.b) T2]
-- Step 2:
FOR i IN 1..Partition_Count(source => T1) LOOP
-- build [in memory] table for partition i of T1 --
FOR rec_t1_part_i IN
(SELECT * FROM Partition_Table(source=>t1, partition_no=>i))
LOOP
hash_ref_value := Hash_Probe_Function(rec_t1_part_i.a);
In_Memory_Tbl(/*indexed_key =>*/ hash_ref_value) =
/*row_physical_pointer =>*/ rec_t1_part_i .rowid;
END LOOP;
-- probe partition i of T2 against [in memory] table of partition i of T1 --
FOR rec_t2_part_i IN
(SELECT * FROM Partition_Table(source=>t2, partition_no=>i))
LOOP
hash_probe_value := Hash_Probe_Function(rec_t2_part_i.b);
row_physical_pointer _t1 := In_Memory_Tbl(hashe_probe_value) ;
IF row_physical_pointer _t1 IS NOT NULL THEN
rec_t1_i := read_record_from_physical_pointer(
row_physical_pointer _t1);
Save_Joined(rec_t1_i, rec_t2_part_i);
END IF;
END LOOP;
END LOOP; 10
JOIN Operation optimization
11
Practice C6_P1
12
3.2 SORT Operation optimization
● Logical SQL query blocks:
○ ORDER BY, GROUP BY
○ Aggregate functions: MIN, MAX
○ DISTINCT
○ JOIN
○ OVER (ORDER BY),
■ FIRST_VALUE, LAST_VALUE, RANK
● OEP SORT operations:
○ SORT ORDER BY
○ SORT GROUP BY
○ SORT AGGREGATE
○ SORT JOIN
○ WINDOW SORT
13
SQL Query blocks and
OEP SORT Operations
SQL Query Block OEP Operations
14
SORT Operation optimization
15
Practice C6_P2
● To simulate workloads:
○ /*+ cardinality (tbl 1e9)*/
■ 1e9 = 1000 Million rows
○ /*+ leading (t1 t2 t3 t4)*/
■ control joining order
17
3.2 More Advanced Techniques
● Subqueries refactoring
● Temporary tables and materialized viewed
● Parallel queries
18
Subquery refactoring
● Rewriting subqueries:
○ using WITH clause preceding main SELECT statement;
○ adding /*+ materialize */ HINT to avoid VIEW operations within execution plan.
● Hints used to avoid automatic subquery rewriting:
○ NO_MERGE to avoid outer and inner inline views to be combined into a single query;
○ NO_REWRITE to avoid query rewrite in favor of materialized views.
19
Materialized Views
20
Materialized Views
● Server configuration:
○ parameters:
■ Query_rewrite_enabled = true
■ Query_rewrite_integrity = enforced
■ Optimizer_mode = ALL_ROWS
○ user privileges to create and enable MVIEW features:
■ CREATE MATERIALIZED VIEW
■ QUERY REWRITE
* 21
Materialized Views
* 22
Materialized Views
* 23
Materialized Views
* 24
Parallel Queries
25
Practice C6_P3
26
References
27
References
28