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

Fix fetch_coin_balance query: coin balance delta #3284

Merged
merged 2 commits into from
Sep 7, 2020

Conversation

vbaranov
Copy link
Member

@vbaranov vbaranov commented Sep 7, 2020

Motivation

Current query in fetch_coin_balance function causes performance issues.

Changelog

In order to address the performance of the query replace

SELECT a0."block_number", a0."value", a0."value_fetched_at", a0."inserted_at", a0."updated_at", a0."address_hash", value - coalesce(lag(value, 1) over (order by block_number), 0), b1."timestamp" FROM "address_coin_balances" AS a0 INNER JOIN "blocks" AS b1 ON a0."block_number" = b1."number" WHERE (a0."address_hash" = '\...') AND (a0."block_number" <= ...) ORDER BY a0."block_number" DESC LIMIT 1;

with

SELECT c."block_number", c."value", c."value_fetched_at", c."inserted_at", c."updated_at", c."address_hash", value - coalesce(lag(value, 1) over (order by block_number), 0) from (select a0."block_number", a0."value", a0."value_fetched_at", a0."inserted_at", a0."updated_at", a0."address_hash" FROM "address_coin_balances" AS a0  WHERE (a0."address_hash" = '\x...') AND (a0."block_number" <= ...) ORDER BY a0."block_number" DESC LIMIT 2) as c ORDER BY c."block_number" DESC LIMIT 1;

Checklist for your Pull Request (PR)

@coveralls
Copy link

coveralls commented Sep 7, 2020

Pull Request Test Coverage Report for Build 17908d004d8ade203aeaa5af8bf73c547593ad91-PR-3284

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+4.9%) to 75.836%

Files with Coverage Reduction New Missed Lines %
lib/block_scout_web/controllers/chain/market_history_chart_controller.ex 2 71.43%
Totals Coverage Status
Change from base Build 716aa438993f01f20c73a46988cabc14d532cebf: 4.9%
Covered Lines: 1927
Relevant Lines: 2541

💛 - Coveralls

@vbaranov vbaranov merged commit 3240e9b into master Sep 7, 2020
@vbaranov vbaranov deleted the vb-fix-coin-balance-query branch September 7, 2020 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants