Skip to main content
This cookbook contains patterns for optimizing NQL query performance. Apply these techniques to reduce execution time and costs.

Filter early and aggressively

Apply filters before joins

Filter data in subqueries or CTEs before joining to reduce the amount of data processed:

Use price filtering early

Add _price_cpm_usd filters to limit data costs:

Use QUALIFY instead of subqueries

Deduplication with QUALIFY

QUALIFY is more efficient than subquery-based deduplication:

Combined filtering with QUALIFY

Apply multiple window conditions efficiently:

Optimize joins

Use fully qualified column names

Explicit column references help the query optimizer:

Avoid OR in join conditions

OR conditions prevent efficient join algorithms:

Aggregate before joining

Pre-aggregate large tables to reduce join complexity:

Partitioning strategies

Partition by date for time-series data

Monthly partitions for longer retention


Budget management

Set appropriate budget limits

Match budget to expected data volume:

Combine price and date filters

Maximize value by filtering on both price and recency:

Efficient aggregations

Use APPROX_COUNT_DISTINCT instead of COUNT(DISTINCT …)

APPROX_COUNT_DISTINCT is significantly faster and cheaper than COUNT(DISTINCT ...). It returns exact results for low-cardinality columns and near-exact results at scale. Use it for any query that doesn’t require a guaranteed exact count.
Reserve COUNT(DISTINCT ...) for cases where an exact count drives business logic—for example, threshold comparisons in HAVING or CASE WHEN clauses:

Pre-aggregate for common queries

Create summary views for frequently queried aggregations:

Limit result sets

Always use LIMIT during exploration


Efficient date handling

Use DATE_TRUNC for grouping

Avoid functions on indexed columns in WHERE


CTEs for complex queries

Use CTEs for readability and potential optimization


Anti-patterns to avoid

Use explicit column lists (wildcards not supported)

NQL requires explicit column lists—SELECT * and COUNT(*) are not supported. See Explicit Column Selection for why this design choice improves cost transparency, governance, and query optimization.

Don’t use LIKE with leading wildcards when possible

Don’t nest too many subqueries


Query Optimization Guide

Detailed optimization techniques

Join Performance

Understanding join performance implications

Common Queries

Copy-paste query patterns

Complex Joins

Advanced join recipes