---
title: "Run-length encoding"
description: "Run-length encoding (RLE) is a compression technique that replaces consecutive repeated values with a single (value, count) pair, shrinking runs of identical data."
canonical: "https://motherduck.com/glossary/run-length-encoding/"
related:
  - title: "DuckLake Architecture Deep Dive"
    url: "https://motherduck.com/blog/ducklake-architecture-deep-dive/"
  - title: "Loading Data Best Practices | MotherDuck Docs"
    url: "https://motherduck.com/docs/key-tasks/loading-data-into-motherduck/considerations-for-loading-data/"
  - title: "Analyze JSON Data Using SQL and DuckDB"
    url: "https://motherduck.com/blog/analyze-json-data-using-sql/"
---

# Run-length encoding

> Run-length encoding (RLE) is a compression technique that replaces consecutive repeated values with a single (value, count) pair, shrinking runs of identical data.

## Overview

Run-length encoding takes advantage of runs — consecutive repeated values — in a dataset. Instead of storing each occurrence of a repeated value individually, RLE stores the value once along with how many times it repeats in a row. It's one of the oldest and simplest compression techniques, and it remains highly effective for columnar data that's sorted or naturally clustered.

## How RLE Works

A column like `['CA', 'CA', 'CA', 'CA', 'NY', 'NY', 'TX']` becomes the pair sequence `[('CA', 4), ('NY', 2), ('TX', 1)]`. The longer the runs, the greater the compression ratio; a column with no repeated adjacent values compresses poorly (or not at all) under RLE.

## Best-Case Data

RLE performs best on columns that are sorted or clustered by that value — a `date` column in a table stored in date order, or a `status` column where a table has been physically sorted or partitioned by status. It performs poorly on columns with high cardinality or no locality, such as a randomly ordered UUID column.

## DuckDB's Use of RLE

DuckDB automatically applies run-length encoding as one of its per-column-chunk lightweight compression schemes whenever it detects that a chunk has long runs of repeated values, alongside dictionary encoding, bit packing, and FSST — chosen automatically with no configuration. Because RLE benefits so much from data locality, sorting or clustering a table on a low-cardinality column before loading it into DuckDB (or writing it out with `ORDER BY` in a `COPY ... TO 'file.parquet'`) can meaningfully improve both compression ratio and scan speed.
