We value your privacy

This site uses cookies to improve your browsing experience, analyze site traffic, and show personalized content. See our Privacy Policy.

  1. MooseStack
  2. Moose OLAP
  3. Projections

Projections

Projections for ClickHouse tables

Moose lets you declare projections directly in your table definitions. Projections store data in an alternative order (or pre-aggregated) within each data part, so queries that filter or order by non-primary-key columns can avoid full scans.

When to use projections

  • Use projections when a table is regularly queried by a column that isn't in the primary ORDER BY.
  • Trade-off: projections increase storage and insert cost proportional to the number of projections.
  • Projections only apply to MergeTree-family engines.
Projections.ts
import { OlapTable, ClickHouseEngines } from "@514labs/moose-lib"; interface Events {  id: string;  userId: string;  timestamp: Date;  value: number;} export const EventsTable = new OlapTable<Events>("Events", {  engine: ClickHouseEngines.MergeTree,  orderByFields: ["id"],  projections: [    { name: "proj_by_user", body: "SELECT _part_offset ORDER BY userId" },    { name: "proj_by_ts", body: "SELECT _part_offset ORDER BY timestamp" },  ],});

How Moose applies changes

  • On create, Moose emits PROJECTION ... entries inside CREATE TABLE.
  • On change, Moose plans ALTER TABLE DROP PROJECTION <name> then ADD PROJECTION ... if the definition changed; pure adds/drops are applied as single operations.

On this page

Projections for ClickHouse tablesWhen to use projectionsHow Moose applies changes
Edit this page
FiveonefourFiveonefour
Fiveonefour Docs
MooseStackHostingTemplatesGuides
Release Notes
Source531
  • Overview
Build a New App
  • 5 Minute Quickstart
  • Browse Templates
  • Existing ClickHouse
Add to Existing App
  • Next.js
  • Fastify
Fundamentals
  • Moose Runtime
  • MooseDev MCP
  • Language Server
  • Data Modeling
Moose Modules
  • Moose OLAP
    • Data Modeling
    • Tables
    • Views
    • Materialized Views
    • Materialized Columns
    • External Data & Introspection
    • External Tables
    • Introspecting Tables
    • Data Access
    • Inserting Data
    • Reading Data
    • Performance & Optimization
    • Schema Optimization
    • Secondary & Data-skipping Indexes
    • Projections
    • TTL (Time-to-Live)
    • Schema Versioning
  • Moose Streaming
  • Moose Workflows
  • Moose APIs & Web Apps
Deployment & Lifecycle
  • Moose Dev
  • Moose Migrate
  • Moose Deploy
Reference
  • API Reference
  • Query Layer
  • Data Types
  • Table Engines
  • CLI
  • Configuration
  • Observability Metrics
  • Help
  • Release Notes
Contribution
  • Documentation
  • Framework
Projections.ts
import { OlapTable, ClickHouseEngines } from "@514labs/moose-lib"; interface Events {  id: string;  userId: string;  timestamp: Date;  value: number;} export const EventsTable = new OlapTable<Events>("Events", {  engine: ClickHouseEngines.MergeTree,  orderByFields: ["id"],  projections: [    { name: "proj_by_user", body: "SELECT _part_offset ORDER BY userId" },    { name: "proj_by_ts", body: "SELECT _part_offset ORDER BY timestamp" },  ],});