[pybsddb] Making calls to keys() more efficient (or, how to do inequality gets on keys?)

Denis Papathanasiou denis.papathanasiou at gmail.com
Wed Jun 9 21:46:30 CEST 2010


I know that using keys() can be inefficient, as per the warnings in the 
docs.

I do need to calls keys() in the following scenario, though, so I was 
wondering if there was anything I could do to make it run faster:

A db that stores temporary session data has as its key a guid, prefixed 
by a unix timestamp. The associated value is just another guid 
identifier (which is unrelated to the one in the key).

Since the session data is temporary, I have a function which examines 
all the keys, and removes every key/value pair older than n seconds 
(it's a simple calc using the unix timestamp prefix in the key).

I realize that I could restructure the db so that the unix timestamp is 
part of the value, and by adding a secondary index on it, I could get 
all the keys older than n.

But, restructuring it and doing all the tests will take time, so in the 
interim, is there anything I can do to make using keys() more effective?

Alternatively, is there any way to simply get all the keys greater than n?

It seems beyond the scope of bdb, but I was curious if I'd missed anything.



More information about the pybsddb mailing list