optimize cte distinct joins (#797)

* optimize cte distinct joins

* formatting

* formatting
This commit is contained in:
tarikceric 2025-02-11 13:22:16 -08:00 committed by GitHub
parent 66a9f14810
commit 68c1eec35c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 105 additions and 91 deletions

View File

@ -131,9 +131,9 @@ transfers AS (
FROM
{{ ref('silver__transfers') }} a
INNER JOIN (
SELECT DISTINCT tx_id
FROM bridge_to
) d ON d.tx_id = a.tx_id
SELECT DISTINCT tx_id, block_timestamp::date as bt FROM bridge_to) d
ON d.tx_id = a.tx_id
AND d.bt = a.block_timestamp::date
WHERE
a.succeeded
AND {{ between_stmts }}

View File

@ -106,13 +106,18 @@ WITH base AS (
transfers AS (
SELECT
* exclude(index),
split_part(index,'.',1)::int AS index,
nullif(split_part(index,'.',2),'')::int AS inner_index
t.* exclude(index),
split_part(t.index,'.',1)::int AS index,
nullif(split_part(t.index,'.',2),'')::int AS inner_index
FROM
{{ ref('silver__transfers') }}
{{ ref('silver__transfers') }} AS t
INNER JOIN
(SELECT DISTINCT block_timestamp::date AS bt, tx_id FROM base) AS b
ON b.bt = t.block_timestamp::date
AND b.tx_id = t.tx_id
WHERE
{{ between_stmts }}
t.succeeded
AND {{ between_stmts }}
),
deposit_transfers AS (

View File

@ -92,13 +92,18 @@ WITH base AS (
transfers AS (
SELECT
* exclude(index),
split_part(index,'.',1)::int AS index,
nullif(split_part(index,'.',2),'')::int AS inner_index
t.* exclude(index),
split_part(t.index,'.',1)::int AS index,
nullif(split_part(t.index,'.',2),'')::int AS inner_index
FROM
{{ ref('silver__transfers') }}
{{ ref('silver__transfers') }} AS t
INNER JOIN
(SELECT DISTINCT block_timestamp::date AS bt, tx_id FROM base) AS b
ON b.bt = t.block_timestamp::date
AND b.tx_id = t.tx_id
WHERE
{{ between_stmts }}
t.succeeded
AND {{ between_stmts }}
),
deposit_transfers AS (

View File

@ -84,13 +84,17 @@ WITH base AS (
transfers AS (
SELECT
* exclude(index),
split_part(index,'.',1)::int AS index,
nullif(split_part(index,'.',2),'')::int AS inner_index
t.* exclude(index),
split_part(t.index,'.',1)::int AS index,
nullif(split_part(t.index,'.',2),'')::int AS inner_index
FROM
{{ ref('silver__transfers') }}
{{ ref('silver__transfers') }} AS t
INNER JOIN
(SELECT DISTINCT block_timestamp::date AS bt, tx_id FROM base) AS b
ON b.bt = t.block_timestamp::date
AND b.tx_id = t.tx_id
WHERE
succeeded
t.succeeded
AND {{ between_stmts }}
),

View File

@ -120,13 +120,17 @@ WITH base AS (
transfers AS (
SELECT
* exclude(index),
split_part(index,'.',1)::int AS index,
nullif(split_part(index,'.',2),'')::int AS inner_index
t.* exclude(index),
split_part(t.index,'.',1)::int AS index,
nullif(split_part(t.index,'.',2),'')::int AS inner_index
FROM
{{ ref('silver__transfers') }}
{{ ref('silver__transfers') }} AS t
INNER JOIN
(SELECT DISTINCT block_timestamp::date AS bt, tx_id FROM base) AS b
ON b.bt = t.block_timestamp::date
AND b.tx_id = t.tx_id
WHERE
succeeded
t.succeeded
AND {{ between_stmts }}
),

View File

@ -78,13 +78,17 @@ WITH base AS (
transfers AS (
SELECT
* exclude(index),
split_part(index,'.',1)::int AS index,
nullif(split_part(index,'.',2),'')::int AS inner_index
t.* exclude(index),
split_part(t.index,'.',1)::int AS index,
nullif(split_part(t.index,'.',2),'')::int AS inner_index
FROM
{{ ref('silver__transfers') }}
{{ ref('silver__transfers') }} AS t
INNER JOIN
(SELECT DISTINCT block_timestamp::date AS bt, tx_id FROM base) AS b
ON b.bt = t.block_timestamp::date
AND b.tx_id = t.tx_id
WHERE
succeeded
t.succeeded
AND {{ between_stmts }}
),

View File

@ -124,13 +124,17 @@ WITH base AS (
transfers AS (
SELECT
* exclude(index),
split_part(index,'.',1)::int AS index,
nullif(split_part(index,'.',2),'')::int AS inner_index
t.* exclude(index),
split_part(t.index,'.',1)::int AS index,
nullif(split_part(t.index,'.',2),'')::int AS inner_index
FROM
{{ ref('silver__transfers') }}
{{ ref('silver__transfers') }} AS t
INNER JOIN
(SELECT DISTINCT block_timestamp::date AS bt, tx_id FROM base) AS b
ON b.bt = t.block_timestamp::date
AND b.tx_id = t.tx_id
WHERE
succeeded
t.succeeded
AND {{ between_stmts }}
),

View File

@ -40,11 +40,15 @@ AND _inserted_timestamp >= (
),
base_ptb AS (
SELECT
DISTINCT mint AS mint_paid,
account,
DECIMAL
DISTINCT p.mint AS mint_paid,
p.account,
p.DECIMAL
FROM
{{ ref('silver___post_token_balances') }}
{{ ref('silver___post_token_balances') }} AS p
INNER JOIN
(SELECT DISTINCT block_timestamp::date AS bt, tx_id FROM base_candy_machine_events) AS b
ON b.bt = p.block_timestamp::date
AND b.tx_id = p.tx_id
{% if is_incremental() %}
WHERE

View File

@ -91,9 +91,9 @@ transfers AS (
FROM
{{ ref('silver__transfers') }} A
INNER JOIN (
SELECT DISTINCT tx_id
FROM decoded
) d ON d.tx_id = A.tx_id
SELECT DISTINCT tx_id, block_timestamp::date as bt FROM decoded) d
ON d.tx_id = A.tx_id
AND d.bt = A.block_timestamp::date
WHERE
A.succeeded
AND {{ between_stmts }}

View File

@ -95,10 +95,10 @@ transfers AS (
a._inserted_timestamp
FROM
{{ ref('silver__transfers') }} a
INNER JOIN (
SELECT DISTINCT tx_id
FROM decoded
) d ON d.tx_id = a.tx_id
INNER JOIN
(SELECT DISTINCT tx_id, block_timestamp::date as bt FROM decoded) d
ON d.tx_id = a.tx_id
AND d.bt = a.block_timestamp::date
WHERE
a.succeeded
AND {{ between_stmts }}

View File

@ -78,13 +78,10 @@ transfers AS (
NULLIF(SPLIT_PART(INDEX :: text, '.', 2), '') :: INT AS inner_index_1
FROM
{{ ref('silver__transfers') }} A
INNER JOIN (
SELECT
DISTINCT tx_id
FROM
decoded
) d
INNER JOIN
(SELECT DISTINCT tx_id, block_timestamp::date as bt FROM decoded) d
ON d.tx_id = A.tx_id
AND d.bt = A.block_timestamp::date
WHERE
A.succeeded
AND {{ between_stmts }}

View File

@ -151,14 +151,18 @@ WITH decoded AS (
),
base_transfers AS (
SELECT
block_timestamp,
tx_id,
index,
tx_from,
mint,
amount
t.block_timestamp,
t.tx_id,
t.index,
t.tx_from,
t.mint,
t.amount
FROM
{{ ref('silver__transfers') }} t
INNER JOIN
(SELECT DISTINCT block_timestamp::date AS bt, tx_id FROM decoded) AS d
ON d.bt = t.block_timestamp::date
AND d.tx_id = t.tx_id
WHERE
{{ between_stmts }}
),

View File

@ -55,16 +55,12 @@ WITH base AS (
mints AS (
SELECT
a.*
FROM {{ ref('silver__token_mint_actions') }} a
INNER JOIN (
SELECT
DISTINCT tx_id
FROM
base
WHERE
event_type IN ('deposit', 'depositStakeAccount')
) b
ON b.tx_id = a.tx_id
FROM
{{ ref('silver__token_mint_actions') }} a
INNER JOIN
(SELECT DISTINCT tx_id, block_timestamp::date as bt FROM base WHERE event_type IN ('deposit', 'depositStakeAccount')) b
ON b.tx_id = a.tx_id
AND b.bt = a.block_timestamp::date
WHERE
a.succeeded
AND {{ between_stmts }}
@ -76,15 +72,10 @@ transfers AS (
nullif(split_part(a.index,'.',2),'')::int AS inner_index
FROM
{{ ref('silver__transfers') }} a
INNER JOIN (
SELECT
DISTINCT tx_id
FROM
base
WHERE
event_type = 'claim'
) b
ON b.tx_id = a.tx_id
INNER JOIN
(SELECT DISTINCT tx_id, block_timestamp::date as bt FROM base WHERE event_type = 'claim') b
ON b.tx_id = a.tx_id
AND b.bt = a.block_timestamp::date
WHERE
a.succeeded
AND {{ between_stmts }}
@ -94,15 +85,10 @@ sol_balances as (
a.*
FROM
{{ ref('silver__sol_balances') }} a
INNER JOIN (
SELECT
DISTINCT tx_id
FROM
base
WHERE
event_type = 'depositStakeAccount'
) b
ON b.tx_id = a.tx_id
INNER JOIN
(SELECT DISTINCT tx_id, block_timestamp::date as bt FROM base WHERE event_type = 'depositStakeAccount') b
ON b.tx_id = a.tx_id
AND b.bt = a.block_timestamp::date
WHERE
a.succeeded
AND {{ between_stmts }}

View File

@ -116,14 +116,11 @@ transfers AS (
NULLIF(SPLIT_PART(INDEX :: text, '.', 2), '') :: INT AS inner_index_1
FROM
{{ ref('silver__transfers') }} A
INNER JOIN (
SELECT
DISTINCT tx_id
FROM
decoded
) d
INNER JOIN
(SELECT DISTINCT tx_id, block_timestamp::date as bt FROM decoded) d
ON d.tx_id = A.tx_id
WHERE
AND d.bt = A.block_timestamp::date
WHERE
A.succeeded
and {{ between_stmts }}
),