Search code examples
redisweb-applicationsquery-optimization

How to use redis better in the situation?


Bg:

Table school:
3 fields:
sid (school id)
s_name(school_name)
s_type(school type)

Table user:
2 fields:
user_id(user id)
name(user name)

Table school_user_relationship:
3 fields:
rid(relationship id)
sid(school id)
user_id(user id)

Explain:

  1. Sid and user_id all exist in the user table, using flag to distinguish between schools and ordinary users. sid and user_id comes from the same global user system, generated by a distributed ID algorithm, and is not self incremented by MySQL.
  2. Table school is an expanded table of school information.
  3. Unique constraint union (sid, user_id) exists in the table school_ user_ relationship
  4. Each record in the relationship table indicates that the sid school has a user_ id user
  5. The maximum number of users for each sid school is 100000
  6. The number of schools is highly uncertain

Question:

How to use Redis to optimize the following two requirements?

  1. Given a sid representing the school and a user_id representing a school or a student, determine the user_id belong to this school(sid)
  2. Given a user representing a (school id or user id) set, which determines whether all users of the set belong to the same school

Data structure: hash? set? bitset?

When not using any optimization, the query time is a bit long. Therefore, optimization is very likely to graduate. But I don't have any plans yet.


Solution

  • Your question makes it sound like you have a relational database (SQL) approach to your problem. Redis, on the other hand, is, at its core, a key-value store, or an associative array. The Java developer kit provides the HashMap data type as a key-value store. Python provides dictionaries and Javascript provides objects with properties. php and perl provide associative arrays

    Let's just call the data structure a dictionary (generic, not python). In all these data structures, and in redis, you store, let us say, your user data by doing something like this pseudo code to put your user's name into a usernames dictionary. ( <-- is the pseudo code assignment operator.)

    usernames[user_id] <-- name
    

    Then, later if you know a user's id you can get the name like so

    name <-- usernames[user_id]
    

    But if you know the user's name and you want their id, you need a separate data structure

    userids[name] <-- user_id
    

    and do this to recover the id by name.

    user_id <-- userids[name]
    

    In SQL you could say SELECT user_id FROM users WHERE name = whatever, but dictionaries don't work that way (not efficiently anyhow).

    Dictionaries can have dictionaries (HASH in redis) and sets (SET in redis) as values. So you can have a dictionary with one entry for each school that is the set of user ids for people enrolled in that school

    A good way to approach a well-performing design for redis is to work out how you would do it in an ordinary programming language with dictionaries.