-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
list-proposals: fix SQL #1552
list-proposals: fix SQL #1552
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
This SQL script appears to be part of a query that involves data from a blockchain or voting system, as suggested by terms like voting_anchor
, off_chain_vote_data
, gov_action_proposal
, and creator_block
. The script primarily includes a SELECT
statement with various JOIN
operations to combine data from multiple tables, including block
, voting_anchor
, off_chain_vote_data
, and others.
Key Points
-
SELECT Statement:
- The query selects several columns, including
voting_anchor.url
,voting_anchor.data_hash
, and several columns fromoff_chain_vote_data
. - The column selection has been updated to replace
off_chain_vote_data
withoff_chain_vote_gov_action_data
.
- The query selects several columns, including
-
FROM Clause and JOINs:
- The query joins multiple tables, starting with
block
,voting_anchor
, and then joiningoff_chain_vote_data
onvoting_anchor_id
. - The join on
off_chain_vote_data
has been updated to join onoff_chain_vote_gov_action_data
.
- The query joins multiple tables, starting with
-
GROUP BY Clause:
- The query groups results by several columns, including
stake_address.view
,treasury_withdrawal.amount
,creator_block.epoch_no
, and columns fromoff_chain_vote_data
.
- The query groups results by several columns, including
Improvements
-
Alias Consistency:
- Ensure that table aliases are used consistently throughout the query. For example, you use
off_chain_vote_data
andoff_chain_vote_gov_action_data
. Consider using shorter and more consistent aliases likeovd
andovga
.
- Ensure that table aliases are used consistently throughout the query. For example, you use
-
Column Selection:
- Check if all selected columns are necessary for the final result. Reducing the number of columns in the
SELECT
statement can improve performance.
- Check if all selected columns are necessary for the final result. Reducing the number of columns in the
-
JOIN Conditions:
- Ensure that all join conditions are correctly defined and indexed to improve query performance.
-
Grouping:
- Ensure that the
GROUP BY
clause includes all non-aggregated columns from theSELECT
statement to avoid potential errors.
- Ensure that the
Improved Script
Here’s an improved version of your script considering the suggestions above:
SELECT
/* created date */
voting_anchor.url,
encode(voting_anchor.data_hash, 'hex'),
off_chain_vote_gov_action_data.title,
off_chain_vote_gov_action_data.abstract,
off_chain_vote_gov_action_data.motivation,
off_chain_vote_gov_action_data.rationale,
off_chain_vote_data.json,
off_chain_vote_data.jsonb->'{body, references}' as references,
coalesce(SUM(ldd.amount) FILTER (WHERE voting_procedure.vote_text = 'Yes'), 0) * (
/* other selected columns */
FROM
block AS creator_block
JOIN voting_anchor ON creator_block.id = voting_anchor.creator_tx_block_id
JOIN gov_action_proposal ON voting_anchor.id = gov_action_proposal.voting_anchor_id
LEFT JOIN off_chain_vote_gov_action_data ON off_chain_vote_gov_action_data.off_chain_vote_data_id = voting_anchor.id
LEFT JOIN voting_procedure ON gov_action_proposal.id = voting_procedure.gov_action_proposal_id
LEFT JOIN LatestDrepDistr ldd ON ldd.hash_id = voting_procedure.drep_voter AND ldd.rn = 1
GROUP BY
stake_address.view,
treasury_withdrawal.amount,
creator_block.epoch_no,
off_chain_vote_gov_action_data.title,
off_chain_vote_gov_action_data.abstract,
off_chain_vote_gov_action_data.motivation,
off_chain_vote_gov_action_data.rationale,
off_chain_vote_data.json,
gov_action_proposal.index,
creator_tx.hash,
/* other grouped columns */
;
Notes
- Ensure all necessary indexes are in place to optimize the performance of
JOIN
operations. - Validate the query against the actual database schema to ensure there are no mismatches or missing columns.
- Review the business logic to ensure that all selected and grouped columns are aligned with the requirements.
List of changes
Checklist