-
Notifications
You must be signed in to change notification settings - Fork 152
Add index for cancellation timestamp #4018
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
base: main
Are you sure you want to change the base?
Conversation
|
Reminder: Please update the DB Readme and comment whether migrations are reversible (include rollback scripts if applicable).
Caused by: |
squadgazzz
left a comment
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. If we still encounter issues with this query, we should probably start looking into restructuring the orders table.
|
/gemini review |
|
@gemini-code-assist review |
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.
Code Review
This pull request addresses a performance issue by adding a new index on cancellation_timestamp for the orders table, which significantly speeds up a key query. The changes also include removing a now-redundant composite index and, importantly, updating the database README to better document existing indexes. The SQL migration is correct and the documentation improvements are very welcome. I've made one suggestion to further improve the documentation accuracy.
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.
Code Review
This pull request introduces a valuable performance improvement by adding an index on cancellation_timestamp to speed up order fetching. The logic is sound and should resolve the performance issue described. I have two main points of feedback. First, and most critically, the database migration script should use CREATE INDEX CONCURRENTLY and DROP INDEX CONCURRENTLY to avoid locking the orders table in production, which could cause downtime. Second, there's a minor inconsistency in the README documentation update for an existing index. Overall, this is a great change once the migration safety is addressed.
|
This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed. |
Description
Simply fetching the recently updated/added orders for updating the solvable orders cache is surprisingly slow. On mainnet this takes anywhere between 250ms and 3s (resolution of the time buckets is not ideal).
This query basically has the shape
SELECT fields FROM orders WHERE cancellation_timestamp > $1 OR creation_timestamp > $1 OR uid = ANY($2). When running the query withEXPLAIN ANALYZEit became apparent thatcreation_timestamp > $1is very fast butcancellation_timestamp > $1is not. It turned out there was only 1 index usingcancellation_timestampand it was in an indexbtree(creation_timestamp, cancellation_timestamp). This query can only be used to search orders bycreation_timestampvery efficiently but not bycancellation_timestamp.When I added an index
btree(cancellation_timestamp)the query time dropped from ~150ms to ~2ms. It's not completely clear how this translates to the prod environment since my test DB ran the originally query significantly faster than the prod replica but a speed up of 75x will certainly not hurt.Changes
How to test
manual tests on a cloned prod db