24
10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10 Copyright © 2005, Oracle. All rights reserved. Dimensions

Embed Size (px)

Citation preview

Page 1: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10Copyright © 2005, Oracle. All rights reserved.

Dimensions

Page 2: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-2 Copyright © 2005, Oracle. All rights reserved.

Objectives

After completing this lesson, you should be able to do the following:

• Create dimensions

• Validate dimension data

• View dimension definition information

Page 3: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-3 Copyright © 2005, Oracle. All rights reserved.

What Are Dimensions?

• Data dictionary structures that define hierarchies between pairs of column sets

• Used with materialized views for query rewrite

• Superset of referential constraints:– Normalized dimensions: Each child-side row joins

with one and only one parent-side row.– Denormalized dimensions: Child-side columns

uniquely determine the parent-side columns.

• Never enforced, but can be validated

• Enable additional query rewrites without the use of constraints

Page 4: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-4 Copyright © 2005, Oracle. All rights reserved.

Dimensions and Hierarchies

MONTH_NAME

WeekSeason

ALL

Level key

Additional attribute

Hierarchy

CALENDAR_ROLLUP

MONTH

Level key attribute

Year

Quarter

Salesdate

Month

TIME_DIM dimension

Page 5: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-6 Copyright © 2005, Oracle. All rights reserved.

Dimension Table: Example

SQL> SELECT * FROM time;

TIME_ID MONTH MONTH_NAM QUARTER YEAR

--------- ------ ---------- ------- --------

01-JAN-02 M1_02 January02 Q1_02 2002

02-JAN-02 M1_02 January02 Q1_02 2002

03-JAN-02 M1_02 January02 Q1_02 2002

04-MAR-02 M2_02 March02 Q1_02 2002

...

30-DEC-02 M12_02 December02 Q4_02 2002

31-DEC-02 M12_02 December02 Q4_02 2002

...

Table used in examples in the lesson:

Page 6: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-7 Copyright © 2005, Oracle. All rights reserved.

Benefits of Dimensions

SELECT year, sum(amount_sold) AS sumamtFROM time t, sales sWHERE t.time_id=s.time_id GROUP BY year;

SELECT year, sum(sumamt)FROM sumsales mv,(SELECT DISTINCT month, year FROM times) v WHERE v.month = mv.monthGROUP BY year;

CREATE MATERIALIZED VIEW sumsales ASSELECT month, sum(amount_sold) AS sumamtFROM time t, sales sWHERE t.time_id = s.time_id GROUP BY month;

Materialized view created

User executes query

Query rewritten

Page 7: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-8 Copyright © 2005, Oracle. All rights reserved.

Defining Dimensions and Hierarchies

CREATE DIMENSION time_dim

LEVEL sdate IS time.sdate

LEVEL month IS time.month

LEVEL qtr IS time.quarter

LEVEL yr IS time.year

HIERARCHY calendar_rollup (

sdate CHILD OF

month CHILD OF

qtr CHILD OF yr )

ATTRIBUTE month

DETERMINES time.month_name;

yr

qtr

month

sdate

Page 8: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-9 Copyright © 2005, Oracle. All rights reserved.

Specifying the SKIP WHEN NULL Clause

CREATE DIMENSION SALE_DATA_DIM LEVEL CUSTOMER IS SALE_DATA.CUST_ID LEVEL CITY IS SALE_DATA.CITY LEVEL STATE IS SALE_DATA.STATE SKIP WHEN NULLLEVEL COUNTRY IS SALE_DATA.COUNTRY HIERARCHY GEOG_ROLLUP (CUSTOMER CHILD OF CITY CHILD OF STATE CHILD OF COUNTRY)

CUST_ID

CITY

STATE

COUNTRY

Washington, D.C.

USA

20145

No state

Page 9: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-11 Copyright © 2005, Oracle. All rights reserved.

Creating Dimensions UsingEnterprise Manager

create_dimension.gif

Page 10: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-12 Copyright © 2005, Oracle. All rights reserved.

Dimensions Based on Multiple Tables

CREATE DIMENSION customers_dim LEVEL customer IS customers.cust_id LEVEL city IS customers.cust_city LEVEL state IS customers.cust_state_province LEVEL country IS countries.country_id LEVEL subregion IS countries.country_subregion LEVEL region IS countries.country_regionHIERARCHY geo_rollup (customer CHILD OF city CHILD OF state CHILD OFcountry CHILD OF subregion CHILD OF regionJOIN KEY (customers.country_id) REFERENCES country)ATTRIBUTE customer DETERMINES(cust_first_name, cust_last_name)ATTRIBUTE country DETERMINES countries.country_name;

Page 11: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-13 Copyright © 2005, Oracle. All rights reserved.

Dimensions with Multiple Hierarchies

CAL hierarchy

WEEK hierarchyYEAR

QUARTER

MONTH

DAY

YEAR

WEEK

DAY

Page 12: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-14 Copyright © 2005, Oracle. All rights reserved.

Dimensions and Privileges

• CREATE DIMENSION• CREATE ANY DIMENSION• SELECT object privilege on each referenced object

• ALTER ANY DIMENSION• DROP ANY DIMENSION

Page 13: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-15 Copyright © 2005, Oracle. All rights reserved.

Dimension Restrictions

• Two levels cannot have the same column set.

• Columns of a hierarchy level cannot be associated with more than one dimension.

• A hierarchy level cannot be a child of itself.

• All level key attributes for one level must belong to the same table.

• The JOIN clause is required if columns of different levels come from different tables.

• All additional attributes for one level key must belong to the same table.

• It is not possible to create dimensions based on views.

Page 14: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-16 Copyright © 2005, Oracle. All rights reserved.

Using VALIDATE_DIMENSION to Verify Relationships in a Dimension

DBMS_DIMENSION.VALIDATE_DIMENSION('SH.TIMES_DIM',TRUE,TRUE,'ver times_dim');

Check that levels are non-null

Check only new rows in the tables

Dimension owner and name

User-supplied identifier

Page 15: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-17 Copyright © 2005, Oracle. All rights reserved.

Verifying Relationships in a Dimension

DBMS_DIMENSION.VALIDATE_DIMENSION('SH.CHANNELS_DIM', false, true, 'ver chan_dim');

SELECT * FROM sh.channelsWHERE rowid IN (SELECT bad_rowid FROM dimension_exceptions WHERE statement_id = 'ver chan_dim');

CHANNEL_ID CHANNEL_DESC CHANNEL_CLASS---------- -------------------- --------------------S Direct Sales DirectS Security Direct

@utldim.sql

Page 16: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-18 Copyright © 2005, Oracle. All rights reserved.

Viewing Dimensions UsingEnterprise Manager

view_dimension.gif

Page 17: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-19 Copyright © 2005, Oracle. All rights reserved.

Viewing Dimensions in the Data Dictionary

• DBA_DIMENSIONS• DBA_DIM_LEVELS• DBA_DIM_LEVEL_KEY• DBA_DIM_ATTRIBUTES • DBA_DIM_HIERARCHIES• DBA_DIM_CHILD_OF• DBA_DIM_JOIN_KEY

Page 18: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-20 Copyright © 2005, Oracle. All rights reserved.

Viewing the Definition of a Dimension

SQL> SET SERVEROUTPUT ON FORMAT WRAPPEDSQL> EXECUTE DBMS_DIMENSION.DESCRIBE_DIMENSION ('sh.channels_dim'); DIMENSION SH.CHANNELS_DIM LEVEL CHANNEL IS SH.CHANNELS.CHANNEL_ID LEVEL CHANNEL_CLASS IS SH.CHANNELS.CHANNEL_CLASS_ID LEVEL CHANNEL_TOTAL IS SH.CHANNELS.CHANNEL_TOTAL_ID HIERARCHY CHANNEL_ROLLUP ( CHANNEL CHILD OF CHANNEL_CLASS CHILD OF CHANNEL_TOTAL ) ATTRIBUTE CHANNEL LEVEL CHANNEL DETERMINES SH.CHANNELS.CHANNEL_DESC ATTRIBUTE CHANNEL_CLASS LEVEL CHANNEL_CLASS DETERMINES SH.CHANNELS.CHANNEL_CLASS ATTRIBUTE CHANNEL_TOTAL LEVEL CHANNEL_TOTAL DETERMINES SH.CHANNELS.CHANNEL_TOTAL

Page 19: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-21 Copyright © 2005, Oracle. All rights reserved.

Dimension Invalidation

• Invalidation is done automatically when related objects are altered.

• Verify a dimension’s status through Enterprise Manager or the INVALID column of DBA_DIMENSIONS.

• Use the ALTER DIMENSION … COMPILE command to revalidate a dimension.

Page 20: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-22 Copyright © 2005, Oracle. All rights reserved.

• Drop a dimension as follows:

• Does not invalidate materialized views that use relationships specified in the dimension

Dropping Dimensions

DROP DIMENSION channels_dim;

Page 21: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-23 Copyright © 2005, Oracle. All rights reserved.

• Create the materialized view using the attribute LEVEL clause:

• Selectively drop a column:

Selectively Dropping Dimension Components

ALTER DIMENSION product_dimDROP ATTRIBUTE att LEVEL prod_id COLUMN prod_status;

CREATE DIMENSION product_dimLEVEL prod_id IS (products.prod_id)ATTRIBUTE att LEVEL prod_id DETERMINES prod_name, prod_status;

Page 22: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-24 Copyright © 2005, Oracle. All rights reserved.

Constraints or Dimensions?

• For normalized dimensions, use:– PK/NOT NULL FK relationships as long as possible,

or– Dimensions

• For denormalized dimensions, use dimensions.

• Joins between dimension and fact tables should be materialized by:– PK/NOT NULL FK relationships: NOVALIDATE RELY

flags if validation is of concern, and/or – Dimensions, or– Joins-only materialized views defined with outer

joins

• Always make sure dimensions are valid if used.

Page 23: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-26 Copyright © 2005, Oracle. All rights reserved.

Summary

In this lesson, you should have learned how to:

• Distinguish between dimensions and constraints

• Create dimensions

• Validate dimension data

• View dimension definitions

Page 24: 10 Copyright © 2005, Oracle. All rights reserved. Dimensions

10-27 Copyright © 2005, Oracle. All rights reserved.

Practice 10: Overview

This practice covers the following topics:

• Creating dimensions on multiple tables

• Validating dimensions