-
Notifications
You must be signed in to change notification settings - Fork 499
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
services/horizon/internal/db2: Add column and index to improve trade_type filter performance #4149
Conversation
ALTER TABLE history_trades ADD trade_type smallint DEFAULT 1 CHECK(trade_type > 0); | ||
UPDATE history_trades SET trade_type = 2 WHERE base_liquidity_pool_id IS NOT NULL OR counter_liquidity_pool_id IS NOT NULL; | ||
CREATE INDEX htrd_by_trade_type ON history_trades USING BTREE(trade_type, history_operation_id, "order"); | ||
|
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.
would it be ok to drop the other liquidity pool id indexes on history_trades? not sure if they come into play on other queries, but they aren't used for deriving trade type query anymore, correct? htrd_by_base_liquidity_pool_id
htrd_by_counter_liquidity_pool_id
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.
there is an endpoint to fetch all trades occurring on a specific liquidity pool (e.g. https://horizon.stellar.org/liquidity_pools/001403bfa898ef65015bb585e07343811aca01e02cc2a6f126a6c842f6d5359c/trades ) . those indexes are used by that endpoint
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.
nice improvement.
I wonder if we can reuse existing fields in the index. 30m migration sounds like a long one (but I think we had similar ones in the past). |
@bartekn I tried adding different indexes without adding a new trade type column, for example:
But unfortunately those indexes were not effective in speeding up the query. |
PR Checklist
PR Structure
otherwise).
services/friendbot
, orall
ordoc
if the changes are broad or impact manypackages.
Thoroughness
.md
files, etc... affected by this change). Take a look in the
docs
folder for a given service,like this one.
Release planning
needed with deprecations, added features, breaking changes, and DB schema changes.
semver, or if it's mainly a patch change. The PR is targeted at the next
release branch if it's not a patch change.
What
Add
trade_type
column which is an enum value that represents whether the trade occurs on the orderbook or against a liquidity pool. Thetrade_type
column is populated during ingestion. This commit also adds an index on thetrade_type
column to make queries for liquidity pool trades fast.Why
Requests to the
/trades?trade_type=liquidity_pool
endpoint timeout because the query to fetch liquidity pool trades is too slow.Known limitations
It took 33 minutes on staging to apply the migration. Also, the size of the
htrd_by_trade_type
index is 3278 mb.I considered removing the
trade_type
filter functionality from the trades endpoint but, within the past week, we have received horizon requests to the trades endpoint withtrade_type
set to orderbook. So, it seems that we would be breaking clients by removing this functionality.