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

Improve last token native price sql query #3269

Merged
merged 6 commits into from
Feb 6, 2025

Conversation

squadgazzz
Copy link
Contributor

@squadgazzz squadgazzz commented Feb 5, 2025

Currently, the query performs awfully for unpopular tokens. Also, the metrics were not updated properly. This PR fixes both issues. I was unable to reproduce it on staging, so another hotfix is required here to improve the tenderly alert execution time.

Execution plan before(auction_id is used first, which causes the performance issue):

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|QUERY PLAN                                                                                                                                                              |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|Limit  (cost=0.56..63.18 rows=1 width=62) (actual time=21566.534..21566.545 rows=0 loops=1)                                                                             |
|  ->  Index Scan Backward using auction_prices_pkey on auction_prices  (cost=0.56..2323157.04 rows=37102 width=62) (actual time=21566.533..21566.539 rows=0 loops=1)|
|        Index Cond: (token = '\xd684b049314ea296b9603f6ee3440380b08c8aa2'::bytea)                                                                                       |
|Planning Time: 0.417 ms                                                                                                                                                 |
|Execution Time: 21566.620 ms                                                                                                                                            |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
                                                                                                                                                                         

And after:

+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|QUERY PLAN                                                                                                                                                          |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|Limit  (cost=0.56..4.50 rows=1 width=62) (actual time=0.186..0.187 rows=0 loops=1)                                                                                  |
|  ->  Index Scan using auction_prices_token_auction_id_idx on auction_prices  (cost=0.56..144221.68 rows=36690 width=62) (actual time=0.185..0.185 rows=0 loops=1)|
|        Index Cond: (token = '\xd684b049314ea296b9603f6ee3440380b08c8aa2'::bytea)                                                                                   |
|Planning Time: 1.618 ms                                                                                                                                             |
|Execution Time: 0.247 ms                                                                                                                                            |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+

@squadgazzz squadgazzz added the hotfix Labels PRs that should be applied into production right away label Feb 5, 2025
@squadgazzz squadgazzz requested a review from a team as a code owner February 5, 2025 19:09
Copy link

github-actions bot commented Feb 5, 2025

Reminder: Please update the DB Readme and comment whether migrations are reversible (include rollback scripts if applicable).


Caused by:

@@ -0,0 +1,2 @@
-- Create index to improve `fetch_latest_token_price` query performance.
CREATE INDEX auction_prices_token_auction_id_idx ON auction_prices (token, auction_id DESC);
Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving a reminder to myself that here is another performance reason why we will probably have to keep auction_prices table, and erase auction prices from competition_auctions. (there are a few more places in the code)

Copy link
Contributor

@m-lord-renkse m-lord-renkse left a comment

Choose a reason for hiding this comment

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

Really nice!!

@squadgazzz squadgazzz enabled auto-merge (squash) February 6, 2025 08:17
@squadgazzz squadgazzz merged commit afe10d0 into main Feb 6, 2025
11 checks passed
@squadgazzz squadgazzz deleted the update-token-metadata-metrics branch February 6, 2025 08:22
@github-actions github-actions bot locked and limited conversation to collaborators Feb 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
hotfix Labels PRs that should be applied into production right away
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants