Commit d356c7b0456e7817b6203dcde7df4499d9c1aabe

Authored by comboy
Committed by Julian Langschaedel
1 parent ff61417ba3

fetch existing tx ids when storing block in one query instead of N

Showing 1 changed file with 3 additions and 2 deletions Side-by-side Diff

lib/bitcoin/storage/sequel/sequel_store.rb
... ... @@ -65,9 +65,10 @@
65 65 blk_tx, new_tx, addrs, names = [], [], [], []
66 66  
67 67 # store tx
  68 + existing_tx = Hash[*@db[:tx].filter(hash: blk.tx.map {|tx| tx.hash.htb.blob }).map { |tx| [tx[:hash].hth, tx[:id]] }.flatten]
68 69 blk.tx.each.with_index do |tx, idx|
69   - existing = @db[:tx][hash: tx.hash.htb.blob]
70   - existing ? blk_tx[idx] = existing[:id] : new_tx << [tx, idx]
  70 + existing = existing_tx[tx.hash]
  71 + existing ? blk_tx[idx] = existing : new_tx << [tx, idx]
71 72 end
72 73  
73 74 new_tx_ids = @db[:tx].insert_multiple(new_tx.map {|tx, _| tx_data(tx) })