Skip to main content
Understanding DataStore’s lazy evaluation model is key to using it effectively and achieving optimal performance.

Lazy Evaluation

DataStore uses lazy evaluation - operations are not executed immediately but are recorded and compiled into optimized SQL queries. Execution happens only when results are actually needed.

Example: Lazy vs Eager

Benefits of Lazy Evaluation

  1. Query Optimization: Multiple operations compile to a single optimized SQL query
  2. Filter Pushdown: Filters are applied at the data source level
  3. Column Pruning: Only needed columns are read
  4. Deferred Decisions: Execution engine can be chosen at runtime
  5. Plan Inspection: You can view/debug the query before executing

Execution Triggers

Execution is triggered automatically when you need actual values:

Automatic Triggers

Examples:

Operations That Stay Lazy

Examples:

Three-Phase Execution

DataStore operations follow a three-phase execution model:

Phase 1: SQL Query Building (Lazy)

Operations that can be expressed in SQL are accumulated:

Phase 2: Execution Point

When a trigger occurs, the accumulated SQL is executed:

Phase 3: DataFrame Operations (if any)

If you chain pandas-only operations after execution:

Viewing Execution Plans

Use explain() to see what will be executed:
Query
Response
Use verbose=True for more details:
See Debugging: explain() for complete documentation.

Caching

DataStore caches execution results to avoid redundant queries.

How Caching Works

Cache Invalidation

Cache is invalidated when operations modify the DataStore:

Manual Cache Control


Mixing SQL and Pandas Operations

DataStore intelligently handles operations that mix SQL and pandas:

SQL-Compatible Operations

These compile to SQL:
  • filter(), where()
  • select()
  • groupby(), agg()
  • sort(), orderby()
  • limit(), offset()
  • join(), union()
  • distinct()
  • Column operations (math, comparison, string methods)

Pandas-Only Operations

These trigger execution and use pandas:
  • apply() with custom functions
  • pivot_table() with complex aggregations
  • stack(), unstack()
  • Operations on executed DataFrames

Hybrid Pipelines


Execution Engine Selection

DataStore can execute operations using different engines:

Auto Mode (Default)

Force chDB Engine

Force pandas Engine

See Configuration: Execution Engine for details.

Performance Implications

Good: Filter Early

Bad: Filter Late

Good: Select Columns Early

Good: Let SQL Do the Work


Best Practices Summary

  1. Chain operations before executing - Build the full query, then trigger once
  2. Filter early - Reduce data at the source
  3. Select only needed columns - Column pruning improves performance
  4. Use explain() to understand execution - Debug before running
  5. Let SQL handle aggregations - ClickHouse is optimized for this
  6. Be aware of execution triggers - Avoid accidental early execution
  7. Use caching wisely - Understand when cache is invalidated
Last modified on July 2, 2026