Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Star Transformation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Star Transformation

By Ahmed Baraka

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Objectives
In this lecture, you will learn how to perform the following
• Describe and implement start transformation

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


About Optimizer Components
• Optimizer Components:
Parsing
- Query Transformer
 OR Expansion
 View Merging
Optimization
 Predicate Pushing
 Subquery Un-nesting Query Transformer
 Star Transformation
Estimator
 In-Memory Aggregation (VECTOR GROUP BY)
 Cursor-Duration Temporary Tables Plan Generator
 Table Expansion
 Join Factorization
- Estimator Row Source
Generation
- Plan Generator

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Star Schema General Layout

D2
D1 FACT PK2
PK1 PK COL1
COL1 ... COL2

COL2 …
LCOL1
… ...
D3
LCOL2 PK3
... COL1
LOCL3 COL2

SELECT ..., SUM(FACT.COLx), COUNT(FACT.COLy),...


FROM FACT F, D1,D2,D3
WHERE F.LCOL1=D1.PK1 AND F.LCOL2=D2.PK2 AND F.LCOL3=D3.PK3
AND D2.COLy IN ('..','..','..') AND D3.COLz LIKE 'A%'

Oracle© Database 12c Backup and Recovery - a course by Ahmed Baraka


Star Schema General Layout

CUSTOMERS
EMP ORDERS CUSTOMER_ID
EMP_NO ORDER_ID CUST_NAME
ENAME ... REGION

JOB_CODE …
CUSTOMER_ID
GRADE ...
... SALES_REP_ID
...

SELECT O.CUSTOMER_ID, CUST_NAME, SUM(O.ORDER_TOTAL)


FROM ORDERS O, CUSTOMERS C, EMP E
WHERE (O.CUSTOMER_ID=C.CUSTOMER_ID) AND (O.SALES_REP_ID = E.EMP_NO)
AND C.REGION='America' AND E.JOB_CODE='SL01'
GROUP BY O.CUSTOMER_ID, CUST_NAME;

Oracle© Database 12c Backup and Recovery - a course by Ahmed Baraka


Using Star Transformation
• Requirements:
- Bitmap indexes on each of the fact table foreign key columns
- The parameter STAR_TRANSFORMATION_ENABLED should be set to TRUE
(system or session level). The default is FALSE.

• Performed in two stages:


- Retrieve the required data from fact table
 Subqueries from the dimension tables that utilizes the foreign key bitmap indexes
 AND, OR, or MINUS operators are applied on the bitmaps. The output is used to
retrieve the rows from the fact table
- Rows retrieved from the fact table are joined to the dimension tables
• The technique of retrieving the data from the fact table first then from
the dimension tables is called semijoin
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Stat Transformation Implementation: Example
• Original query:
SELECT O.CUSTOMER_ID, CUST_NAME, SUM(O.ORDER_TOTAL)
FROM ORDERS O, CUSTOMERS C, EMP E
WHERE
O.CUSTOMER_ID=C.CUSTOMER_ID
AND O.SALES_REP_ID = E.EMP_NO
AND C.REGION='America'
AND E.JOB_CODE='SL01'
GROUP BY O.CUSTOMER_ID, CUST_NAME;

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Stat Transformation Implementation: Example
• Original query:
SELECT O.CUSTOMER_ID, CUST_NAME, SUM(O.ORDER_TOTAL)
FROM ORDERS O, CUSTOMERS C, EMP E
WHERE
O.CUSTOMER_ID=C.CUSTOMER_ID
AND O.SALES_REP_ID = E.EMP_NO
AND C.REGION='America'
AND E.JOB_CODE='SL01'
GROUP BY O.CUSTOMER_ID, CUST_NAME;

• Partially transformed query:


SELECT ... FROM ORDERS O
WHERE
O.CUSTOMER_ID IN ( SELECT CUSTOMER_ID FROM CUSTOMERS WHERE REGION='America')
AND
O.SALES_REP_ID IN ( SELECT EMP_NO FROM EMP WHERE JOB_CODE='SL01')

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Stat Transformation: the Re-written Query
SELECT O.CUSTOMER_ID, CUST_NAME, SUM(O.ORDER_TOTAL)
FROM ORDERS O, CUSTOMERS C, EMP E
WHERE
O.CUSTOMER_ID=C.CUSTOMER_ID
AND O.SALES_REP_ID = E.EMP_NO
AND C.REGION='America'
AND E.JOB_CODE='SL01'
AND O.CUSTOMER_ID IN ( SELECT CUSTOMER_ID FROM CUSTOMERS
WHERE REGION='America')
AND
O.SALES_REP_ID IN ( SELECT EMP_NO FROM EMP
WHERE JOB_CODE='SL01')
GROUP BY O.CUSTOMER_ID, CUST_NAME;

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Stat Transformation: the Re-written Query using
Temporary Table Transformation
SYS_TEMP_x :
SELECT "C"."CUSTOMER_ID" "ITEM_1","C"."CUST_NAME" "ITEM_2“
FROM "CUSTOMERS" "C" WHERE "C"."REGION"='America'

SELECT O.CUSTOMER_ID, CUST_NAME, SUM(O.ORDER_TOTAL)


FROM ORDERS O, "SYS_TEMP_x" C, EMP E
WHERE
O.CUSTOMER_ID=C.CUSTOMER_ID
AND O.SALES_REP_ID = E.EMP_NO
AND C.REGION='America'
AND E.JOB_CODE='SL01'
AND O.CUSTOMER_ID IN ( SELECT CUSTOMER_ID FROM "SYS_TEMP_x")
AND
O.SALES_REP_ID IN ( SELECT EMP_NO FROM EMP
WHERE JOB_CODE='SL01')
GROUP BY O.CUSTOMER_ID, CUST_NAME;

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Star Transformation Execution Plan
| 0 | SELECT STATEMENT | |
| 1 | TEMP TABLE TRANSFORMATION | |
| 2 | LOAD AS SELECT (CURSOR DURATION MEMORY)| SYS_TEMP_0FD9D66E9_4B042B |
|* 3 | TABLE ACCESS FULL | CUSTOMERS |
| 4 | HASH GROUP BY | |
|* 5 | HASH JOIN | |
| 6 | VIEW | VW_ST_8DFE8582 |
| 7 | NESTED LOOPS | |
| 8 | BITMAP CONVERSION TO ROWIDS | |
| 9 | BITMAP AND | |
| 10 | BITMAP MERGE | |
| 11 | BITMAP KEY ITERATION | |
|* 12 | TABLE ACCESS FULL | EMP |
|* 13 | BITMAP INDEX RANGE SCAN | ORDERS_SALESR_BIX |
| 14 | BITMAP MERGE | |
| 15 | BITMAP KEY ITERATION | |
| 16 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |
|* 17 | BITMAP INDEX RANGE SCAN | ORDERS_CUST_BIX |
| 18 | TABLE ACCESS BY USER ROWID | ORDERS |
| 19 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Star Transformation Execution Plan
| 0 | SELECT STATEMENT | |
| 1 | TEMP TABLE TRANSFORMATION | |
| 2 | LOAD AS SELECT (CURSOR DURATION MEMORY)| SYS_TEMP_0FD9D66E9_4B042B |
|* 3 | TABLE ACCESS FULL | CUSTOMERS |
| 4 | HASH GROUP BY | |
|* 5 | HASH JOIN | |
| 6 | VIEW | VW_ST_8DFE8582 |
| 7 | NESTED LOOPS | |
| 8 | BITMAP CONVERSION TO ROWIDS | |
| 9 | BITMAP AND | |
| 10 | BITMAP MERGE | |
| 11 | BITMAP KEY ITERATION | |
|* 12 | TABLE ACCESS FULL | EMP |
|* 13 | BITMAP INDEX RANGE SCAN | ORDERS_SALESR_BIX |
| 14 | BITMAP MERGE | |
| 15 | BITMAP KEY ITERATION | |
| 16 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |
|* 17 | BITMAP INDEX RANGE SCAN | ORDERS_CUST_BIX |
| 18 | TABLE ACCESS BY USER ROWID | ORDERS |
| 19 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Star Transformation Execution Plan
| 0 | SELECT STATEMENT | |
| 1 | TEMP TABLE TRANSFORMATION | |
| 2 | LOAD AS SELECT (CURSOR DURATION MEMORY)| SYS_TEMP_0FD9D66E9_4B042B |
|* 3 | TABLE ACCESS FULL | CUSTOMERS |
| 4 | HASH GROUP BY | |
|* 5 | HASH JOIN | |
| 6 | VIEW | VW_ST_8DFE8582 |
| 7 | NESTED LOOPS | |
| 8 | BITMAP CONVERSION TO ROWIDS | |
| 9 | BITMAP AND | |
| 10 | BITMAP MERGE | |
| 11 | BITMAP KEY ITERATION | |
|* 12 | TABLE ACCESS FULL | EMP |
|* 13 | BITMAP INDEX RANGE SCAN | ORDERS_SALESR_BIX |
| 14 | BITMAP MERGE | |
| 15 | BITMAP KEY ITERATION | |
| 16 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |
|* 17 | BITMAP INDEX RANGE SCAN | ORDERS_CUST_BIX |
| 18 | TABLE ACCESS BY USER ROWID | ORDERS |
| 19 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Star Transformation Execution Plan
| 0 | SELECT STATEMENT | |
| 1 | TEMP TABLE TRANSFORMATION | |
| 2 | LOAD AS SELECT (CURSOR DURATION MEMORY)| SYS_TEMP_0FD9D66E9_4B042B |
|* 3 | TABLE ACCESS FULL | CUSTOMERS |
| 4 | HASH GROUP BY | |
|* 5 | HASH JOIN | |
| 6 | VIEW | VW_ST_8DFE8582 |
| 7 | NESTED LOOPS | |
| 8 | BITMAP CONVERSION TO ROWIDS | |
| 9 | BITMAP AND | |
| 10 | BITMAP MERGE | |
| 11 | BITMAP KEY ITERATION | |
|* 12 | TABLE ACCESS FULL | EMP |
|* 13 | BITMAP INDEX RANGE SCAN | ORDERS_SALESR_BIX |
| 14 | BITMAP MERGE | |
| 15 | BITMAP KEY ITERATION | |
| 16 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |
|* 17 | BITMAP INDEX RANGE SCAN | ORDERS_CUST_BIX |
| 18 | TABLE ACCESS BY USER ROWID | ORDERS |
| 19 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Star Transformation Execution Plan
| 0 | SELECT STATEMENT | |
| 1 | TEMP TABLE TRANSFORMATION | |
| 2 | LOAD AS SELECT (CURSOR DURATION MEMORY)| SYS_TEMP_0FD9D66E9_4B042B |
|* 3 | TABLE ACCESS FULL | CUSTOMERS |
| 4 | HASH GROUP BY | |
|* 5 | HASH JOIN | |
| 6 | VIEW | VW_ST_8DFE8582 |
| 7 | NESTED LOOPS | |
| 8 | BITMAP CONVERSION TO ROWIDS | |
| 9 | BITMAP AND | |
| 10 | BITMAP MERGE | |
| 11 | BITMAP KEY ITERATION | |
|* 12 | TABLE ACCESS FULL | EMP |
|* 13 | BITMAP INDEX RANGE SCAN | ORDERS_SALESR_BIX |
| 14 | BITMAP MERGE | |
| 15 | BITMAP KEY ITERATION | |
| 16 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |
|* 17 | BITMAP INDEX RANGE SCAN | ORDERS_CUST_BIX |
| 18 | TABLE ACCESS BY USER ROWID | ORDERS |
| 19 | TABLE ACCESS FULL | SYS_TEMP_0FD9D66E9_4B042B |

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


More about Star Transformation
• If you want to look into the optimizer actions in details:
ALTER SYSTEM SET EVENTS= '10053 trace name context forever, level 2';
SELECT ... ;
ALTER SYSTEM SET EVENTS = '10053 trace name context off';

• To instruct the optimizer to use star transformation:


SELCT /*+ STAR_TRANSFORMATION( O SUBQUERIES( (E) TEMP_TABLE(C))) */
...

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka


Summary
In this lecture, you should have learnt how to perform the following
• Describe and implement start transformation

Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka

You might also like