Question

Is there some how to change only the value of the keys in a leveldb database?

I already have a database with 18 milion rows, and I would like to change keys to add a prefix.

I am using leveldb with node.js. But any solution must fit.

I have already tried to rebuilt this database, but it costs too much time.

Was it helpful?

Solution

The way LevelDB works, every "Put" is a write that creates new state. If you do a "Put" that overwrites an existing value, you'll have two copies until compaction eliminates the older value.

Practically, this means that rebuilding will be the only option.

To make it faster you can try:

  • Scan the existing database, and rebuild by doing a linear scan through the keyspace. Your new data will be inserted in sorted order, which may help speed things up.

  • Insert in batches. Many variants of LevelDB will slow down writes to accommodate compaction. In the stock LevelDB, your writer will sleep 1ms whenever it needs to sleep and this is independent of the number of writes in the batch.

  • Increase the write buffer size. I've found 16MB-32MB to be a good "sweet spot".

  • Consider using HyperLevelDB. We were running into some performance issues with LevelDB with writes, and made some changes that improve performance. You'll see your insertion speed go up, which could help you rebuild faster -- sometimes significantly faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top