Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find key by value

Tags:

redis

The think I'm trying to implement is an id table. Basically it has the structure (user_id, lecturer_id) which user_id refers to the primary key in my User table and lecturer_id refers to the primary key of my Lecturer table.

I'm trying to implement this in redis but if I set the key as User's primary id, when I try to run a query like get all the records with lecturer id=5 since lecturer is not the key, but value I won't be able to reach it in O(1) time.

How can I form a structure like the id table I mentioned in above, or Redis does not support that?

like image 385
Ali Avatar asked Oct 05 '12 12:10

Ali


People also ask

Can I get key from value?

Get key from a value by using list comprehension. Instead of using index() method, we can use list comprehension to get the keys associated with the given value. To find the key, we will create a list of keys which have associated values equal to the given value using list comprehension as follows.

How do I find the value of my key?

To check if a key-value pair exists in a dictionary, i.e., if a dictionary has/contains a pair, use the in operator and the items() method. Specify a tuple (key, value) . Use not in to check if a pair does not exist in a dictionary.

How do I search for a key in Python?

To simply check if a key exists in a Python dictionary you can use the in operator to search through the dictionary keys like this: pets = {'cats': 1, 'dogs': 2, 'fish': 3} if 'dogs' in pets: print('Dogs found!') # Dogs found! A dictionary can be a convenient data structure for counting the occurrence of items.

How do you get a specific value from a dictionary in Python?

get() method is used in Python to retrieve a value from a dictionary. dict. get() returns None by default if the key you specify cannot be found. With this method, you can specify a second parameter that will return a custom default value if a key is not found.


1 Answers

One of the things you learn fast while working with redis is that you get to design your data structure around your accessing needs, specially when it comes to relations (it's not a relational database after all)

There is no way to search by "value" with a O(1) time complexity as you already noticed, but there are ways to approach what you describe using redis. Here's what I would recommend:

  • Store your user data by user id (in e.g. a hash) as you are already doing.
  • Have an additional set for each lecturer id containing all user ids that correspond to the lecturer id in question.

This might seem like duplicating the data of the relation, since your user data would have to store the lecture id, and your lecture data would store user ids, but that's the (tiny) price to pay if one is to build relations in a no-relational data store like redis. In practical terms this works well; memory is rarely a bottleneck for small-ish data-sets (think thousands of ids).

To get a better picture at how are people using redis to model applications with relations, I recommend reading Design and implementation of a simple Twitter clone and the source code of Lamernews, both of which are written by redis author Salvatore Sanfilippo.

like image 124
Mahn Avatar answered Oct 10 '22 03:10

Mahn