Skip to content

Commit

Permalink
perf(InvoiceGeneration) - add index on invoices to speed up fetching …
Browse files Browse the repository at this point in the history
…the highest organization_sequential_id (#3146)

## description

This index reduces the time to fetch the highest id from around 40ms to
0.4ms.
  • Loading branch information
nudded authored Feb 6, 2025
1 parent d5ce018 commit 7f9a60b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
21 changes: 11 additions & 10 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,17 @@ def status_changed_to_finalized?
#
# Indexes
#
# index_invoices_on_customer_id (customer_id)
# index_invoices_on_customer_id_and_sequential_id (customer_id,sequential_id) UNIQUE
# index_invoices_on_issuing_date (issuing_date)
# index_invoices_on_number (number)
# index_invoices_on_organization_id (organization_id)
# index_invoices_on_payment_overdue (payment_overdue)
# index_invoices_on_ready_to_be_refreshed (ready_to_be_refreshed) WHERE (ready_to_be_refreshed = true)
# index_invoices_on_self_billed (self_billed)
# index_invoices_on_sequential_id (sequential_id)
# index_invoices_on_status (status)
# idx_on_organization_id_organization_sequential_id_2387146f54 (organization_id,organization_sequential_id DESC)
# index_invoices_on_customer_id (customer_id)
# index_invoices_on_customer_id_and_sequential_id (customer_id,sequential_id) UNIQUE
# index_invoices_on_issuing_date (issuing_date)
# index_invoices_on_number (number)
# index_invoices_on_organization_id (organization_id)
# index_invoices_on_payment_overdue (payment_overdue)
# index_invoices_on_ready_to_be_refreshed (ready_to_be_refreshed) WHERE (ready_to_be_refreshed = true)
# index_invoices_on_self_billed (self_billed)
# index_invoices_on_sequential_id (sequential_id)
# index_invoices_on_status (status)
#
# Foreign Keys
#
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20250205184611_add_index_on_invoices_org_seq_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class AddIndexOnInvoicesOrgSeqId < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
add_index :invoices, [:organization_id, :organization_sequential_id],
order: {organization_sequential_id: :desc},
algorithm: :concurrently,
if_not_exists: true,
include: %i[self_billed]
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f9a60b

Please sign in to comment.