Skip to content

Commit 60da22a

Browse files
committed
perf: order by paging_token rather than created_at
1 parent 8d9de2d commit 60da22a

File tree

7 files changed

+49
-6
lines changed

7 files changed

+49
-6
lines changed

sc_audit/db_schema/distribution.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ class DistributionTx(DistributionTxBase, ScBase):
2626

2727

2828
idx_created_at = Index("idx_dtx_created_at", DistributionTx.created_at.desc())
29+
idx_toid = Index("idx_dtx_toid", DistributionTx.paging_token.desc(), unique=True)
2930
idx_recipient = Index("idx_dtx_recipient", DistributionTx.recipient)

sc_audit/db_schema/mint.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ def _credits_remaining_on_date_expression(cls, on_date: dt.date) -> SQLColumnExp
102102

103103

104104
idx_created_at = Index("idx_block_created_at", MintedBlock.created_at)
105+
idx_toid = Index("idx_block_toid", MintedBlock.paging_token.desc(), unique=True)

sc_audit/db_schema/sink.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ def as_dict(self):
7373

7474

7575
idx_created_at = Index("idx_stx_created_at", SinkingTx.created_at.desc())
76+
idx_toid = Index("idx_stx_toid", SinkingTx.paging_token.desc(), unique=True)
7677
idx_funder = Index("idx_stx_funder", SinkingTx.funder)
7778
idx_recipient = Index("idx_stx_recipient", SinkingTx.recipient)

sc_audit/loader/get_latest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ def get_latest_attr(*models: CoreModelName) -> LatestAttr | list[LatestAttr]:
3838
latest_attrs[i] = latest_retirement_date
3939
elif model == 'sink_tx':
4040
latest_sink_tx_cursor = session.scalar(
41-
select(SinkingTx.paging_token).order_by(SinkingTx.created_at.desc())
41+
select(SinkingTx.paging_token).order_by(SinkingTx.paging_token.desc())
4242
)
4343
latest_attrs[i] = increment_paging_token(latest_sink_tx_cursor) or settings.FIRST_SINK_CURSOR
4444
elif model == 'mint_tx':
4545
latest_mint_tx_cursor = session.scalar(
46-
select(MintedBlock.paging_token).order_by(MintedBlock.created_at.desc())
46+
select(MintedBlock.paging_token).order_by(MintedBlock.paging_token.desc())
4747
)
4848
latest_attrs[i] = increment_paging_token(latest_mint_tx_cursor) or settings.FIRST_MINT_CURSOR
4949
elif model == 'dist_tx':
5050
latest_dist_tx_cursor = session.scalar(
51-
select(DistributionTx.paging_token).order_by(DistributionTx.created_at.desc())
51+
select(DistributionTx.paging_token).order_by(DistributionTx.paging_token.desc())
5252
)
5353
latest_attrs[i] = increment_paging_token(latest_dist_tx_cursor) or settings.FIRST_DIST_CURSOR
5454

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""add paging_token indexes
2+
3+
Revision ID: 713e40249322
4+
Revises: 0c2f50c4d517
5+
Create Date: 2025-06-26 14:13:08.992641
6+
7+
"""
8+
from typing import Sequence
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = '713e40249322'
16+
down_revision: str | None = '0c2f50c4d517'
17+
branch_labels: str | Sequence[str] | None = None
18+
depends_on: str | Sequence[str] | None = None
19+
20+
21+
def upgrade() -> None:
22+
with op.batch_alter_table('distribution_txs', schema=None) as batch_op:
23+
batch_op.create_index('idx_dtx_toid', [sa.literal_column('paging_token DESC')], unique=True) # type: ignore[arg-type]
24+
25+
with op.batch_alter_table('minted_blocks', schema=None) as batch_op:
26+
batch_op.create_index('idx_block_toid', [sa.literal_column('paging_token DESC')], unique=True) # type: ignore[arg-type]
27+
28+
with op.batch_alter_table('sinking_txs', schema=None) as batch_op:
29+
batch_op.create_index('idx_stx_toid', [sa.literal_column('paging_token DESC')], unique=True) # type: ignore[arg-type]
30+
31+
32+
def downgrade() -> None:
33+
with op.batch_alter_table('sinking_txs', schema=None) as batch_op:
34+
batch_op.drop_index('idx_stx_toid')
35+
36+
with op.batch_alter_table('minted_blocks', schema=None) as batch_op:
37+
batch_op.drop_index('idx_block_toid')
38+
39+
with op.batch_alter_table('distribution_txs', schema=None) as batch_op:
40+
batch_op.drop_index('idx_dtx_toid')

sc_audit/views/inventory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def view_inventory(omit_empty: bool = False, until_date: dt.date | None = None)
4040
VcsProject.category,
4141
]
4242
columns = block_columns + vcs_project_columns
43-
q_blocks = select(*columns).join(MintedBlock.vcs_project).order_by(MintedBlock.created_at)
43+
q_blocks = select(*columns).join(MintedBlock.vcs_project).order_by(MintedBlock.paging_token)
4444
if omit_empty:
4545
q_blocks = q_blocks.where(credits_remaining_col > 0)
4646

sc_audit/views/sink_status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ def view_sinking_txs(
3030
stx_query = construct_stx_query(for_funder, for_recipient, from_date, before_date, finalized)
3131

3232
if order == 'asc':
33-
stx_query = stx_query.order_by(SinkingTx.created_at.asc())
33+
stx_query = stx_query.order_by(SinkingTx.paging_token.asc())
3434
if cursor:
3535
stx_query = stx_query.where(SinkingTx.paging_token > cursor)
3636
if order == 'desc':
37-
stx_query = stx_query.order_by(SinkingTx.created_at.desc())
37+
stx_query = stx_query.order_by(SinkingTx.paging_token.desc())
3838
if cursor:
3939
stx_query = stx_query.where(SinkingTx.paging_token < cursor)
4040

0 commit comments

Comments
 (0)