Using SQL to Query Oracle OLAP Cubes Bud Endress Director of Product Management, OLAP.
-
Upload
darrell-shepherd -
Category
Documents
-
view
225 -
download
1
Transcript of Using SQL to Query Oracle OLAP Cubes Bud Endress Director of Product Management, OLAP.
Using SQL to Query Oracle OLAP Cubes
Bud EndressDirector of Product Management, OLAP
Powerful OLAP for SQL Applications
• Power your SQL based applications with Oracle OLAP performance and analytic content• OLAP cubes provide fast refresh, fast query and rich analytic
content• SQL query interface allows any application query cubes and
dimensions without having to learn and use OLAP languages
Powerful OLAP for SQL Applications
• Example: Oracle Application Express• A general purpose database application development tool• Queries OLAP cubes using SQL• Has no specific knowledge of OLAP• Reporting via tables and graphs• OLAP content in Application Express
• Time Series• Rankings• Product share• Interactive drilling to children in a hierarchy• Etc.
OLAP Cube Views
• The OLAP Option automatically creates a collection of views that allow SQL applications to easily query cubes and dimensions
• OLAP cube and dimension views are similar to a star schema• Cube views play the role of fact tables• Dimension and hierarchy views play the role of dimension
tables
Finding Cube and Dimension Views
• Finding cube and dimension views• Naming conventions
• Fact views: cube_VIEW• Dimension views: dimension_VIEW• Hierarchy views: dimension_hierarchy_VIEW
• Dictionary queries• user_cube_views• user_cube_dimension_views• user_cube_hierarchy_views
Finding Cube Views
SELECT view_name
FROM user_views
WHERE view_name LIKE 'UNITS_CUBE%';
VIEW_NAME
------------------------------
UNITS_CUBE_VIEW Cube fact view
Cube Fact View
DESC units_cube_view;
Name Null Type ------------------------- -------- -------------- TIME VARCHAR2(100) CHANNEL VARCHAR2(100) CUSTOMER VARCHAR2(100) PRODUCT VARCHAR2(100)UNITS NUMBER SALES NUMBER COST NUMBERSALES_PRIOR_PERIOD NUMBERSALES_DIFF_PRIOR_PERIOD NUMBERSALES_PCT_DIFF_PRIOR_PERIOD NUMBER
Keys (by default, named afterdimensions)
Facts
Finding Dimension and Hierarchy Views
SELECT view_name
FROM user_views
WHERE view_name LIKE 'CUSTOMER%'
VIEW_NAME
------------------------------
CUSTOMER_VIEW Dimension view Customer dimension
CUSTOMER_SEGMENT_VIEW Hierarchy view for Segment hierarchy
CUSTOMER_SHIPMENTS_VIEW Hierarchy view for Shipments hierarchy
Cube Fact View
• The cube fact view returns all data of the cube• All facts (measures) of the cube, both stored and calculated• All rows, both detailed and summary
• All the calculation rules are embedded in the cube• Calculation rules do not need to be expressed in SQL query
of the cube
Cube Fact View
DESC units_cube_view;
Name Null Type ------------------------- -------- -------------- TIME VARCHAR2(100) CHANNEL VARCHAR2(100) CUSTOMER VARCHAR2(100) PRODUCT VARCHAR2(100)UNITS NUMBER SALES NUMBER COST NUMBERSALES_PRIOR_PERIOD NUMBERSALES_DIFF_PRIOR_PERIOD NUMBERSALES_PCT_DIFF_PRIOR_PERIOD NUMBER
Keys
Stored facts
Calculated facts
Cube Fact ViewSELECT time, product, customer, channel, unitsFROM units_cube_viewWHERE rownum < 15;
TIME PRODUCT CUSTOMER CHANNEL UNITS--------- ------- -------- ------- ----------TOTAL TOTAL TOTAL TOTAL 4000968CY1999 TOTAL TOTAL TOTAL 330425CY2003 TOTAL TOTAL TOTAL 534069CY1998 TOTAL TOTAL TOTAL 253816CY2005 TOTAL TOTAL TOTAL 565718CY2006 TOTAL TOTAL TOTAL 584929CY2004 TOTAL TOTAL TOTAL 587419CY2000 TOTAL TOTAL TOTAL 364233CY2002 TOTAL TOTAL TOTAL 364965CY2001 TOTAL TOTAL TOTAL 415394CY2000.Q1 TOTAL TOTAL TOTAL 88484CY2001.Q2 TOTAL TOTAL TOTAL 97346CY2001.Q3 TOTAL TOTAL TOTAL 105704CY2005.Q3 TOTAL TOTAL TOTAL 138953
Calendar Year aggregates
Calendar Quarter aggregates
Totals for Product, Customerand Channel dimensions
Dimension Views
• Dimension views contain• Rows for all keys of a dimension
• A union of all hierarchies• Detail and aggregate level keys
• Columns for• The primary key• Attributes of the primary key. E.g,
• Short and Long Description• The level of the key• User defined attributes (color, size, etc.)
Dimension Views
desc customer_view;
Name Null Type ------------------- -------- --------------DIM_KEY VARCHAR2(100) Primary KeyLEVEL_NAME VARCHAR2(30) LONG_DESCRIPTION VARCHAR2(100) AttributesSHORT_DESCRIPTION VARCHAR2(100)
Dimension Views
SELECT * FROM customer_viewWHERE rownum < 15;
DIM_KEY LEVEL_NAME LONG_DESCRIPTION SHORT_DESCRIPTION--------------------------- ----------------- -----------------TOTAL TOTAL_CUSTOMER All Customers All Customers EMEA REGION Europe Europe AMER REGION North America North AmericaAPAC REGION Asia Pacific Asia PacificJPN WAREHOUSE Japan JapanCAN WAREHOUSE Canada CanadaITA WAREHOUSE Italy ItalyUK WAREHOUSE United Kingdom United KingdomAUS WAREHOUSE Australia AustraliaSPA WAREHOUSE Spain SpainFRA WAREHOUSE France FranceUS WAREHOUSE United States United StatesSIN WAREHOUSE Singapore SingaporeGER WAREHOUSE Germany Germany
Detail and aggregate keys
Level Attributes
Hierarchy Views
• Hierarchy views contain• Rows for all keys of a hierarchy
• Detail and aggregate level keys• Columns for
• The primary key• Hierarchical attributes
• The parent of the key• The level of the key• Ancestors of the key
• User defined attributes (color, size, etc.)
Hierarchy Views
desc time_calendar_view;
Name Null Type ------------------ -------- --------------DIM_KEY VARCHAR2(100) Primary KeyPARENT VARCHAR2(100) Parent of primary keyMONTH VARCHAR2(100) CALENDAR_QUARTE VARCHAR2(100) Ancestors CALENDAR_YEAR VARCHAR2(100) ALL_TIMES VARCHAR2(100) LEVEL_NAME VARCHAR2(30) LONG_DESCRIPTION VARCHAR2(100) SHORT_DESCRIPTION VARCHAR2(100)END_DATE DATE AttributesTIME_SPAN NUMBERDIM_ORDER NUMBERHIER_ORDER NUMBER
Hierarchy Views
SELECT dim_key, level_name, parent, month, calendar_quarter, calendar_yearFROM time_calendar_viewWHERE calendar_year = 'CY2007';
Hierarchy Views
DIM_KEY LEVEL_NAME PARENT MONTH CALENDAR_QUARTER CALENDAR_YEAR----------------------------- ---------- -------- ---------------- -------------CY2007 CALENDAR_YEAR 9999 CY2007 CY2007.Q3 CALENDAR_QUARTER CY2007 CY2007.Q3 CY2007 CY2007.Q2 CALENDAR_QUARTER CY2007 CY2007.Q2 CY2007 CY2007.Q1 CALENDAR_QUARTER CY2007 CY2007.Q1 CY2007 CY2007.Q4 CALENDAR_QUARTER CY2007 CY2007.Q4 CY2007 2007.03 MONTH CY2007.Q1 2007.03 CY2007.Q1 CY2007 2007.01 MONTH CY2007.Q1 2007.01 CY2007.Q1 CY2007 2007.05 MONTH CY2007.Q2 2007.05 CY2007.Q2 CY2007 2007.06 MONTH CY2007.Q2 2007.06 CY2007.Q2 CY2007 2007.08 MONTH CY2007.Q3 2007.08 CY2007.Q3 CY2007 2007.10 MONTH CY2007.Q4 2007.10 CY2007.Q4 CY2007 2007.09 MONTH CY2007.Q3 2007.09 CY2007.Q3 CY2007 2007.12 MONTH CY2007.Q4 2007.12 CY2007.Q4 CY2007 2007.02 MONTH CY2007.Q1 2007.02 CY2007.Q1 CY2007 2007.07 MONTH CY2007.Q3 2007.07 CY2007.Q3 CY2007 2007.04 MONTH CY2007.Q2 2007.04 CY2007.Q2 CY2007 2007.11 MONTH CY2007.Q4 2007.11 CY2007.Q4 CY2007
Level AncestorsParentDetail andaggregate
keys
Querying the Cube Fact View
• Query the cube view fact like a fact table of a star schema:• Cube fact views are typically joined to dimension or hierarchy
views• But, GROUP BY is typically not needed because the cube
fact view returns aggregate data computed in the cube
Querying the Cube Fact View
• A typical query of the cube fact view
SELECT t.long_description time, p.long_description product, cu.long_description customer, ch.long_description channel, f.salesFROM time_view t, product_view p, customer_view cu, channel_view ch, units_cube_view fWHERE t.level_name = 'CALENDAR_YEAR' AND p.level_name = 'TOTAL_PRODUCT' AND cu.level_name = 'TOTAL_CUSTOMER' AND ch.level_name = 'TOTAL_CHANNEL' AND t.dim_key = f.TIME AND p.dim_key = f.product AND cu.dim_key = f.customer AND ch.dim_key = f.channel;
SELECT list (like a star query)
FROM clause (like a star query)
Level filters (instead of GROUP BY)
Joins (like a star query)
Querying the Cube Fact View
• Guidelines for successful queries• Apply a filter to each dimension
• Filters should be compatible with the cube• Allow the cube to aggregate the data
SQL Processing
APPLICATION
SQL ‘ENGINE’
SELECT Statement
Data
Returns data through OCI or JDBC
CUBE
Filter Translation
Filters, cube joins, aggregation and calculation
CUBE_TABLE
SELECT Statement Data (as rows)
SQL functions, filters and joins to tables
Let the cube do as muchwork as possible
SQL will finish the query as needed
Return the minimum number of rows as required
by the query
Dimension Filters
• Typically, there should be a filter on every dimension when querying the cube fact view• Remember, the cube fact view contains both detail and
aggregate level data• The cube fact view can return very large numbers of rows• If a filter is not applied to a dimension, rows for all keys –
detailed and summary – are returned by the query
Dimension Filter Examples
SELECT t.long_description TIME, p.long_description product, cu.long_description customer, ch.long_description channel, sales, sales_pct_diff_prior_periFROM time_view t, product_view p, customer_view cu, channel_view ch, units_cube_view fWHERE t.long_description in ('2005','2006') AND p.buyer = 'MONTURIO' AND cu.level_name = 'TOTAL_CUSTOMER' AND ch.level_name = 'TOTAL_CHANNEL' AND t.dim_key = f.TIME AND p.dim_key = f.product AND cu.dim_key = f.customer AND ch.dim_key = f.channel;
Level filters
Filters using attributes
Dimension Filters
Example of why dimension filters they are important:
•In this small example, the Units Cube is dimensioned by•Time (222 values)•Product (48 values)•Customer (106 values)•Channel (4 values)
•Units Cube can potentially return 4,518,144 rows (222 * 48 * 106 * 4)
•Every filter dramatically reduces the number of rows returned by the cube
WHERE t.long_description in ('2005','2006') 20,352 rows remaining after this filter AND p.buyer = 'MONTURIO' 8,480 rows remaining after this filter AND cu.level_name = 'TOTAL_CUSTOMER' 80 rows remaining after this filter AND ch.level_name = 'TOTAL_CHANNEL‘ 20 rows remaining after this filter
Aggregation
• The cube will aggregate data for all dimension members• There is no need for GROUP BY on keys
Examples of Dimensional Queries
• Leveraging elements of the dimensional model for multidimensional queries• Level based queries• Drilling• Ancestor and descendant based queries
Level Based Queries
SELECT t.long_description TIME, p.long_description product, cu.long_description customer, ch.long_description channel, round(f.sales,0) salesFROM time_view t, product_view p, customer_view cu, channel_view ch, units_cube_view fWHERE t.level_name = 'CALENDAR_YEAR' AND p.level_name = 'TOTAL_PRODUCT' AND cu.level_name = 'TOTAL_CUSTOMER' AND ch.level_name = 'TOTAL_CHANNEL' AND t.dim_key = f.TIME AND p.dim_key = f.product AND cu.dim_key = f.customer AND ch.dim_key = f.channelORDER BY time;
Drilling Down
SELECT t.long_description TIME, p.long_description product, cu.long_description customer, ch.long_description channel, f.salesFROM time_calendar_view t, product_view p, customer_view cu, channel_view ch, units_cube_view fWHERE p.level_name = 'TOTAL_PRODUCT' AND cu.level_name = 'TOTAL_CUSTOMER' AND ch.level_name = 'TOTAL_CHANNEL' AND t.parent = 'CY2006' AND t.dim_key = f.TIME AND p.dim_key = f.product AND cu.dim_key = f.customer AND ch.dim_key = f.channelORDER BY time;
Drilling Up
SELECT t.long_description TIME, p.long_description product, cu.long_description customer, ch.long_description channel, ROUND(f.sales, 0) salesFROM time_calendar_view_2 t, product_view p, customer_view cu, channel_view ch, units_cube_view fWHERE t.dim_key = (SELECT DISTINCT calendar_year FROM time_calendar_view WHERE MONTH = '2006.01')AND p.level_name = 'TOTAL_PRODUCT' AND cu.level_name = 'TOTAL_CUSTOMER' AND ch.level_name = 'TOTAL_CHANNEL' AND t.dim_key = f.TIME AND p.dim_key = f.product AND cu.dim_key = f.customer AND ch.dim_key = f.channel;
Descendants Of Member
SELECT t.long_description TIME, p.long_description product, cu.long_description customer, ch.long_description channel, ROUND(f.sales, 0) salesFROM time_calendar_view_2 t, product_view p, customer_view cu, channel_view ch, units_cube_view fWHERE t.level_name = 'MONTH' AND t.calendar_year = 'CY2006' AND p.level_name = 'TOTAL_PRODUCT' AND cu.level_name = 'TOTAL_CUSTOMER' AND ch.level_name = 'TOTAL_CHANNEL' AND t.dim_key = f.TIME AND p.dim_key = f.product AND cu.dim_key = f.customer AND ch.dim_key = f.channel;
Explain Plan Examples
Explain Plans for Cubes
Oracle OLAPin Oracle Database 11g
• Improves business intelligence applications• Optimized for fast refresh and ad-hoc query
• Database-managed summary management• Embedded BI calculations and metadata
• Accessible by any application• SQL or OLAP API based
For More Information
http://search.oracle.com
or
oracle.com/database
oracle olap