Data Cube

A data cube is a multidimensional model in which each axis is a dimension and each cell holds a measure. It is called a cube although it may have more than three dimensions. Every combination of dimensions to group by is a cuboid. The cuboid with all dimensions is the base cuboid; the cuboid with no dimension (a single total) is the apex cuboid. Together the cuboids form a lattice.

Number of Cuboids

For n dimensions with no hierarchies there are 2^n cuboids. If dimension i has Li hierarchy levels, the number is (L1 + 1) x (L2 + 1) x ... x (Ln + 1), the extra one being the level "all".

Data Cube Operations

  • Roll-up: move up a hierarchy or remove a dimension to get a summary (month to year).
  • Drill-down: the reverse, moving to more detail (year to month).
  • Slice: select one value of one dimension, giving a sub-cube (month = Jan).
  • Dice: select values on two or more dimensions (city in Delhi or Pune and month in Jan).
  • Pivot (rotate): turn the axes to see the data from another side.

Data Cube Computation

Computing all cuboids is costly. Methods include full materialisation (all cuboids, fast queries, large space), no materialisation (compute when asked, slow) and partial materialisation (choose the most useful cuboids). A cuboid is computed from a smaller parent cuboid, for example the (product, city) cuboid from (product, city, month). Algorithms such as multiway array aggregation and BUC (bottom-up computation) use this idea, and the iceberg cube keeps only cells above a minimum count.

Definition: Data cube. A multidimensional structure of aggregated measures, with one axis for each dimension, that allows summaries at every combination of dimension levels.

Table 1.3: Data cube operations

Operation Meaning Effect
Roll-up Aggregate to a higher level Less detail
Drill-down Go to a lower level More detail
Slice Fix one dimension value Sub-cube
Dice Select a range on many dimensions Smaller cube
Pivot Rotate the axes New view

Example 1.3: Counting cuboids

Problem: A cube has dimensions time (day, month, year), location (city, country) and product (item, type). How many cuboids are possible, and how many if there were 4 dimensions with no hierarchy?

Solution:

  1. Levels are 3, 2 and 2, so each dimension also has the level "all".
  2. Number of cuboids = (3 + 1) x (2 + 1) x (2 + 1) = 4 x 3 x 3 = 36.
  3. With no hierarchy, n = 4 dimensions give 2^4 = 16 cuboids.

Result: The hierarchical cube has 36 cuboids; a plain 4-dimensional cube has 16.

from itertools import combinations
rows = [("Pen","Delhi","Jan",100), ("Book","Delhi","Jan",250),
        ("Mouse","Pune","Jan",600), ("Pen","Pune","Feb",200),
        ("Mouse","Delhi","Feb",300), ("Book","Pune","Feb",200)]
dims = ["product", "city", "month"]
count = sum(1 for k in range(4) for _ in combinations(dims, k))
print("Cuboids for 3 dimensions:", count)
def rollup(pos):
    out = {}
    for r in rows:
        out[r[pos]] = out.get(r[pos], 0) + r[3]
    return out
print("By month  :", rollup(2))
print("By product:", rollup(0))
print("Apex total:", sum(r[3] for r in rows))
jan = [r for r in rows if r[2] == "Jan"]
print("Slice month=Jan:", sum(r[3] for r in jan))

Output

Cuboids for 3 dimensions: 8
By month  : {'Jan': 950, 'Feb': 700}
By product: {'Pen': 300, 'Book': 450, 'Mouse': 900}
Apex total: 1650
Slice month=Jan: 950

Common mistakes

  • Forgetting that the apex cuboid and the base cuboid are both counted in 2^n.
  • Confusing slice (one dimension fixed) with dice (several dimensions restricted).
  • Believing that roll-up always removes a dimension; it may also climb a hierarchy.
  • Materialising every cuboid for a cube with many dimensions.

Key takeaways

  • Cuboid count is 2^n, or the product of (levels + 1) with hierarchies.
  • Roll-up, drill-down, slice, dice and pivot are the cube operations.
  • Base cuboid has all dimensions; apex has none.
  • Partial materialisation balances space and speed.

Practice questions

  1. How many cuboids has a 5-dimensional cube without hierarchies?
  2. What is the difference between roll-up and drill-down?
  3. What is the apex cuboid?

Answers to the practice questions

  1. 2^5 = 32.
  2. Roll-up gives less detail; drill-down gives more detail.
  3. The cuboid with no dimensions, which holds the grand total.

Sources

  • Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit I