Why Optimise

A warehouse holds hundreds of millions of rows and users ask complex queries with joins and aggregations. Without tuning, a query may take hours. Three main techniques make queries fast: indexing, partitioning and query optimisation.

Indexing

An index lets the system find rows without scanning the whole table.

  • B-tree index: a balanced tree good for columns with many different values (customer id, date) and for range queries.
  • Bitmap index: for each distinct value of a low-cardinality column (gender, region, category) a bit vector has one bit per row. Queries with AND, OR and NOT are answered by fast bitwise operations, and counts need no table access.
  • Join index: pre-computes the join between a dimension and the fact table; the star-join index speeds up star schema queries.

Indexes need storage and slow down loading, so they are created for the columns used most in queries.

Partitioning

Partitioning splits a large table into smaller parts.

  • Range partitioning: by value ranges such as month or year.
  • List partitioning: by a list of values such as region.
  • Hash partitioning: by a hash of a key, to spread the load evenly.

The optimiser uses partition pruning: only partitions that can contain the answer are read. Partitions can also be loaded, backed up and queried in parallel, and old partitions can be archived.

Query Optimisation

  • Materialised views: store the results of common summaries (sales by month and category); the optimiser rewrites queries to use them.
  • Cost-based optimiser: uses statistics to choose the cheapest join order and access path.
  • Parallel processing and caching, and writing queries that filter early and select only needed columns.

Definition: Bitmap index. An index that keeps, for each value of a column, a string of bits with one bit per row showing whether the row has that value.

Table 4.5: Index types

Index Best for Note
B-tree High-cardinality columns, ranges General purpose
Bitmap Low-cardinality columns Fast AND, OR, count
Join index Fact-dimension joins Pre-computed joins

Example 4.5: Bitmap index query and partition pruning

Problem: Eight rows have Gender M, F, M, M, F, M, F, M and Region N, S, N, N, S, S, N, N. How many rows match Gender = M AND Region = N using bitmaps? Also, a 120-million-row table is partitioned by month over 5 years; how many rows does a one-month query read?

Solution:

  1. Bitmap of Gender = M: 10110101. Bitmap of Region = N: 10110011.
  2. Bitwise AND: 10110001, which has four 1 bits, at rows 1, 3, 4 and 8.
  3. Partitions = 5 x 12 = 60, so each holds 120,000,000 / 60 = 2,000,000 rows.
  4. A one-month query reads one partition, 2,000,000 rows, which is 1/60 or 1.67 percent of the table.

Result: Four rows match, and partition pruning reduces the scan to 1.67 percent of the data.

gender = "MFMMFMFM"
region = "NSNNSSNN"
bm = int("".join("1" if g == "M" else "0" for g in gender), 2)
br = int("".join("1" if r == "N" else "0" for r in region), 2)
both = bm & br
print("Bitmap M   :", format(bm, "08b"))
print("Bitmap N   :", format(br, "08b"))
print("AND        :", format(both, "08b"))
print("Matching rows:", bin(both).count("1"))
parts = 5 * 12
rows = 120_000_000 // parts
print("Rows per partition:", rows)
print("Scan fraction %:", round(100 / parts, 2))

Output

Bitmap M   : 10110101
Bitmap N   : 10110011
AND        : 10110001
Matching rows: 4
Rows per partition: 2000000
Scan fraction %: 1.67

Common mistakes

  • Using a bitmap index on a column with millions of distinct values.
  • Creating too many indexes, which slows data loading.
  • Partitioning on a column that queries never filter on, so no pruning happens.
  • Not refreshing materialised views after new data is loaded.

Key takeaways

  • Indexing: B-tree, bitmap and join indexes.
  • Partitioning: range, list and hash; partition pruning reads fewer rows.
  • Materialised views store pre-computed summaries.
  • A cost-based optimiser picks the cheapest plan using statistics.

Practice questions

  1. When is a bitmap index suitable?
  2. What is partition pruning?
  3. What is a materialised view?

Answers to the practice questions

  1. For columns with few distinct values, such as gender or region.
  2. Reading only the partitions that can contain the answer.
  3. A stored result of a query, used to answer later queries faster.

Sources

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