Skip to content
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

Merged
merged 1 commit into from
Jul 12, 2024

Conversation

disassembler
Copy link
Contributor

List of changes

  • Add / Fix / Change / Remove

Checklist

  • related issue
  • My changes generate no new warnings
  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the changelog
  • I have added tests that prove my fix is effective or that my feature works

Copy link
Contributor

@placek placek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Copy link

@Imran-imtiaz48 Imran-imtiaz48 left a 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

  1. SELECT Statement:

    • The query selects several columns, including voting_anchor.url, voting_anchor.data_hash, and several columns from off_chain_vote_data.
    • The column selection has been updated to replace off_chain_vote_data with off_chain_vote_gov_action_data.
  2. FROM Clause and JOINs:

    • The query joins multiple tables, starting with block, voting_anchor, and then joining off_chain_vote_data on voting_anchor_id.
    • The join on off_chain_vote_data has been updated to join on off_chain_vote_gov_action_data.
  3. GROUP BY Clause:

    • The query groups results by several columns, including stake_address.view, treasury_withdrawal.amount, creator_block.epoch_no, and columns from off_chain_vote_data.

Improvements

  1. Alias Consistency:

    • Ensure that table aliases are used consistently throughout the query. For example, you use off_chain_vote_data and off_chain_vote_gov_action_data. Consider using shorter and more consistent aliases like ovd and ovga.
  2. 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.
  3. JOIN Conditions:

    • Ensure that all join conditions are correctly defined and indexed to improve query performance.
  4. Grouping:

    • Ensure that the GROUP BY clause includes all non-aggregated columns from the SELECT statement to avoid potential errors.

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.

@bosko-m bosko-m merged commit d29b93e into IntersectMBO:develop Jul 12, 2024
@bosko-m bosko-m self-assigned this Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants