Query editor
-- version 3-- EXPLAIN ANALYZE-- EXPLAINWITH-- Define native tokens with direct VALUES syntax (more efficient than map_from_entries)native_tokens AS (SELECT * FROM (VALUES('ethereum', 0x),('avalanche_c', 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7),('polygon', 0x0000000000000000000000000000000000001010),('optimism', 0x4200000000000000000000000000000000000006),('bnb', 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c),('fantom', 0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83),('base', 0x4200000000000000000000000000000000000006),('arbitrum', 0x82af49447d8a07e3bd95bd0d56f35241523fbab1)) AS t(chain, native_token)),-- Base data with resolved tokens (single scan)base_data AS (SELECTr.tx_hash,r.time,r.token0,r.amount0,r.token1,r.amount1,r.chain,r.txn_type,r.nfpm,DATE_TRUNC('hour', r.time) AS hour_time,DATE_TRUNC('month', r.time) AS block_month,-- Resolve token addresses in a single scanCASE WHEN r.token0 = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeTHEN n.native_token ELSE r.token0 END AS actual_token0,CASE WHEN r.token1 = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeTHEN n.native_token ELSE r.token1 END AS actual_token1FROM dune.krystal.result_manual_zaps_raws rJOIN native_tokens n ON r.chain = n.chain-- exclude some token on BNB which has noise in pricing dataWHERE r.token0 NOT IN (0x0e7779e698052f8fe56c415c3818fcf89de9ac6d, -- ULTI0x11Cee2f0F707810c98382724fb6D5e14972366dC, -- MAGA0x1188d953aFC697C031851169EEf640F23ac8529C -- AUCTION) AND r.token1 NOT IN (0x0e7779e698052f8fe56c415c3818fcf89de9ac6d, -- ULTI0x11Cee2f0F707810c98382724fb6D5e14972366dC, -- MAGA0x1188d953aFC697C031851169EEf640F23ac8529C -- AUCTION))-- Final query with optimized joins to pre-filtered dataSELECTb.time,b.chain,(CAST(b.amount0 AS double) / POWER(10, COALESCE(p0.decimals, 18)) * COALESCE(p0.price, 0)+ CAST(b.amount1 AS double) / POWER(10, COALESCE(p1.decimals, 18)) * COALESCE(p1.price, 0)) AS volume,