Search code examples
pythonperformancehashrediskey-value-store

Storing key-value pairs with or without a hash in redis


I am storing key-value pairs in a Redis database via the Redis-py client. All keys are unique, there are no duplicates. Here's an example :

key = 133735570
value = {"key":133735570,"value":[[141565041,1.2],[22592300,1.0],[162439394,1.0],[19397942,1.0],[79996146,1.0],[84352985,1.0],[123276403,1.0],[18356816,1.0],[113839687,1.0],[16235789,1.0],[144779115,1.0],[94628304,1.0],[134973120,1.0],[138501363,1.0],[34351681,1.0],[80202522,1.0],[81561595,1.0],[18913677,1.0],[130488590,1.0],[128208311,1.0],[93912155,0.5]]}

Would adding a hash (same as the key name) improve performance? For example,

key = 133735570
hash = 133735570
value = {"key":133735570,"value":[[141565041,1.2],[22592300,1.0],[162439394,1.0],[19397942,1.0],[79996146,1.0],[84352985,1.0],[123276403,1.0],[18356816,1.0],[113839687,1.0],[16235789,1.0],[144779115,1.0],[94628304,1.0],[134973120,1.0],[138501363,1.0],[34351681,1.0],[80202522,1.0],[81561595,1.0],[18913677,1.0],[130488590,1.0],[128208311,1.0],[93912155,0.5]]}

My requirement is to look-up keys so as to retrieve the corresponding values from it.


Solution

  • You can try to store your key-value pairs (value part in your example) within hash data structure (where key part of your pair would be stored as hash field and value part as hash value; check out HMSET) which is more flexible for data manipulation and might consume less memory than plain value strings.