2. OLTP/OLAP Comparison
OLAP: On-Line
Analytic Processing
OLTP: On-Line
Transaction Processing
OLTP Query for A Single
Sales Order in an SAP DB
OLAP Query for revenue
by Geography / Product-
Line in an IBM Cognos DB
3. OLTP/OLAP Comparison
OLTP
• Simple, frequent queries
• Up-to-the-minute information
• Fast response time needed
OLAP
• Few queries, but very complex
• Lots of indexes
• May run for hours
• Doesn’t need up-to-the-minute
info
5. OLAP Queries
• We have a long-running transaction
analyzing all sales data.
• What about the new sales data
coming into the database while our
transaction is running?
• Talk to your neighbor. Think in
terms of serializable transactions
and locking mechanisms…
• Recall, Reads get an S-Lock.
Writers forbidden from
getting their X-Lock
StalledTransactions!
Animation
7. The DataWarehouse Congregates
Multiple Data Sources
CRM
SCP
ERP
DataWarehouse
Supply Chain
(Vendor) Data
Factory
Planning Data
Sales/
Marketing Data
OLAP Queries
What is our worst-selling vehicle?
Do we need another factory to meet demand?
Should we focus sales on Europe or Japan?
8. Summarizing the
Motivation for OLAP
• We want to add lots of Indexes for faster search, and perform
long-running transactions, but this slows down the DB
• We only need to look at the long term trends, not up-to-the-
second data
• We may want to aggregate data from multiple DBs
• Answer: Move the data to a DataWarehouse for targeted
analysis. Use OLAP techniques which we learn today to slice
& dice the massive aggregate into discrete actionable data.
9. Auto Sales Example
• Say we want to record in a data warehouse all auto sales
• We would create aTable with key info for the sale
• Serial Number, Dealer Name, Date of Sale, Price of Sale
• The relation becomes our FactTable:
• sales(auto, dealer, date, price)
10. Visualizing Multidimensional
Data
• FactTable: a central
relation or collection
of data
• E.g. all information
about a sale of an
automobile
• Dimensions:
Represent the
properties of the sale
Dealer
Days
Auto
11. Dealer
Auto
Dicing the Data
• The FactTable is ‘diced’
(divided) by its dimensions
- the DimensionTables
Date
Boring Barney’s
WittyWilma’s
Friendly Fred’s
Gobi
Aardvark
200120022003200420052006
13. Working within a Star Schema
• A Star Schema example can be found in my personal
‘sfabini’ CAT database
• Connect by issuing the following command:
Please do not alter the database
16. Dealer
Auto
Dicing the Data
• Recapping, does everyone see
the ‘dimensions’ and how
they divide the FactTable?
• Date divided along x axis
• Different dealers on y axis
• Different autos on z axis
• Within the cube (fact table) are
the dots -- individual auto sales,
including how much it sold for
(price)
Date
Boring Barney’s
WittyWilma’s
Friendly Fred’s
Gobi
Aardvark
200120022003200420052006
17. Dealer
Auto
Exercise: Slicing the Data
• Find Average Sales Price
by State
SELECT state, AVG(price)
FROM sales, dealers
WHERE sales.dealer =
dealer.name
GROUP BY state;
Date
Boring Barney’s
WittyWilma’s
Arizona
New
M
exico
Friendly Fred’s
Try it yourself!
Which state’s dealers maximize ASP
(average selling price)?
18. Dealer
Auto
Exercise: Drilling Down
• FindTotal Sales Price for Gobis at
Friendly Fred’s
SELECT color, SUM(price)
FROM sales,
NATURAL JOIN autos
JOIN dealer ON name = sales.dealer
WHERE sales.dealer = ‘Friendly Fred’
AND
Model = ‘Gobi’
GROUP BY color;
Date
Boring Barney’s
WittyWilma’s
Arizona
New
M
exico
Friendly Fred’s
Try it yourself!
Joining tables in your query, and then
Grouping ‘drills down’ the table
19. Rolling Up and Drilling Down
• Drill-Down: Focusing on specific values in certain
dimensions
• looking at the sales of red Gobis at Friendly Fred’s
• Roll-Up: Aggregate along one or more dimensions
• Looking at all colors as groups to find the best selling color overall
20. Foundations of the CUBE
Operator
Dealer
Date
Auto
Sum of price of
all autos, by a
specific dealer,
on all dates
Sum of price of a
specific auto, by a
specific dealer, on
all datesPrice of a specific
auto, by a specific
dealer, on a
specific date
The Cube
operator adds a
border of
aggregations to
the data cube
across all
combinations of
dimensions.
Sum of price of all
autos, by all
dealers, on all
dates
21. CUBE vs. ROLLUP Operators
• WITH CUBE: If we add the termWITH CUBE to a group-by
clause, then we get not only the tuples for the group , but
also the tuples that represent aggregation along one or
more dimensions along which we have grouped
• WITH ROLLUP: A variant of the CUBE operator that
produces the additional aggregated tuples over the tail of
the sequence of grouping attributes
22. Working with a Cube and
phpMyAdmin
• We need to use MySQL to demonstrate Cube/Rollup
• A Cube example can be found in my personal ‘sfabini’ CAT
database
• Connect via website: cat.pdx.edu/phpMyAdmin/
23. Working with a Cube
• We need to use MySQL to demonstrate Cube/Rollup
• A Cube example can be found in my personal ‘sfabini’ CAT
database
• Connect by issuing the following command:
24. ROLLUP Query in MySQL
• SELECT model, color, DATE, dealer, SUM( val ) ,
SUM( cnt )
FROM SalesCube
GROUP BY model, color, DATE, dealer
WITH ROLLUP
25. Results: Auto SalesWITH ROLLUP
in MySQL
Aggregate of blue Gobis sold
Aggregate of red Gobis sold
Aggregate of all Gobis sold
Aggregate of all cars sold
Aggregate of red Gobis sold by any dealer on this date
26. CUBE Query in SQL99
• SELECT model, color, DATE, dealer, SUM( val ) ,
SUM( cnt )
FROM SalesCube
GROUP BY model, color, DATE, dealer
WITH CUBE
27. • CUBE Query Result (Partial):
• Rollup Query Result:
Comparison:WITH ROLLUP vs.
WITH CUBE results
All colors of Gobis sold at Friendly Fred’s on 5/21
Aggregate of all Gobis sold
Aggregate of all autos sold
Aggregate of all Gobi sales by any dealer on 5/21
… all such combinations are listedWITH CUBE
Red Gobis sold at Friendly Fred’s on 5/21
Red Gobis sold at any dealer on 5/21
Red Gobis sold at any dealer on any date
Any Gobi sold at any dealer on any date
Aggregate of all autos sold
28. Homework
• 1a) Use the ‘sfabini’ mysql db to answer Exercise 10.7.2 (a)
and (b) from the book
• Notes:
• Execute your query FROM the SalesCube table
• Use theWITH ROLLUP operator, instead ofWITH CUBE
• Smilin Sally is spelled without the apostrophe in the DB
• See Example 10.33 for further hints on usingWITH ROLLUP