Optimise missing block numbers query (#23)

This commit is contained in:
Edvard Hübinette 2019-03-05 11:52:38 +01:00 committed by GitHub
parent 6ff67f26d1
commit 55b6ad9381
3 changed files with 18 additions and 9 deletions

View File

@ -12,6 +12,9 @@ CREATE TABLE public.headers (
ON DELETE CASCADE
);
-- Index is removed when table is
CREATE INDEX headers_block_number ON public.headers (block_number);
-- +goose Down
DROP TABLE public.headers;

View File

@ -3,7 +3,7 @@
--
-- Dumped from database version 10.5
-- Dumped by pg_dump version 10.4
-- Dumped by pg_dump version 10.5
SET statement_timeout = 0;
SET lock_timeout = 0;
@ -710,6 +710,13 @@ CREATE INDEX block_id_index ON public.transactions USING btree (block_id);
CREATE INDEX block_number_index ON public.blocks USING btree (number);
--
-- Name: headers_block_number; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX headers_block_number ON public.headers USING btree (block_number);
--
-- Name: node_id_index; Type: INDEX; Schema: public; Owner: -
--

View File

@ -19,9 +19,7 @@ package repositories
import (
"database/sql"
"errors"
log "github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
)
@ -61,12 +59,13 @@ func (repository HeaderRepository) GetHeader(blockNumber int64) (core.Header, er
func (repository HeaderRepository) MissingBlockNumbers(startingBlockNumber, endingBlockNumber int64, nodeID string) ([]int64, error) {
numbers := make([]int64, 0)
err := repository.database.Select(&numbers, `SELECT all_block_numbers
FROM (
SELECT generate_series($1::INT, $2::INT) AS all_block_numbers) series
WHERE all_block_numbers NOT IN (
SELECT block_number FROM headers WHERE eth_node_fingerprint = $3
) `,
err := repository.database.Select(&numbers,
`SELECT series.block_number
FROM (SELECT generate_series($1::INT, $2::INT) AS block_number) AS series
LEFT OUTER JOIN (SELECT block_number FROM headers
WHERE eth_node_fingerprint = $3) AS synced
USING (block_number)
WHERE synced.block_number IS NULL`,
startingBlockNumber, endingBlockNumber, nodeID)
if err != nil {
log.Errorf("MissingBlockNumbers failed to get blocks between %v - %v for node %v",