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

Add Indexes to candles and orders to optimize queries #1060

Merged
merged 2 commits into from
Feb 9, 2024

Conversation

Christopher-Li
Copy link
Contributor

@Christopher-Li Christopher-Li commented Feb 8, 2024

Changelist

Add Indexes to candles and orders to optimize queries:

  • 150ms -> 3ms for candles
    • select "candles".* from "candles" where "ticker" in ($1) and "resolution" = $2 order by "ticker" DESC, "resolution" DESC, "startedAt" DESC limit $3
  • 2500ms -> 180ms for orders
    • select distinct on ("perpetualId") "funding_index_updates".* from "funding_index_updates" where "effectiveAtHeight" <= $1 order by "perpetualId" asc, "effectiveAtHeight" DESC

Test Plan

Tested in database directly that this reduced latency

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Copy link
Contributor

coderabbitai bot commented Feb 8, 2024

Walkthrough

This update introduces new indexing capabilities to the PostgreSQL database, enhancing query performance for specific tables. By adding indexes on the candles table for the ticker and resolution columns, and on the orders table for clobPairId and subaccountId, the changes aim to optimize data retrieval operations, making the system more efficient in handling requests related to market candles and order pairings.

Changes

File Path Change Summary
.../20240208153050_add_candles_ticket_resolution_index.ts
.../20240208161948_add_orders_clobpairid_subaccountid_index.ts
Adds indexes on candles table for ticker and resolution, and on orders table for clobPairId and subaccountId

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d3273b1 and 0112bd3.
Files selected for processing (2)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240208153050_add_candles_ticket_resolution_index.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240208161948_add_orders_clobpairid_subaccountid_index.ts (1 hunks)
Additional comments: 6
indexer/packages/postgres/src/db/migrations/migration_files/20240208153050_add_candles_ticket_resolution_index.ts (3)
  • 6-6: Ensure the column names ticker and resolution are correctly spelled and exist in the candles table schema.
  • 12-12: Confirm that the index name candles_ticker_resolution_index matches the one created in the up function to ensure proper rollback.
  • 16-18: The configuration transaction: false is correctly set for operations that cannot run inside a transaction, such as CREATE INDEX CONCURRENTLY.
indexer/packages/postgres/src/db/migrations/migration_files/20240208161948_add_orders_clobpairid_subaccountid_index.ts (3)
  • 6-6: Ensure the column names clobPairId and subaccountId are correctly spelled and exist in the orders table schema.
  • 12-12: Confirm that the index name orders_clobpairid_subaccountid_index matches the one created in the up function to ensure proper rollback.
  • 16-18: The configuration transaction: false is correctly set for operations that cannot run inside a transaction, such as CREATE INDEX CONCURRENTLY.

export async function up(knex: Knex): Promise<void> {
// eslint-disable-next-line @typescript-eslint/quotes
await knex.raw(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS orders_clobpairid_subaccountid_index ON orders("clobPairId", "subaccountId");
Copy link
Contributor

@lcwik lcwik Feb 8, 2024

Choose a reason for hiding this comment

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

nit: we are inconsistent with our indices where in some we don't include id (e.g. perpetual_positions_subaccount_perpetual_created_at_index) and in others we use the exact same case and use quotes (e.g. fills_clobPairId_createdAtHeight_partial).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will go with camel case for consistency

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0112bd3 and 326f2b4.
Files selected for processing (1)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240208161948_add_orders_clobpairid_subaccountid_index.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240208161948_add_orders_clobpairid_subaccountid_index.ts

@Christopher-Li Christopher-Li merged commit 450ce8b into main Feb 9, 2024
11 checks passed
@Christopher-Li Christopher-Li deleted the cl_add_indexes branch February 9, 2024 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants