New LookML
Old LookML
New LookML
Caching Queries and Rebuilding PDTs with Datagroups


Looker reduces the load on your database and improves performance by using cached results of prior queries when available and permitted by your caching policy. In addition, you can create complex queries as PDTs which store their results to simplify later queries.

Using datagroups (using datagroup parameters) and persist_with parameters is the most powerful technique to define a caching policy for queries on explores and also for when persistent derived tables (PDTs) are rebuilt.

You use one or more datagroup parameters to name caching policies and specify the desired behavior. Then you can use persist_with parameters at the model or explore level to specify which explores use each policy and use datagroup_trigger in a PDT definition to specify which policy to use in rebuilding the PDTs.

Typical use cases for datagroups include:

How Looker Uses Cached Queries

For queries, the caching mechanism in Looker works as follows:

  1. Once a user runs a specific query, the result of that query is cached. Cache results are stored in an encrypted file on your Looker instance.
  2. When a new query is written, the cache is checked to see if the exact same query was previously run. All fields, filters, and parameters must be the same, including things such as the row limits. If the query is not found, then Looker runs the query against the database to get fresh database results (and those results are then cached).

    Note that Context Comments do not affect caching. If your Looker admin has enabled the Context Comments feature, Looker adds a unique comment to the beginning of each SQL query. But as long as the SQL query itself is the same as a previous query (not including the context comments), Looker will use cached results.

  3. If the query is found in the cache, then Looker checks whether the query results are still valid. By default, Looker invalidates cached results after an hour (unless you enabled the legacy feature to default to 5 minutes.) You can use a persist_for parameter (defined here and here) or the more powerful datagroup parameter to specify the caching policy for when or in what circumstances the cached results become invalid and should be ignored. An administrator can also cause the cache results for a datagroup to become invalid.
    • If the query results are still valid, then those results are used.
    • If the query results are no longer valid, then Looker runs the query against the database to get fresh query results. (Those new results are also cached.)

How Looker Uses PDTs and Rebuilds Them

PDTs are queries, typically very complex queries, that you can create as the basis of a view. For example, your query might identify the customer’s first and most recent orders plus the lifetime value of all of their orders.

These PDTs are rebuilt periodically based on one of three settings:

Using a datagroup gives you the most control over when the PDT is rebuilt. This page discusses setting up a datagroup for query caching and PDT rebuilding.

Specifying Caching Policies With datagroup Parameters

You define one or more datagroup parameters at the model level to specify the caching policy that you will assign to explores and/or PDTs. If you want different caching policies for different explores and/or PDTs, then specify each caching policy in a separate datagroup.

If the connection uses a user attribute to specify the connection username, then you cannot use a datagroup for models using that connection. Instead, use a persist_for parameter (defined here and here) for queries and either sql_trigger_value or persist_for for derived tables.

Each datagroup specifies a policy using one or both of these subparameters:

Specifying Using a Datagroup’s Caching Policy

You can specify using a datagroup’s caching policy for

Using a Datagroup for Query Results

You can specify that a datagroup’s caching policy be used for all queries on a specific explore, a group of explores, or as a default caching policy for all explores in a model.

To specify a datagroup’s caching policy as:

In the following example, the orders and orders_facts explores both use the orders_datagroup caching policy. That policy specifies that the query select max(id) from my_tablename will be used to detect when an ETL has happened. Even if that ETL doesn’t happen for awhile, the cached data will be used for a maximum of 24 hours.

Meanwhile, the customer_facts and customer_background explores both use the customers_datagroup caching policy. That policy specifies that the query select max(id) from my_other_tablename will be used to detect when an ETL has happened. If no ETL happens, the cached results will continue to be used.

datagroup: orders_datagroup { sql_trigger: SELECT max(etl_job_id) FROM my_tablename ;; max_cache_age: "24 hours" }   datagroup: customers_datagroup { sql_trigger: SELECT max(etl_job_id) FROM my_other_tablename ;; }   persist_with: orders_datagroup   explore: orders { … }   explore: order_facts { … }   explore: customer_facts { persist_with: customers_datagroup … }   explore: customer_background { persist_with: customers_datagroup … }

If both persist_with and persist_for are specified, then you will receive a validation warning and the persist_with will be used.

Using a Datagroup for a PDT

To use a datagroup’s caching policy to trigger rebuilding a PDT, you use the datagroup_trigger in the PDT’s definition and specify the name of the datagroup.

Do not also specify a sql_trigger_value or persist_for parameter for the PDT — if you do, then:


If we wanted to set the PDT to rebuild as specified by the customers_datagroup datagroup, we could use the following definition. This definition also adds several indexes, on both customer_id and first_order_date. For more information about defining PDTs, see this page.

view: customer_order_facts { derived_table: { sql: … ;; datagroup_trigger: customers_datagroup indexes: ["customer_id", "first_order_date"] } }

If you have PDTs that are dependent on other PDTs, be careful not to specify incompatible datagroup caching policies.

Using the Admin Panel for Datagroups

If you have the Admin role, you can use the Admin tab’s Datagroups page to view the existing datagroups. You can see the connection and model of each datagroup, as well as the datagroups’ current status. You can also reset the cache for a datagroup, trigger the datagroup, or navigate to the datagroup’s LookML.

Datagroups can also be used to trigger scheduled data delivery, as described on the Scheduling Data Deliveries page. With this option, Looker will send your data when the datagroup completes, so that the scheduled content is up to date.

Seeing Whether a Query Was Returned From Cache

You can determine whether or not a query has been returned from the cache by looking in the upper right corner after running a query.

If results are from a fresh query,
you will see …
If the results are from the cache,
you will see …

Forcing New Results To Be Generated From the Database

Users can also force new results to be retrieved from the database. Select the Clear Cache & Refresh option from the Gear dropdown menu, which you’ll find in the upper right of the screen after running a query:

A persistent derived table normally is regenerated based on the specified datagroup, persist_for, or sql_trigger_value. You can force the derived table to regenerate early if your admin has given you the develop permission, and you are viewing an Explore that includes fields from the PDT. Select the Rebuild Derived Tables & Run option from the Gear dropdown menu, which you’ll find in the upper right of the screen after running a query: