Skip to content

Conversation

@eric-laurello
Copy link
Collaborator

@eric-laurello eric-laurello commented Jan 7, 2026

Summary

This PR addresses multiple performance anti-patterns identified through a comprehensive codebase analysis. 29 files modified with fixes targeting compilation efficiency, query performance, and partition pruning.

Changes

Critical Fixes

File Issue Solution
silver__transactions.sql 4 sequential run_query() calls Converted temp tables to CTEs, reduced to 1 call (75% reduction)
silver__nft_sales_combined.sql MAX(_inserted_timestamp) repeated 3x Cached as Jinja variable
silver_observability__transactions_completeness.sql Implicit Cartesian JOIN ON 1=1 Changed to explicit CROSS JOIN
silver__hourly_prices_priority.sql LOWER() on both JOIN sides (non-sargable) Pre-computed in CTEs
core__ez_transfers.sql LOWER() + DATE_TRUNC() in JOINs Pre-computed in base CTEs

Partition Pruning (24 models)

Added incremental_predicates = ["dynamic_range_predicate", "block_timestamp::DATE"] to:

  • 17 DEX swap models: animeswap, auxexchange, cetus, hippo, liquidswap, pancake, sushi, thala, thala_v2, aires, batswap, cellana, cetus_clmm, hyperfluid, thala_v0, tsunami, tapp
  • 3 NFT sales models: mercato, souffl3, topaz
  • 2 NFT mint models: mints_combined, mints_v2
  • 2 bridge models: wormhole_transfers, layerzero_transfers

Benefits

  1. Reduced compilation time - Fewer database round-trips during dbt compilation
  2. Partition pruning enabled - Snowflake can skip irrelevant partitions during incremental runs
  3. Improved JOIN performance - Index-friendly conditions via pre-computed columns
  4. Clearer code intent - Explicit CROSS JOIN documents intentional behavior

Test Plan

  • Run dbt compile to verify all models compile without errors
  • Run dbt run --select silver__transactions to test core model changes
  • Run incremental builds on modified DEX/NFT/bridge models
  • Verify query plans show partition pruning where incremental_predicates added

claude added 4 commits January 7, 2026 14:16
Comprehensive analysis of dbt models revealing:
- 84% of incremental models missing incremental_predicates
- 58 run_query() calls causing N+1-like patterns
- Non-sargable JOINs using LOWER() function
- Cartesian product in observability model
- Repeated MAX(_inserted_timestamp) subqueries
Performance improvement for the core transactions model:
- Convert temp tables to CTEs (blocks_source, tx_batch_source)
- Replace Jinja for-loop date generation with SQL subquery
- Keep only essential run_query for max_inserted_timestamp

Benefits:
- 75% reduction in compilation-time database round-trips
- Snowflake CTE optimization materializes intermediate results
- Cleaner, more maintainable code structure
Performance optimizations applied:

1. silver__nft_sales_combined.sql
   - Cache MAX(_inserted_timestamp) as Jinja variable (was repeated 3x)
   - Add incremental_predicates for partition pruning

2. silver_observability__transactions_completeness.sql
   - Replace implicit Cartesian (JOIN ON 1=1) with explicit CROSS JOIN

3. silver__hourly_prices_priority.sql
   - Pre-compute LOWER() values in CTEs to enable index usage
   - Eliminates non-sargable join conditions

4. core__ez_transfers.sql
   - Pre-compute LOWER() and DATE_TRUNC() in base CTEs
   - Eliminates per-row function calls during joins

5. Add incremental_predicates to 24 silver models:
   - 17 DEX swap models
   - 3 NFT sales models
   - 2 NFT mint models
   - 2 bridge transfer models

Benefits:
- Reduced compilation-time database round-trips
- Enabled partition pruning for incremental runs
- Improved join performance via index usage
Updated executive summary to document:
- 29 files modified total
- 6 categories of fixes applied
- Specific models and improvements listed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants