सामग्री पर जाएँ

लेसन 19 · Querying and Performance

Partitioning, Clustering and Columnar Performance

Speed up warehouse queries by reading less data: columnar storage, compression, partition pruning, clustering, join habits, indexes and query plans.

Most warehouse tuning comes down to one idea: read less data and move less data. A query that skips what it does not need is usually faster, and on many cloud platforms cheaper too. Features and names differ between products, so treat this lesson as concepts and confirm the details in your platform's documentation.

Columnar storage and compression

Because a column store keeps each column separately, a query reads only the columns it names. So SELECT * on a wide table can be much more work than selecting three columns.

Similar values stored together compress well. Common techniques include:

  • Dictionary encoding: replace repeated text with small codes.
  • Run-length encoding: store "Portugal, 5,000 times" instead of 5,000 copies.
  • Delta encoding: store differences between neighboring values.

Columnar engines usually choose encodings automatically or from table settings.

SQL Stand-in: why sorted data compresses into fewer runs

Press Run (or Ctrl + Enter) to see the result here.

The same 12 values need many runs unsorted but only 3 when sorted.

Partitioning

Partitioning divides a table into parts based on a column, most often a date. When a query filters on that column, the engine can skip the other parts. This is partition pruning.

SQL
-- Read-only: PostgreSQL-style range partitioning; other platforms differ
CREATE TABLE fact_sales (
  sale_date date NOT NULL,
  product_key integer,
  amount numeric(12, 2)
) PARTITION BY RANGE (sale_date);

CREATE TABLE fact_sales_2026_01 PARTITION OF fact_sales
  FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');

Partitions also help operations: you can reload or remove a whole month at once.

Too many tiny partitions add overhead. Choose a size that suits your data volume and the platform's guidance and limits.

Zone maps and clustering

Many engines keep minimum and maximum values for each block of stored data (called zone maps, block statistics or micro-partition metadata, among other names). A block whose range cannot match the filter is skipped.

This works well when related values are stored near each other. Clustering or sort keys arrange data to improve that. Good candidates are columns you often filter or join on. Some platforms maintain the order automatically, others need maintenance jobs, and either may cost compute.

SQL Stand-in: skip blocks using min and max

Press Run (or Ctrl + Enter) to see the result here.

Ordered data needs one block; scattered data needs all ten, because every block's range covers day 42.

Write filters that can prune

Whether pruning happens depends on the engine, but these habits help:

  • Filter directly on the partition or cluster column with a range, such as sale_date >= '2026-03-01' AND sale_date < '2026-04-01'.
  • Avoid wrapping the column in functions, such as YEAR(sale_date) = 2026, unless your platform documents that it can still prune.
  • When you filter through the date dimension, check the query plan to see whether pruning still works.
  • Compare values using matching data types.

Joins and data movement

Joins between a large fact table and small dimensions are usually efficient. In distributed engines, a small table can be copied to each worker, while two large tables may need to be redistributed by the join key. To help:

  • Filter early.
  • Join on well-typed keys.
  • Watch for skew: if one key value holds a large share of rows, one worker does most of the work.
  • Aggregate before joining when that keeps the meaning correct.

Indexes

Row-store databases rely heavily on B-tree indexes. Many columnar warehouses rely on pruning and ordering instead, and offer few or different kinds of index. If you build a warehouse on a row database such as PostgreSQL or SQL Server, indexes (and, where available, columnstore options) are important.

SQL See a query plan change with an index

Press Run (or Ctrl + Enter) to see the result here.

The first plan scans the table; the second searches using the index. The plan format is specific to SQLite, but every serious platform offers some way to inspect a plan.

A tuning routine

  1. Reproduce the slow query and record its time and data scanned.
  2. Read the query plan or profile.
  3. Find the most expensive step: scanning, joining, sorting or spilling to disk.
  4. Change one thing.
  5. Check that results are identical and measure again. Cached results can fool you.

More on this in query plans.

Many small files

On platforms that store data in files, many tiny loads can leave a huge number of small files. The engine then spends time opening files and reading metadata instead of data. Compaction rewrites them into fewer, larger files. Some services do this automatically and some need scheduled maintenance. If you load every few seconds, find out how your platform handles it. See partitioning and compaction.

Statistics

Query optimizers estimate how many rows each step will produce, using statistics about the tables. Outdated or missing statistics can lead to a poor join order or method. Platforms differ: some keep statistics current automatically, others need a command after large loads. When a plan's estimated row count is very different from the actual count, statistics are one of the first things to check.

Model choices that affect speed

Design decisions often matter more than settings:

  • A grain that is finer than any question needs increases every scan. Be sure the detail is useful before storing it, and consider aggregates for common summaries.
  • Wide text columns in fact tables add storage and scanning work when selected. Move descriptions to dimensions.
  • Matching key types let joins avoid conversions.
  • Repeated heavy transformations in views make every dashboard pay again. Store the result when many readers need it.

A tuning example in words

Suppose a monthly report takes far longer than expected. The profile shows that it scans every partition. The filter applies a formatting function to sale_date. You rewrite it as a range on the column itself. The plan now shows only one month being read. You compare the totals from the old and new queries, which match, and record the change. Had they differed, one likely suspect would be an error at the month boundary.

Common mistakes

Buying more compute can hide a poor query for a while at a higher cost. On the other hand, do not tune a query that runs once a month for a few seconds. Spend effort where time and money actually go.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why can SELECT * be wasteful on a column store?

    A column store saves work only for the columns you leave out.
  2. 2. What is partition pruning?

    The engine reads only the relevant parts of the table.
  3. 3. Why does ordering data help min/max block skipping?

    Narrow, non-overlapping ranges make it clear which blocks cannot match.
  4. 4. What should you do after changing something to speed up a query?

    A faster wrong answer is not an improvement.

Summary

  • Speed mainly comes from reading and moving less data.
  • Columnar storage, compression, partitioning and clustering reduce reads.
  • Filters should be written so the engine can prune; verify with query plans.
  • Measure before and after, and tune where it matters.

Next: Materialized Views and Aggregate Tables

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।