mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-07-07 05:49:59 +02:00
sqldb+invoices: Optimize invoice fetching when the reference is only a hash
The current sqlc GetInvoice query experiences incremental slowdowns during the migration of large invoice databases, primarily due to its complex predicate set. For this specific use case, a streamlined GetInvoiceByHash function provides a more efficient solution, maintaining near-constant lookup times even with extensive table sizes.
This commit is contained in:
@ -255,6 +255,38 @@ func (q *Queries) GetInvoice(ctx context.Context, arg GetInvoiceParams) ([]Invoi
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getInvoiceByHash = `-- name: GetInvoiceByHash :one
|
||||
SELECT i.id, i.hash, i.preimage, i.settle_index, i.settled_at, i.memo, i.amount_msat, i.cltv_delta, i.expiry, i.payment_addr, i.payment_request, i.payment_request_hash, i.state, i.amount_paid_msat, i.is_amp, i.is_hodl, i.is_keysend, i.created_at
|
||||
FROM invoices i
|
||||
WHERE i.hash = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetInvoiceByHash(ctx context.Context, hash []byte) (Invoice, error) {
|
||||
row := q.db.QueryRowContext(ctx, getInvoiceByHash, hash)
|
||||
var i Invoice
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Hash,
|
||||
&i.Preimage,
|
||||
&i.SettleIndex,
|
||||
&i.SettledAt,
|
||||
&i.Memo,
|
||||
&i.AmountMsat,
|
||||
&i.CltvDelta,
|
||||
&i.Expiry,
|
||||
&i.PaymentAddr,
|
||||
&i.PaymentRequest,
|
||||
&i.PaymentRequestHash,
|
||||
&i.State,
|
||||
&i.AmountPaidMsat,
|
||||
&i.IsAmp,
|
||||
&i.IsHodl,
|
||||
&i.IsKeysend,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getInvoiceBySetID = `-- name: GetInvoiceBySetID :many
|
||||
SELECT i.id, i.hash, i.preimage, i.settle_index, i.settled_at, i.memo, i.amount_msat, i.cltv_delta, i.expiry, i.payment_addr, i.payment_request, i.payment_request_hash, i.state, i.amount_paid_msat, i.is_amp, i.is_hodl, i.is_keysend, i.created_at
|
||||
FROM invoices i
|
||||
|
@ -24,6 +24,7 @@ type Querier interface {
|
||||
// from different invoices. It is the caller's responsibility to ensure that
|
||||
// we bubble up an error in those cases.
|
||||
GetInvoice(ctx context.Context, arg GetInvoiceParams) ([]Invoice, error)
|
||||
GetInvoiceByHash(ctx context.Context, hash []byte) (Invoice, error)
|
||||
GetInvoiceBySetID(ctx context.Context, setID []byte) ([]Invoice, error)
|
||||
GetInvoiceFeatures(ctx context.Context, invoiceID int64) ([]InvoiceFeature, error)
|
||||
GetInvoiceHTLCCustomRecords(ctx context.Context, invoiceID int64) ([]GetInvoiceHTLCCustomRecordsRow, error)
|
||||
|
@ -54,6 +54,11 @@ WHERE (
|
||||
GROUP BY i.id
|
||||
LIMIT 2;
|
||||
|
||||
-- name: GetInvoiceByHash :one
|
||||
SELECT i.*
|
||||
FROM invoices i
|
||||
WHERE i.hash = $1;
|
||||
|
||||
-- name: GetInvoiceBySetID :many
|
||||
SELECT i.*
|
||||
FROM invoices i
|
||||
|
Reference in New Issue
Block a user