Skip to content

Commit

Permalink
leverage height index on windows query
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Aug 12, 2019
1 parent 92b2bbc commit f243570
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions db/dcrpg/internal/blockstmts.go
Expand Up @@ -133,9 +133,9 @@ const (
MIN(time) AS time,
COUNT(*) AS blocks_count
FROM blocks
WHERE height BETWEEN $2 AND $3
GROUP BY window_start
ORDER BY window_start DESC
LIMIT $2 OFFSET $3;`
ORDER BY window_start DESC;`

SelectBlocksTimeListingByLimit = `SELECT date_trunc($1, time) as index_value,
MAX(height),
Expand Down
3 changes: 2 additions & 1 deletion db/dcrpg/pgblockchain.go
Expand Up @@ -1628,7 +1628,8 @@ func (pgb *ChainDB) TicketPoolByDateAndInterval(maturityBlock int64,
func (pgb *ChainDB) PosIntervals(limit, offset uint64) ([]*dbtypes.BlocksGroupedInfo, error) {
ctx, cancel := context.WithTimeout(pgb.ctx, pgb.queryTimeout)
defer cancel()
bgi, err := retrieveWindowBlocks(ctx, pgb.db, pgb.chainParams.StakeDiffWindowSize, limit, offset)
bgi, err := retrieveWindowBlocks(ctx, pgb.db,
pgb.chainParams.StakeDiffWindowSize, pgb.Height(), limit, offset)
return bgi, pgb.replaceCancelError(err)
}

Expand Down
8 changes: 6 additions & 2 deletions db/dcrpg/queries.go
Expand Up @@ -1019,8 +1019,12 @@ func RetrieveAllVotesDbIDsHeightsTicketDbIDs(ctx context.Context, db *sql.DB) (i

// retrieveWindowBlocks fetches chunks of windows using the limit and offset provided
// for a window size of chaincfg.Params.StakeDiffWindowSize.
func retrieveWindowBlocks(ctx context.Context, db *sql.DB, windowSize int64, limit, offset uint64) ([]*dbtypes.BlocksGroupedInfo, error) {
rows, err := db.QueryContext(ctx, internal.SelectWindowsByLimit, windowSize, limit, offset)
func retrieveWindowBlocks(ctx context.Context, db *sql.DB, windowSize, currentHeight int64, limit, offset uint64) ([]*dbtypes.BlocksGroupedInfo, error) {
endWindow := currentHeight/windowSize - int64(offset)
startWindow := endWindow - int64(limit) + 1
startHeight := startWindow * windowSize
endHeight := (endWindow+1)*windowSize - 1
rows, err := db.QueryContext(ctx, internal.SelectWindowsByLimit, windowSize, startHeight, endHeight)
if err != nil {
return nil, fmt.Errorf("retrieveWindowBlocks failed: error: %v", err)
}
Expand Down

0 comments on commit f243570

Please sign in to comment.