Sunday, 3 August 2025

Optimizing Aggregation Queries with Star-Tree Index in Apache Pinot

Star-tree index is a hybrid indexing technique in Apache Pinot that optimizes query performance for aggregated queries on large datasets. It pre-aggregates data at various levels and stores it in a tree structure, enabling fast lookups without scanning the entire dataset. 

Let me explain it with an example.

 

1. Consider the following retail_sales table:

 

order_id

region

category

sub_category

product

sales_amount

101

US

Electronics

Mobile

iPhone 15

1200

102

US

Electronics

Mobile

Samsung S23

1000

103

US

Electronics

Laptop

MacBook Pro

2500

104

US

Electronics

Laptop

Dell XPS

1800

105

EU

Electronics

Mobile

iPhone 14

1100

106

EU

Electronics

Laptop

Lenovo ThinkPad

1600

107

EU

Clothing

Shoes

Nike AirMax

200

108

EU

Clothing

Shoes

Adidas Ultra

220

109

APAC

Clothing

T-Shirts

Uniqlo Tee

50

110

APAC

Electronics

Mobile

OnePlus 11

900

 

2. Configuring Star-Tree Index with 3 Levels

{
  "tableName": "retail_sales",
  "tableType": "OFFLINE",
  "tableIndexConfig": {
    "starTreeIndexConfigs": [
      {
        "dimensionsSplitOrder": ["region", "category", "sub_category"],
        "skipStarNodeCreationForDimensions": ["product"],
        "functionColumnPairs": ["SUM(sales_amount)"]
      }
    ]
  }
}

Now, aggregations (here I set it as sum of sales_amount) are precomputed at each level (region, category, sub_category).

 

3. Understanding SUM(sales_amount) at Each Node

The Star-Tree Index generates precomputed sums as follows:

 

3.1 Root Node (Total Sales): At the top level, Pinot precomputes the total sales amount for the entire dataset:

 

SUM(sales_amount) = 1200 + 1000 + 2500 + 1800 + 1100 + 1600 + 200 + 220 + 50 + 900 = 10570

 

3.2 Region-Level Aggregation: At the Region Level, Pinot stores:

 

·      US Sales: 1200 + 1000 + 2500 + 1800 = 6500

·      EU Sales: 1100 + 1600 + 200 + 220 = 3120

·      APAC Sales: 50 + 900 = 950

 

 


3.3 Category-Level Aggregation within Each Region: Within each region, the system further precomputes sums at the category level:

 

US Region

category 

SUM(sales_amount)

Electronics

1200 + 1000 + 2500 + 1800 = 6500

 

EU Region

category

SUM(sales_amount)

Electronics

1100 + 1600 = 2700

Clothing 

200 + 220 = 420

 

APAC Region

category 

SUM(sales_amount)

Electronics

900

Clothing 

50

  


 

Sub-Category-Level Aggregation within Each Category

At the Sub-Category Level, Pinot further precomputes sums:

 

3.4 Sub-Category-Level Aggregation within Each Category

At the Sub-Category Level, Pinot further precomputes sums:

                                            

US & Electronics

sub_category 

SUM(sales_amount)

Mobile 

1200 + 1000 = 2200

Laptop 

2500 + 1800 = 4300

 

EU & Electronics

sub_category 

SUM(sales_amount)

Mobile 

1100

Laptop 

1600

 

 

EU& Clothing

sub_category 

SUM(sales_amount)

Shoes

200 + 220 = 420

 

 

APAC & Electronics

sub_category 

SUM(sales_amount)

Mobile 

900

 

APAC & Clothing

sub_category

SUM(sales_amount)

T-Shirts 

50

 

Final tree looks like below.

 


The index now contains precomputed SUM values at different levels:

Root
 ├── US (6500)
 │    ├── Electronics (6500)
 │    │    ├── Mobile (2200)
 │    │    ├── Laptop (4300)
 ├── EU (3120)
 │    ├── Electronics (2700)
 │    │    ├── Mobile (1100)
 │    │    ├── Laptop (1600)
 │    ├── Clothing (420)
 │    │    ├── Shoes (420)
 ├── APAC (950)
 │    ├── Electronics (900)
 │    │    ├── Mobile (900)
 │    ├── Clothing (50)
 │    │    ├── T-Shirts (50)

Now, if we run a query like below:

SELECT region, category, sub_category, SUM(sales_amount)
FROM retail_sales
GROUP BY region, category, sub_category;

Instead of scanning the entire dataset, Pinot retrieves precomputed aggregates from the Star-Tree Index, making the query nearly instant.

 

When to Use Star-Tree Index?

·      When queries involve aggregations (SUM, AVG, COUNT, etc.).

·      When working with high-cardinality dimensions (e.g., millions of users or products).

·      When optimizing for low-latency analytical queries.

 

It is not useful for raw data lookups or non-aggregated queries.

 

In summary, Apache Pinot’s Star-Tree Index is a powerful technique for speeding up aggregation queries. By precomputing results and organizing data in a hierarchical tree structure, it significantly reduces query time, making it ideal for real-time analytics on large-scale datasets.

 

 

Previous                                                    Next                                                    Home

Creating an Index in Apache Pinot (Step-by-Step Guide)

Apache Pinot is a real-time distributed OLAP datastore designed for low-latency analytics. Indexing in Pinot helps to optimize query performance. In this guide, we will go through the step-by-step process to:

 

·      How to create indexes?

·      Define a schema

·      Define a table configuration with an index

·      Upload the schema and table

·      Verify the table and query it

 

How to Create Indexes?

Indexes in Apache Pinot are specified inside the "tableIndexConfig" object of the table configuration (table.json). 

{
  "tableName": "somePinotTable",
  "tableType": "OFFLINE",
  "segmentsConfig": {
    "schemaName": "someSchema",
    "timeColumnName": "eventTime",
    "timeType": "MILLISECONDS"
  },
  "tableIndexConfig": {
    "bloomFilterColumns": ["playerID"],
    "sortedColumn": ["eventTime"],
    "invertedIndexColumns": ["category"],
    "rangeIndexColumns": ["price"],
    "starTreeIndexConfigs": [{
      "dimensionsSplitOrder": ["Country", "Browser", "Locale"],
      "functionColumnPairs": ["SUM__Impressions"],
      "maxLeafRecords": 1
    }]
  },
  "tenants": {
    "broker": "DefaultTenant",
    "server": "DefaultTenant"
  }
}

Indexes can be defined when creating a new table using the tableIndexConfig object inside the table configuration file.

 

If an index needs to be added or modified after the table is created, update the table configuration using Pinot's API.

 

 

Let’s follow below step-by-step procedure to define invertedIndex on country column.

 

Step 1: Define a Schema

A schema in Pinot describes the structure of the data, defining columns and data types.

 

country_schema.json

 

{
  "schemaName": "country_schema",
  "dimensionFieldSpecs": [
    {
      "name": "country",
      "dataType": "STRING"
    },
    {
      "name": "city",
      "dataType": "STRING"
    }
  ],
  "metricFieldSpecs": [
    {
      "name": "population",
      "dataType": "INT"
    }
  ],
  "dateTimeFieldSpecs": [
    {
      "name": "eventTime",
      "dataType": "LONG",
      "format": "1:MILLISECONDS:EPOCH",
      "granularity": "1:MILLISECONDS"
    }
  ]
}

Let’s onboard this schema into Pinot using the Swagger User interface.

 

Open Pinot user interface by navigating to the url http://localhost:9000.

 


Click on ‘Swagger REST API’ option available at left navigation bar to open the Swagger User Interface.

 

Execute the API ‘POST /schemas’, with country_schema.json payload to onboard the schema.


Upon successful insertion of schema, you can see following response.

{
  "unrecognizedProperties": {},
  "status": "country_schema successfully added"
}

 

Step 2: Define a Table with an Index

A table in Pinot is associated with a schema and contains configurations for indexing and ingestion.

 

country_table.json

{
  "tableName": "country",
  "tableName": "country_schema",
  "tableType": "OFFLINE",
  "segmentsConfig": {
    "timeColumnName": "eventTime",
    "timeType": "MILLISECONDS",
    "retentionTimeUnit": "DAYS",
    "retentionTimeValue": "30",
    "schemaName": "country_schema",
    "segmentAssignmentStrategy": "Balanced",
    "replication": "1"
  },
  "tableIndexConfig": {
    "invertedIndexColumns": ["country"]
  },
  "tenants": {
    "broker": "DefaultTenant",
    "server": "DefaultTenant"
  },
  "metadata": {}
}

Execute the API ‘POST /tables’ to onboard the tables.

 

Upon successful insertion on table definition, you can see below response.

{
  "unrecognizedProperties": {},
  "status": "Table country_schema_OFFLINE successfully added"
}

 

Previous                                                    Next                                                    Home

Friday, 1 August 2025

Range Index in Apache Pinot

The Range Index in Apache Pinot divides numeric values into predefined ranges (buckets). These ranges are created during segment creation, and helps Pinot quickly filter queries like price > 500 or age BETWEEN 20 AND 30 kinds of queries efficiently.

 

Let’s take a sample dataset:

product_id

name

price

timestamp

1

Laptop

1000

1700000000

2

Phone

700

1700005000

3

Tablet

500

1700010000

4

Keyboard

50

1700020000

5

Monitor

300

1700030000

 

Let's a Range Index on:

·      price (INT)

·      timestamp (LONG)

 

Range Index for price

Range

Records in this range

0-250

[]

251-500

[3, 5]

501-750

[2]

751-1000

[1]

 

Range Index for timestamp

Range

Records in this range

1699999000-1700005000

[1, 2]

1700005001-1700015000

[3]

1700015001-1700025000

[4]

1700025001-1700035000

[5]

 

For example, when you execute following query

SELECT * FROM products WHERE price > 500;

 

How Pinot Uses the Range Index?

·      Pinot finds relevant ranges:

o   It skips 0-250 and 251-500 ranges.

o   It directly looks up 501-750 and 751-1000, and get the records {2, 1}

·      It retrieves rows 2 and 1 (Phone and Laptop).

·      No full table scan is needed in this case

 

In summary,

·      Range Indexes are optimized for BETWEEN, >, < queries.

·      Best for analytics on numeric/timestamp columns.

 

Previous                                                    Next                                                    Home