Optimal way to iterate over few set of keys

What version of Go are you using (go version)?

$ go version 1.15

What operating system are you using?

Ubuntu 20 LTS

What version of Badger are you using?


Hi, We’re using badger DB as a key-value store in our blockchain client were, I would like to iterate over a few set of keys rather than iterating over all keys, does badger support any logical grouping (similar to tables in SQL DB’s) ?


Hey @rahullenkala , how is your set of keys defined? If they all are prefixed with some common prefix, then you can use iterator.Prefix option.

Hi @Naman
Usually, these are account addresses, don’t have any prefix.

Can you define a set of keys? What do they have in common?

These are 256-bit hashes

If there is no way to distinguish the keys from each other, then you cannot select just them for iteration. You mentioned in your original post:

tables in SQL DB’s

This can absolutely be achieved by adding a prefix to your key. Then to iterate just one “table” you can set the Prefix option on the iterator to that “table” prefix.

Basically, if you have enough information to distinguish what should be in one “table” vs another, you will just want to add that to the beginning of the key. Hashes are tough on their own, and completely negate the usefulness of the prefix features.

1 Like

Thank you. :grinning: