Professional Documents
Culture Documents
Time to Complete:
Approximately 60 minutes
Topics
This tutorial covers the following topics:
Overview
Scenario
Prerequisites
Connecting to the OLAPTRAIN Schema in SQL Developer
Executing a Simple OLAP Cube Query
Using Level and Member Conditions in a Query
Adding Calculated Measures to a Query
Leveraging Embedded Total Features of Cubes in a Query
Performing Drill-down in a Query
Using Parameterized Drilling
Using Cube MVs for Query Rewrite
More Information
Viewing Screenshots
Place the cursor over this icon to load and view all the screenshots for this tutorial. (Caution: Because this action loads all
screenshots simultaneously, response time may be slow depending on your Internet connection.)
Note: Alternatively, you can place the cursor over each individual icon in the following steps to load and view only the screenshot
associated with that step.
Overview
Oracle OLAP cube data is made directly accessible to SQL by a set of relational views. These views represent an OLAP cube as a star
schema with the following characteristics:
- A cube view plays the role of a fact table.
- Dimension views and hierarchy views play the role of dimension tables.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
1/30
4/14/13
The star design exposed by OLAP cubes is very similar to traditional table-based star models. The dimension views form a
constellation around one or more cube views. However, there are two key differences:
- Fact tables in a star schema store detail data (called leaves), while the cube views reveal many summary levels.
- Calculations in a cube are simply exposed as columns in the cube view, and the computation for the equations occurs
in the OLAP engine.
These differences impact the way you query data. With star queries, you aggregate the data by combining aggregation functions (such
as sum) and the GROUP BY clause. With OLAP queries, you simply select the data you want (either stored or calculated) as a column.
Typically, no aggregation function is necessary since the data has already been summarized by the cube.
For the vast majority of cube-based queries, there are four basic steps:
1.
2.
3.
4.
Scenario
The OLAP data for this tutorial was created using steps found in the Building OLAP11g Cubes tutorial. For information about the OLAP
model used in this tutorial, and for step-by-step instructions on how to create OLAP 11g cubes, click the link.
Back to Topic List
Prerequisites
Before you perform this tutorial, you should:
1.
Install Oracle Database 11g with the OLAP Option (Patch level 11.1.0.7 or higher).
2.
Create a desktop launcher for SQL Developer. (SQL Developer is shipped free with Oracle Database 11g.)
Then, download the following files to a location on the machine that contains SQL Developer:
cube_queries.sql
summary_queries.sql
Note: These files contain the completed olap cube and sql summary queries used in this tutorial.
3.
Have access to the OLAP data model which is part of the Oracle OLAP 11g Sample Schema package. You can
either:
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
2/30
4/14/13
2.
Select View > Connections to display the Connections tab in the navigator pane.
3.
In the Connections tab, right-click the Connections node, and select New Connection from the menu.
4.
In the New / Select Database Connection window, enter a Connection Name, Username and Password. Then, in
the Oracle tab, enter or select the following options:
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
3/30
4/14/13
Notes:
- Username is olaptrain.
- Password is the password that you created when you installed the OLAPTRAIN schema.
- Hostname is the host name of the server where Oracle Database is installed.
5.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
4/30
4/14/13
6.
Select olaptrain > Views to display the cube views for your OLAP data in the olaptrain schema.
Note: The cube views are automatically created and maintained by Oracle OLAP when you create cubes using
AWM 11g.
Next, you will use these views to directly query the OLAP data that you created in the Building OLAP 11g Cubes
tutorial (or, that you created by using the Sample Schema installation program).
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
5/30
4/14/13
3. Vertically resize the SQL statement pane so that you can view the first query. Then, at the top-right corner of the
Enter SQL Statement pane, select the olaptrain connection from the list.
Result: The queries in this .sql file may now be executed against the olaptrain schema.
Query Notes:
- Sales is simply selected as a column. There are no SQL aggregation functions applied.
- A level within the Product dimension hierarchy -- DEPARTMENT -- is used to filter product
members.
- All of the dimensions are qualified in the WHEREclause, even though only the Product dimension is
selected. In OLAP cube queries, dimensions that are not selected in the query require an '"ALL"
condition -- which specifies the top-level hierarchy value for each of the dimension columns -- in
order to leverage summaries that are already computed by the cube.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
6/30
4/14/13
Result: The query should return three rows, and results should look like this:
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
7/30
4/14/13
By using the "All" filters, the aggregation is performed in the OLAP engine. Data is returned almost instantaneously.
Back to Topic List
As shown in the Connections tab of the navigation pane, OLAP truncates column names at 24 characters. Therefore, it is helpful to view
the names of columns before using them in your queries.
The following query returns SALES for channels at the CLASS level, products at the DEPARTMENT level, QUARTERS in calendar year
2007, and ALL REGIONS.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
8/30
4/14/13
1.
In the Enter SQL Statement window, scroll down to view the second query:
Query Notes:
(A) Three of the four dimensions are selected -- Channel, Product, and Time.
(B) For each of the selected dimensions, the dimension hierarchy view is used, and the Long
Description column is selected in each case. (Notice that column names are truncated at 24
characters, exactly matching the display shown in the SQL Developer navigation pane.)
(C) Even though the Geography dimension is not in the SELECTstatement, the geography hierarchy
view is specified in the FROMclause. This technique enables you to specify the "ALL" condition for
the missing dimension using the dimension's hierarchy view, instead of the cube view.
(D) Since the Geography dimension is not in the query, an "ALL" condition is required to leverage
the cube aggregations over geography. This is specified in the WHEREclause using the following
Level condition: g.level_name = 'ALL_REGIONS'
(E) Level conditions are used to filter the data for the three dimensions in the query, using the
appropriate dimension hierarchy views, and the appropriate hierarchy level values:
c.level_name = 'CLASS'
p.level_name = 'DEPARTMENT'
t.level_name = 'CALENDAR_QUARTER'
(F) The filter on the Time dimension is further narrowed by applying a "Member" condition. This type
of condition selects a specific dimension member, rather than all members at a particular level.
The member condition is: t.calendar_year_long_descr = 'CY2007'
(E) For the time dimension, the combination of the level condtion (selecting all members at the
'QUARTER' level), and then the member condition (selecting -- from this subset -- all members
where the calendar year long description is 'CY2007') returns only those quarters in the calendar
year 2007.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
9/30
4/14/13
2.
Place the cursor somewhere in the midst of the query, as you did in the previous topic.
3.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
10/30
4/14/13
The OLAP calculations work perfectly across all summary levels, even when the aggregation rules are complex.
In this next query, three OLAP calculated measures are added to the previous query.
1. In the Enter SQL Statement window, scroll down to view the third query:
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
11/30
4/14/13
12/30
4/14/13
Note: For more information on how the calculated measures were created using AWM 11g, see Building OLAP11g Cubes.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
13/30
4/14/13
2. Place the cursor somewhere in the midst of the query and press F9.
Results: All of the calculations work perfectly and query performance is unaffected.
The query results should look like this:
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
14/30
4/14/13
Query Notes:
- In the previous query, the Geography dimension was not selected.
- In this query, Geography is included, and Channel is left out of the SELECTstatement. Therefore, an "ALL"
condition must be applied to the Channel dimension.
- A drill on the Geography dimension is executed by specifying a member in the PARENTcolumn of the geography
hierarchy view's: G.PARENT = "ALL_REGIONS". This condition returns the children of All Regions, which are the
geographic regional members.
- A drill on the Product dimension is executed by specifying a member in the PARENTcolumn of the product
hierarchy view's: P.PARENT = "ALL_PRODUCTS". This condition returns the children of All Products, which are
the product Department members.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
15/30
4/14/13
2. Place the cursor somewhere in the midst of the query and press F9.
The query results should look like this:
Scroll down to view the remaining results. Notice how the drills on Product and Geography select the appropriate hierarchial
children, and all of the calculations work perfectly.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
16/30
4/14/13
Query Notes:
- In this query, Geography is removed from the query, and Channel is included. Therefore, an "ALL" condition is
applied to the Geography dimension.
- Drills on the Channel and Product dimensions are executed in the same way as the previous query.
- In addition, a parameterized drill is performed on the Time dimension using the hierarchy view's PARENT
column. In addition, the nvlfunction is used so that if no value is provided, the "ALL_YEARS" member is
automatically used as the parent value.
2. Place the cursor somewhere in the midst of the query and press F9.
Result: the Enter Bind Values window appears
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
17/30
4/14/13
Notes:
- The query returns data for each year in the Time dimension -- these are the children of ALL_YEARS in the
Calendar Year hierarchy..
- Also notice that for CY2005, there are no data values for the YTD calculations. This is correct, because 2005 is
the first year in the data model (there is no 2004).
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
18/30
4/14/13
4. Once again, place the cursor somewhere in the midst of the query and press F9. Then, in the Enter Bind Values window, enter
CY2007 in the Value box, as shown here:
5. Click Apply.
The query results should look like this:
The data is displayed for the quarters of 2007 (the children of CY2007).
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
19/30
4/14/13
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
20/30
4/14/13
2. Select File > Open, and navigate to the folder where you download the SQL query files. Then, open summary_queries.sql.
At the top-right corner of the Enter SQL Statement pane, select the olaptrain connection from the list, then vertically resize the SQL
statement pane so that it takes up at least half of the space in the SQL Developer window, as shown here:
3. To ensure that Query Rewrite to the Cube MV is turned off, click the ALTER materialized VIEW cb$sales_cube disable query
rewritestatement, and then press F9.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
21/30
4/14/13
This query returns Quantity and Sales by Year and product Category.
5. Place your cursor in the midst of the query, and press F6 to display the Explain Plan for the query, as shown here:
The Explain Plan shows that the query joins the fact table to the dimension tables specified in the query, and then performs a full table
scan of the fact table in order to return data the requested data.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
22/30
4/14/13
6. With the cursor still in the midst of the query, and press F9 to execute the query.
The query results should look like this:
Make a note of the query performance, which is shown value appears at the top of the SQLStatement pane, as shown above. In this
example, the query took approximately 3.05 seconds to run. The performance that you observe will depend on a number of factors
related to the configuration of your database server.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
23/30
4/14/13
7. Click inside the second summary query, which is designed to return Sales by Year, Department, Class and Country.
a. Press F6 to show the Explain Plan. As is the case with all the summary queries, a full table scan of the fact table will
be performed in order to return data the requested data.
b. Press F9 to execute the query, as shown here. Again, record your query time.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
24/30
4/14/13
8. Using the same techniques shown above, execute the third and fourth queries. Record each of the query times.
In our example:
Summary query number 3 performed like this:
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
25/30
4/14/13
9. Scroll back up in the SQL file and enable query rewrite for the session, and to the OLAP cube MV, by performing the following:
a. Click the ALTER SESSION SET query_rewrite_integrity=stale_toleratedstatement, and then press F9.
b. Click the ALTER SESSION SET query_rewrite_enabled = forcestatement, and then press F9.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
26/30
4/14/13
c. Click the ALTER materialized VIEW cb$sales_cube enable query rewritestatement, and then press F9.
10. To confirm that the queries will rewrite, click within the first query and press F6 to display the Explain Plan, as shown in the following
screenshot:
27/30
4/14/13
11. Press F9 to execute the query, and make note of the improved performance.
12 Scroll down and click inside the second summary query. Press F6 to confirm the rewrite, and then press F9 to execute the query.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
28/30
4/14/13
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
29/30
4/14/13
13. Execute the remaining queries. Record each of the query times.
Note: Query times for a Cube MV rewrite commonly are 10 to 50 times faster than summary queries against relational fact tables.
14. When you are done working with the query files, close them and exit SQL Developer.
More Information
For more information on OLAP-related education, use the following links:
Oracle Database 11g: OLAP Essentials (Oracle University inClass)
Building OLAP11g Cubes (OBE)
Using Oracle OLAP 11g With Oracle BI Enterprise Edition (OBE)
Creating Interactive APEX Reports Over OLAP 11g Cubes (OBE)
Back to Topic List
Place the cursor over this icon to hide all screenshots.
st-curriculum.oracle.com/obe/db/11g/r1/olap/cube/querycubes.htm
30/30