Skip to content

Commit 92fd78e

Browse files
authored
refactor(axb-xx): Preliminary Optimization of SQL Views (#115)
* preliminary optimizations to sql views * fixes on delegation events * updated proposal voting history
1 parent f78ee28 commit 92fd78e

File tree

6 files changed

+32
-65
lines changed

6 files changed

+32
-65
lines changed

sql_files/views/approved_proposals.sql

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,14 @@
1212

1313
DROP VIEW IF EXISTS {SCHEMA_NAME}.approved_proposals CASCADE;
1414
CREATE VIEW {SCHEMA_NAME}.approved_proposals AS
15-
WITH execution_outcomes_prep AS (
16-
SELECT
17-
receipt_id
18-
, status
19-
, logs
20-
FROM {SCHEMA_NAME}.execution_outcomes
21-
)
22-
, approve_proposal_action_prep AS (
15+
WITH approve_proposal_action_prep AS (
2316
SELECT
2417
decode(ra.args_base64, 'base64') AS args
2518
, eo.status
2619
, eo.logs
2720
, ra.*
2821
FROM {SCHEMA_NAME}.receipt_actions AS ra
29-
INNER JOIN execution_outcomes_prep AS eo
22+
INNER JOIN {SCHEMA_NAME}.execution_outcomes AS eo
3023
ON ra.receipt_id = eo.receipt_id
3124
AND eo.status = 'SuccessReceiptId'
3225
WHERE

sql_files/views/delegation_events.sql

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@
1616

1717
DROP VIEW IF EXISTS {SCHEMA_NAME}.delegation_events CASCADE;
1818
CREATE VIEW {SCHEMA_NAME}.delegation_events AS
19-
WITH execution_outcomes_prep AS (
20-
SELECT
21-
receipt_id
22-
, status
23-
, logs
24-
FROM {SCHEMA_NAME}.execution_outcomes
25-
)
26-
, receipt_actions_prep AS (
19+
WITH receipt_actions_prep AS (
2720
SELECT
2821
decode(ra.args_base64, 'base64') AS args
2922
, eo.status
3023
, eo.logs
3124
, ra.*
3225
FROM {SCHEMA_NAME}.receipt_actions AS ra
33-
INNER JOIN execution_outcomes_prep AS eo
26+
INNER JOIN {SCHEMA_NAME}.execution_outcomes AS eo
3427
ON ra.receipt_id = eo.receipt_id
3528
AND eo.status IN ('SuccessReceiptId', 'SuccessValue')
3629
WHERE
3730
ra.action_kind = 'FunctionCall'
31+
AND ra.method_name IN ('delegate_all', 'undelegate')
3832
AND ra.receiver_id IN ( --House of Stake contracts
3933
'{VENEAR_CONTRACT_PREFIX}.{HOS_CONTRACT}' --veNEAR contract
4034
, '{VOTING_CONTRACT_PREFIX}.{HOS_CONTRACT}' --Voting contract
@@ -45,8 +39,6 @@ WITH execution_outcomes_prep AS (
4539
ra.*
4640
, ROW_NUMBER() OVER (PARTITION BY ra.predecessor_id ORDER BY ra.block_timestamp DESC) AS row_num
4741
FROM receipt_actions_prep AS ra
48-
WHERE
49-
ra.method_name IN ('delegate_all', 'undelegate')
5042
)
5143
SELECT
5244
MD5(CONCAT(ra.receipt_id, '_',

sql_files/views/proposal_voting_history.sql

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,18 @@
1515

1616
DROP VIEW IF EXISTS {SCHEMA_NAME}.proposal_voting_history CASCADE;
1717
CREATE VIEW {SCHEMA_NAME}.proposal_voting_history AS
18-
WITH execution_outcomes_prep AS (
19-
SELECT
20-
receipt_id
21-
, status
22-
, logs
23-
FROM {SCHEMA_NAME}.execution_outcomes
24-
)
25-
, receipt_actions_prep AS (
18+
WITH receipt_actions_prep AS (
2619
SELECT
2720
decode(ra.args_base64, 'base64') AS args
2821
, ra.*
2922
, eo.logs
3023
FROM {SCHEMA_NAME}.receipt_actions AS ra
31-
INNER JOIN execution_outcomes_prep AS eo
24+
INNER JOIN {SCHEMA_NAME}.execution_outcomes AS eo
3225
ON ra.receipt_id = eo.receipt_id
3326
AND eo.status = 'SuccessValue'
3427
WHERE
3528
ra.action_kind = 'FunctionCall'
29+
AND ra.method_name IN ('create_proposal', 'vote')
3630
AND ra.receiver_id IN ( --House of Stake contracts
3731
'{VENEAR_CONTRACT_PREFIX}.{HOS_CONTRACT}' --veNEAR contract
3832
, '{VOTING_CONTRACT_PREFIX}.{HOS_CONTRACT}' --Voting contract
@@ -124,13 +118,16 @@ WITH execution_outcomes_prep AS (
124118
THEN (safe_json_parse(convert_from(ra.args, 'UTF8'))->>'proposal_id')::NUMERIC
125119
ELSE NULL
126120
END IS NOT NULL
127-
ORDER BY proposal_id ASC, voted_at ASC
128121
)
129122
, latest_vote_per_proposal_and_voter AS (
130-
SELECT
123+
SELECT DISTINCT ON (proposal_id, voter_id)
131124
*
132-
, ROW_NUMBER() OVER (PARTITION BY proposal_id, voter_id ORDER BY voted_at DESC) AS row_num
133125
FROM proposal_voting_history
126+
ORDER BY
127+
proposal_id,
128+
voter_id, -- DISTINCT ON key
129+
voted_at DESC, -- "latest" first
130+
receipt_id DESC -- deterministic tie-breaker
134131
)
135132
SELECT
136133
l.id
@@ -153,6 +150,4 @@ SELECT
153150
FROM latest_vote_per_proposal_and_voter AS l
154151
LEFT JOIN proposal_metadata AS pm
155152
ON l.proposal_id = pm.proposal_id
156-
WHERE
157-
l.row_num = 1
158153
;

sql_files/views/proposals.sql

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,19 @@
1111

1212
DROP VIEW IF EXISTS {SCHEMA_NAME}.proposals CASCADE;
1313
CREATE VIEW {SCHEMA_NAME}.proposals AS
14-
WITH execution_outcomes_prep AS (
15-
SELECT
16-
receipt_id
17-
, status
18-
, logs
19-
, results_json
20-
FROM {SCHEMA_NAME}.execution_outcomes
21-
)
22-
, receipt_actions_prep AS (
14+
WITH receipt_actions_prep AS (
2315
SELECT
2416
decode(ra.args_base64, 'base64') AS args_decoded
25-
, eo.status AS action_status
2617
, eo.logs AS action_logs
2718
, eo.results_json
2819
, ra.*
2920
FROM {SCHEMA_NAME}.receipt_actions AS ra
30-
INNER JOIN execution_outcomes_prep AS eo
21+
INNER JOIN {SCHEMA_NAME}.execution_outcomes AS eo
3122
ON ra.receipt_id = eo.receipt_id
3223
AND eo.status IN ('SuccessReceiptId', 'SuccessValue')
3324
WHERE
3425
ra.action_kind = 'FunctionCall'
26+
AND ra.method_name IN ('create_proposal', 'approve_proposal', 'on_get_snapshot','reject_proposal')
3527
AND ra.receiver_id IN ( --House of Stake contracts
3628
'{VENEAR_CONTRACT_PREFIX}.{HOS_CONTRACT}' --veNEAR contract
3729
, '{VOTING_CONTRACT_PREFIX}.{HOS_CONTRACT}' --Voting contract
@@ -41,7 +33,6 @@ WITH execution_outcomes_prep AS (
4133
SELECT
4234
ra.receipt_id AS id
4335
, ra.receipt_id AS receipt_id
44-
, DATE(ra.block_timestamp) AS proposal_created_date
4536
, ra.block_timestamp AS proposal_created_at
4637

4738
--Proposal Details
@@ -130,6 +121,8 @@ WITH execution_outcomes_prep AS (
130121
FROM receipt_actions_prep AS ra
131122
INNER JOIN approve_proposal AS ap
132123
ON ra.receipt_id = ap.snapshot_receipt_id
124+
WHERE
125+
ra.method_name = 'on_get_snapshot'
133126
)
134127
, reject_proposal as (
135128
SELECT

sql_files/views/registered_voters.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ WITH
3232
receipt_actions_prep AS (
3333
SELECT
3434
decode(ra.args_base64, 'base64') AS args_decoded
35-
, eo.status AS action_status
3635
, eo.logs AS action_logs
3736
, ra.*
3837
FROM {SCHEMA_NAME}.receipt_actions AS ra
@@ -41,6 +40,7 @@ receipt_actions_prep AS (
4140
AND eo.status IN ('SuccessReceiptId', 'SuccessValue')
4241
WHERE
4342
ra.action_kind = 'FunctionCall'
43+
AND ra.method_name IN ('new', 'storage_deposit', 'on_lockup_update', 'deploy_lockup')
4444
AND ra.receiver_id IN ( --House of Stake contracts
4545
'{VENEAR_CONTRACT_PREFIX}.{HOS_CONTRACT}' --veNEAR contract
4646
, '{VOTING_CONTRACT_PREFIX}.{HOS_CONTRACT}' --Voting contract

sql_files/views/user_activities.sql

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@
1616

1717
DROP VIEW IF EXISTS {SCHEMA_NAME}.user_activities CASCADE;
1818
CREATE VIEW {SCHEMA_NAME}.user_activities AS
19-
WITH execution_outcomes_prep AS (
20-
SELECT
21-
receipt_id
22-
, status
23-
, logs
24-
FROM {SCHEMA_NAME}.execution_outcomes
25-
)
26-
, receipt_actions_prep AS (
19+
WITH receipt_actions_prep AS (
2720
SELECT
2821
decode(ra.args_base64, 'base64') AS args_decoded
2922
, CASE
@@ -35,10 +28,11 @@ WITH execution_outcomes_prep AS (
3528
, eo.logs AS logs
3629
, ra.*
3730
FROM {SCHEMA_NAME}.receipt_actions AS ra
38-
LEFT JOIN execution_outcomes_prep AS eo
31+
LEFT JOIN {SCHEMA_NAME}.execution_outcomes AS eo
3932
ON ra.receipt_id = eo.receipt_id
4033
WHERE
4134
ra.action_kind = 'FunctionCall'
35+
AND ra.method_name IN ('on_lockup_deployed', 'lock_near', 'on_lockup_update', 'delegate_all', 'undelegate', 'begin_unlock_near', 'lock_pending_near')
4236
)
4337
--------------------
4438
--Account Creation--
@@ -58,8 +52,8 @@ WITH execution_outcomes_prep AS (
5852
, ra.signer_account_id AS account_id
5953
, ra.predecessor_id AS hos_contract_address
6054
, CASE
61-
WHEN safe_json_parse(CONVERT_FROM(DECODE(ra.args_base64, 'base64'), 'UTF8'))->>'error' IS NULL
62-
THEN (safe_json_parse(CONVERT_FROM(DECODE(ra.args_base64, 'base64'), 'UTF8'))->>'lockup_deposit')::NUMERIC
55+
WHEN safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'error' IS NULL
56+
THEN (safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'lockup_deposit')::NUMERIC
6357
ELSE NULL
6458
END AS near_amount
6559
, CASE
@@ -94,9 +88,9 @@ WITH execution_outcomes_prep AS (
9488
, ra.event_status
9589
, ra.signer_account_id AS account_id
9690
, SUBSTRING(ra.receiver_id FROM POSITION('.' IN ra.receiver_id) + 1) AS hos_contract_address
97-
, CASE
98-
WHEN safe_json_parse(CONVERT_FROM(DECODE(ra.args_base64, 'base64'), 'UTF8'))->>'error' IS NULL
99-
THEN (safe_json_parse(CONVERT_FROM(DECODE(ra.args_base64, 'base64'), 'UTF8'))->>'amount')::NUMERIC
91+
, CASE
92+
WHEN safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'error' IS NULL
93+
THEN (safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'amount')::NUMERIC
10094
ELSE NULL
10195
END AS near_amount
10296
, CASE
@@ -229,8 +223,8 @@ WITH execution_outcomes_prep AS (
229223
, ra.signer_account_id AS account_id
230224
, SUBSTRING(ra.receiver_id FROM POSITION('.' IN ra.receiver_id) + 1) AS hos_contract_address
231225
, CASE
232-
WHEN safe_json_parse(CONVERT_FROM(DECODE(ra.args_base64, 'base64'), 'UTF8'))->>'error' IS NULL
233-
THEN (safe_json_parse(CONVERT_FROM(DECODE(ra.args_base64, 'base64'), 'UTF8'))->>'amount')::NUMERIC
226+
WHEN safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'error' IS NULL
227+
THEN (safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'amount')::NUMERIC
234228
ELSE NULL
235229
END AS near_amount
236230
, NULL::NUMERIC AS locked_near_balance --There ARE NO logs FOR this event_type
@@ -258,8 +252,8 @@ WITH execution_outcomes_prep AS (
258252
, ra.signer_account_id AS account_id
259253
, SUBSTRING(ra.receiver_id FROM POSITION('.' IN ra.receiver_id) + 1) AS hos_contract_address
260254
, CASE
261-
WHEN safe_json_parse(CONVERT_FROM(DECODE(ra.args_base64, 'base64'), 'UTF8'))->>'error' IS NULL
262-
THEN (safe_json_parse(CONVERT_FROM(DECODE(ra.args_base64, 'base64'), 'UTF8'))->>'amount')::NUMERIC
255+
WHEN safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'error' IS NULL
256+
THEN (safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'amount')::NUMERIC
263257
ELSE NULL
264258
END AS near_amount
265259
, NULL::NUMERIC AS locked_near_balance --There ARE NO logs FOR this event_type

0 commit comments

Comments
 (0)