From 12092b3793182933153f9ebb1eac19f0469c7dd0 Mon Sep 17 00:00:00 2001 From: Artem Poltorzhitskiy Date: Thu, 18 Jul 2024 17:07:31 +0200 Subject: [PATCH] Optimization: block stats query (#252) --- internal/storage/postgres/block.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/storage/postgres/block.go b/internal/storage/postgres/block.go index b000b6ff..541bda17 100644 --- a/internal/storage/postgres/block.go +++ b/internal/storage/postgres/block.go @@ -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.*").