Skip to content

Commit

Permalink
Optimization: block stats query (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Jul 18, 2024
1 parent 3bf2a5c commit 12092b3
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions internal/storage/postgres/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,17 @@ func (b *Blocks) ByHash(ctx context.Context, hash []byte) (block storage.Block,

// ListWithStats -
func (b *Blocks) ListWithStats(ctx context.Context, limit, offset uint64, order sdk.SortOrder) (blocks []*storage.Block, err error) {
subQuery := b.DB().NewSelect().Model(&blocks)
subQuery = postgres.Pagination(subQuery, limit, offset, order)
subQuery := b.DB().NewSelect().
Model(&blocks)
subQuery = limitScope(subQuery, int(limit))
if offset > 0 {
subQuery = subQuery.Offset(int(offset))
}
if order == sdk.SortOrderAsc {
subQuery = subQuery.OrderExpr("time asc, id asc")
} else {
subQuery = subQuery.OrderExpr("time desc, id desc")
}

query := b.DB().NewSelect().
ColumnExpr("block.*").
Expand Down

0 comments on commit 12092b3

Please sign in to comment.