Sitemap
Press enter or click to view image in full size

Understanding `datagroup`, `` , and ``in LookML

6 min readApr 28, 2025

--

In LookML, the modeling language used by Looker, managing data caching is critical for balancing query performance and data freshness. Three key concepts: , , and provide granular control over caching within the model file. This article offers an exhaustive explanation of these concepts using an analogy, a diagram, an example, plain speech, and technical language, culminating in a comprehensive summary.

Analogy

The Notebook Concept: Imagine you’re a student with a notebook where you jot down answers to questions you’ve already solved. This saves you from recalculating or rethinking every time someone asks the same question; much like caching in Looker, where query results are stored for reuse.

Updating Rules with : Now, picture setting rules for when to update your notebook. You might decide to refresh your notes every morning (a time-based rule) or whenever a teacher gives you new information (a condition-based rule). In LookML, a is like these rules; it defines when cached data should be refreshed, either on a schedule or when specific conditions are met.

Choosing Rules with : Suppose your notebook has sections; like math, history, and science; and you apply different updating rules to each. For math, you update daily; for history, weekly. In LookML, lets you assign a specific to an explore or model, tailoring the caching policy to that component, just as you tailor rules to notebook sections.

Setting a Timer with : Alternatively, imagine writing an answer in your notebook and adding a note: “Keep this for one week, then redo it.” This is akin to in LookML, where you set a fixed time limit for how long a cached result remains valid before it’s discarded and recalculated.

Why It Works Together: Your notebook system ensures you’re efficient (reusing answers) yet current (updating when needed). Similarly, , , and work together in LookML to optimize performance while keeping data fresh, adapting to different needs across your model.

Diagram

Below is a Mermaid flowchart depicting the caching decision process in LookML, illustrating how , , and influence whether cached data is used or refreshed.

Press enter or click to view image in full size

Explanation of Components:

  • A (User runs query): The process begins when a user executes a query in Looker.
  • B (Is there a cached result?): Looker checks if a cached result exists for this query.
  • C (Is cache still valid?): If a cache exists, its validity is assessed based on the caching policy.
  • D (Run query and cache result): If no cache exists or it’s invalid, Looker runs the query and caches the result.
  • E (Use cached result): If the cache is valid, Looker returns it without rerunning the query.
  • F (Caching Policy): This input represents the policy governing validity, splitting into two paths:
  • G (persist_for): Validity depends on whether the cache’s age is less than the time set by (e.g., "24 hours").
  • H (datagroup via persist_with): Validity hinges on whether the ’s trigger (e.g., a SQL condition or time event) has fired since the cache was created.

Significance: This diagram highlights the decision-making process Looker uses for caching, showing how offers a simple time-based check, while (via ) enables more dynamic, trigger-based control. It visually ties the concepts to their practical application in query execution.

Example

Here’s a concrete LookML example demonstrating , , and within a model file:

Scenario: You’re modeling sales data and want to manage caching for an explore. You’ll define a for daily refreshes and compare it with a approach.

Code:

Breakdown:

Datagroup Definition:

  • : Creates a named caching policy.
  • : This SQL query changes value at midnight daily, invalidating the cache and triggering a refresh on the next query.

Explore with :

  • : Links to the view.
  • : Ties this explore to the datagroup, ensuring its cache refreshes daily at midnight.

Explore with :

  • : Also links to the view.
  • : Sets a 24-hour cache duration, meaning the cache is valid for 24 hours from when it was created, refreshing only when a query finds it expired.

Behavior:

  • For , the cache resets at midnight daily, regardless of when queries run.
  • For , the cache lasts 24 hours from each query’s execution, expiring individually per query.

Significance: This example shows how offers scheduled, proactive control, while provides a simpler, reactive time limit, both applied within the model file to tailor caching per explore.

Plain Speech

What’s Caching?: In Looker, when you ask a question about your data ; like “How many orders did we get this month?”; it can save the answer. Next time someone asks the same thing, Looker just pulls out the saved answer instead of figuring it out again. This saving trick is called caching, and it makes things faster.

Get Michael Stroud’s stories in your inbox

Join Medium for free to get updates from this writer.

Why Refresh?: But data changes; like new orders coming in; so you don’t want to keep old answers forever. You need rules to decide when to toss the saved answer and get a new one.

- The Timer: One way is like setting a timer. You tell Looker, “Keep this answer for 24 hours, then forget it and get a fresh one.” That’s what does. It’s simple: after the time’s up, the next person asking gets a new answer.

- The Smart Rules: Another way is smarter. Imagine checking every day at midnight to see if your answers need updating, or watching for a signal—like new data arriving.

  • A is like that: it sets rules for when to refresh, either at set times or when something changes.

- Picking Rules: Now, you might have different parts of your data; like orders or customers; and want different rules for each. lets you say, “For orders, use the midnight refresh rule.” It picks a for that part of your model.

How It Fits: These tools let you control caching in your Looker model file. You decide what’s fast and what’s fresh, section by section, like organizing a filing cabinet where some files expire quickly and others get a daily overhaul.

Technical Language

Overview: In LookML, caching enhances query performance by storing results, governed within the model file by , , and .

Datagroup:

Definition: A is a named entity defining a caching policy.

Parameters:

  • : A SQL query (e.g., ) whose result change invalidates the cache. For time-based triggers, a query like shifts daily.
  • : An optional time limit (e.g., "24 hours") for cache validity, though typically paired with triggers.

Behavior: When the trigger fires (e.g., SQL result changes), associated caches are invalidated, prompting a refresh on the next query.

Persist_with:

Definition: A parameter in or blocks linking them to a .

Syntax: .

Scope: Can be set at the model level (default for all explores) or explore level (override).

Effect: Applies the ’s policy to queries for that component, ensuring consistent refresh timing.

Persist_for:

Definition: A parameter specifying a fixed cache duration (e.g., “24 hours”).

Syntax: .

Scope: Applicable at model level (default) or explore level (specific override).

Behavior: Cached results are valid until the duration elapses from creation; subsequent queries refresh the cache if expired.

Execution Flow:

  • Query runs → Check for cached result → Validate cache:
  • If , compare cache age to duration.
  • If ties to a , check if trigger has fired since caching.
  • Valid cache → Return result; Invalid/No cache → Execute query, cache result.

Distinctions:

  • enables dynamic, trigger-driven policies; is static, time-based.
  • integrates flexibility into specific model components.

Conclusion/Summary

  • : A versatile tool for defining caching policies with triggers (SQL or time-based), offering precise control over refresh timing within the model file.
  • : Bridges policies to explores or models, enabling tailored caching strategies per component.
  • : A straightforward time-based caching option, setting fixed expiration periods for simplicity.
  • Combined Power: Together, these constructs allow LookML developers to optimize query performance and data freshness, balancing efficiency (reusing cached results) with accuracy (refreshing as needed). Whether using proactive triggers or reactive timeouts, they provide a robust framework for managing caching in Looker models.

This comprehensive exploration; from relatable analogies to technical precision; equips you with a deep understanding of how , , and function in LookML, empowering effective data modeling in Looker.

--

--

Michael Stroud
Michael Stroud