What You'll Learn
- 1Structure a LookML project with views, explores, and joins that match the warehouse grain
- 2Detect and prevent fanout using join relationships and symmetric aggregates
- 3Choose between inline derived tables, PDTs, and warehouse-native materialized views
- 4Control cache invalidation with datagroups tied to upstream pipeline completion
Looker sits on top of BigQuery and generates SQL from a semantic model written in LookML. For data engineers, the interesting part is not the dashboards but the modelling layer: explores define how business users combine tables, derived tables push computation into the database, and datagroups tie cache freshness to the pipeline that feeds it. Looker misconfigured is a fanout machine; Looker configured well is the most reliable self-serve surface in the stack.
LookML Project Anatomy
A LookML project contains views, which map to tables or derived tables and define dimensions and measures, and explores, which are the query interfaces users actually see. Joins live on explores, along with access filters, always-filters, and aggregate tables. The warehouse connection supplies the database dialect, so the same LookML compiles to different SQL for BigQuery, Snowflake, or Redshift, which is why warehouse-specific tuning belongs in the right dialect rather than in hardcoded SQL fragments.
view: orders {
sql_table_name: `project.analytics.orders` ;;
dimension: order_id {
primary_key: yes
type: string
sql: ${TABLE}.order_id ;;
}
dimension_group: created {
type: time
timeframes: [raw, date, week, month, quarter, year]
sql: ${TABLE}.created_at ;;
}
measure: count {
type: count
drill_fields: [detail*]
}
measure: total_revenue {
type: sum
sql: ${TABLE}.amount ;;
value_format_name: usd_0
}
set: detail {
fields: [order_id, created_date, total_revenue]
}
}
Primary keys earn their keep: Looker uses them for symmetric aggregates and for join correctness checks, and a missing primary_key declaration is the root cause of a surprising number of fanout bugs. Dimensions and measures should also carry value_format_name and labels deliberately, because a finance dashboard reading raw floats erodes trust faster than a wrong number does. Keep derived fields honest: if a dimension applies currency conversion, name it as such, and keep the unconverted field available for engineers who need the raw grain. The modelling layer is a product surface for business users, and the same review discipline you apply to API design applies here, including deprecation notes when a field is about to move.
Explore Design and Join Relationships
An explore decides which tables a user can combine and what the join keys mean. The relationship declaration tells Looker the cardinality, which drives both SQL generation and symmetric aggregate behaviour. Getting relationship wrong is silent: a one_to_many join to another one_to_many table produces a cartesian product that still returns numbers, just wrong ones.
explore: orders {
label: "Orders"
group_label: "Commerce"
description: "Order facts with customer and item detail"
join: customers {
type: left_outer
sql_on: ${orders.customer_id} = ${customers.customer_id} ;;
relationship: many_to_one
}
join: order_items {
type: left_outer
sql_on: ${orders.order_id} = ${order_items.order_id} ;;
relationship: one_to_many
}
always_filter: {
filters: [orders.created_date: "past 90 days"]
}
access_filter: {
field: customers.region
user_attribute: allowed_regions
}
}
Always-filter prevents full scans by forcing a date bound onto every query, which on a multi-terabyte BigQuery table is the difference between a two-second dashboard and a billed scan that finance notices. Access filters bind row visibility to user attributes, so regional analysts never see out-of-region rows even if the LookML permits the field.
Fanout and Symmetric Aggregates
Fanout happens when joins multiply rows before aggregation, inflating counts and sums. Looker's defence is symmetric aggregates: measures declared with count_distinct or sum_distinct deduplicate on the primary key during aggregation, so a one order to many items join cannot inflate order counts.
| Symptom | Likely cause | Fix |
|---|---|---|
| Counts too high after a join | fanout from a one_to_many join | symmetric aggregates or pre-aggregated derived table |
| Sum too high only with filters | filter applied post-join on the many side | restructure filter to the base view |
| Distinct users look inflated | multiple many-side joins | count_distinct on the users primary key |
The structural fix, when symmetric aggregates are not enough, is a derived table that pre-aggregates the many side before Looker joins it, which moves the multiplication into SQL the engineer controls.
Derived Tables and PDTs
Inline derived tables compile into every query that references them, which is fine for small transforms and expensive for heavy ones. Persistent derived tables materialise into the warehouse and refresh on a schedule controlled by datagroups, trading storage and refresh cost for query speed. On BigQuery, PDTs honour partition and cluster keys, so a PDT over years of events should be partitioned by date and clustered by the join key the explore uses.
view: user_order_summary {
derived_table: {
datagroup_trigger: commerce_daily_etl
partition_keys: ["first_order_date"]
cluster_keys: ["customer_id"]
sql:
SELECT
customer_id,
MIN(DATE(created_at)) AS first_order_date,
COUNT(*) AS lifetime_orders,
SUM(amount) AS lifetime_value
FROM `project.analytics.orders`
WHERE status = 'completed'
GROUP BY customer_id
;;
}
dimension: customer_id {
primary_key: yes
sql: ${TABLE}.customer_id ;;
}
measure: avg_lifetime_value {
type: average
sql: ${TABLE}.lifetime_value ;;
value_format_name: usd_0
}
}
Note what does not belong in a BigQuery PDT: distribution and sort keys are Redshift concepts, and copying Redshift-flavoured derived tables into a BigQuery project is a frequent review comment. Partition and cluster are the BigQuery levers.
Datagroups and Cache Invalidation
A datagroup is a freshness contract. The simplest form is a schedule, but the robust form is a SQL trigger: a query that returns a timestamp, usually the max loaded_at of the upstream mart, which Looker polls. When the trigger value moves past the cached value, PDTs rebuild and the query cache invalidates; until it moves, dashboards serve from cache at no BigQuery cost.
datagroup: commerce_daily_etl {
sql_trigger_value: SELECT MAX(loaded_at) FROM `project.analytics.orders` ;;
max_cache_age: "24 hours"
}
max_cache_age is the backstop: if the pipeline dies and the trigger stops moving, the cache ages out after the declared window rather than serving last week's numbers forever. Align that window with the pipeline's service level, not with optimism. Cache economics matter on BigQuery because every uncached query is a billed scan: a dashboard with a five-minute cache and a hundred viewers is one scan per five minutes, while the same dashboard without caching is a hundred scans. When users complain that a dashboard feels slow after a refresh, the correct investigation is whether the datagroup fired early and rebuilt PDTs, not whether BigQuery is suddenly slower; rebuilds and cache misses stack, and the first minute after a trigger change is the slowest by design.
Aggregate Awareness
Aggregate tables declare pre-computed rollups on an explore, and Looker's aggregate awareness routes queries to the smallest table that can answer them. A dashboard asking for daily revenue by region hits the daily aggregate instead of scanning order facts, with no change to the user's query or the LookML the dashboard references.
explore: orders {
aggregate_table: daily_orders {
query: {
dimensions: [created_date, customers.region]
measures: [count, total_revenue]
}
materialization: {
datagroup_trigger: commerce_daily_etl
}
}
}
Aggregate awareness requires the underlying fields to exist in the aggregate and the measures to be compatible, so the engineering task is auditing which query shapes your dashboards actually issue, via the Looker system logs, and building aggregates for the top shapes rather than for hypothetical ones.
Performance and Cost Review
Looker cost on BigQuery is a function of cache hit rate, aggregate coverage, and PDT refresh frequency. The review checklist: every explore used by a dashboard has an always-filter or a required_fields date bound; datagroups key off warehouse timestamps rather than wall clocks; PDTs are partitioned and clustered; and the Looker connection has a statement timeout so a user's ad hoc join cannot run for an hour unattended. System activity logs tell you which dashboards issue the largest scans, and those are the candidates for aggregate tables first.
Common Mistakes
Declaring a one_to_many join as many_to_one
Fix: Set the relationship from the base view's perspective; wrong cardinality silently multiplies every aggregate that crosses the join.
Copying Redshift distribution and sort keys into BigQuery PDTs
Fix: Use partition_keys and cluster_keys instead; Redshift keys are ignored or rejected and you lose the real BigQuery levers.
Driving datagroups off wall-clock schedules when the pipeline is event-driven
Fix: Use a SQL trigger on the upstream loaded_at so refreshes track the data, and set max_cache_age to bound staleness when the pipeline stalls.
Best Practices
- Audit system activity logs quarterly and promote the top query shapes into aggregate tables
- Give every explore an always-filter date bound on warehouse tables larger than a partition
- Keep derived table SQL in LookML review alongside the warehouse models that feed it
Key Takeaways
- Relationship declarations drive join correctness; getting them wrong produces plausible but inflated numbers
- Symmetric aggregates defend counts and sums against fanout, and pre-aggregated derived tables defend everything else
- PDTs materialise computation into BigQuery and should be partitioned and clustered like any other warehouse table
- Datagroups with SQL trigger values tie cache invalidation to pipeline reality instead of optimistic schedules
Interview Questions
Interview Questions
Q1: What is aggregate awareness and when does it pay off?
It routes queries to the smallest pre-computed aggregate that can answer them; it pays off when a handful of dashboards drive most of the scan volume and their query shapes are stable enough to pre-aggregate.
Q2: How do you prevent fanout in a many-to-many explore?
Pre-aggregate one side into a derived table so the join becomes many-to-one, or use symmetric aggregates keyed on the base view's primary key so deduplication happens during aggregation.
Q3: When is a PDT better than a warehouse materialized view?
When the computation is Looker-specific or spans tables the view cannot reference; for plain aggregations of one BigQuery table, a materialized view is cheaper because BigQuery maintains it without Looker scheduling.
Q4: How do you keep a dashboard cache fresh without polling constantly?
Attach a datagroup whose SQL trigger reads the upstream max loaded_at; the cache only invalidates when new data lands, and max_cache_age bounds staleness during pipeline outages.