Search code examples
performanceredisnosql-aggregationnosql

MAX() SQL Equivalent on Redis


I'm new on Redis, and now I have problem to improve my stat application. The current SQL to generate the statistic is here:

SELECT MIN(created_at), MAX(created_at) FROM table ORDER BY id DESC limit 10000

It will return MIN and MAX value from created_at field.

I have read about RANGE and SCORING on Redis, seem them can be used to solve this problem. But I still confused about SCORING for last 10000 records. Are they can be used to solve this problem, or is there another way to solve this problem using Redis?

Regards


Solution

  • Your target appears to be somewhat unclear - are you looking to store all the records in Redis? If so, what other columns does the table table have and what other queries do you run against it?

    I'll take your question at face value, but note that in most NoSQL databases (Redis included) you need to store your data according to how you plan on fetching it. Assuming that you want to get the min/max creation dates of the last 10K records, I suggest that you keep them in a Sorted Set. The Sorted Set's members will be the unique id and their scores will be the creation date (use the epoch value), for example, rows with ids 1, 2 & 3 were created at dates 10, 100 & 1000 respectively:

    ZADD table 10 1 100 2 1000 3 ...
    

    Getting the minimal creation date is easy now - just do ZRANGE table 0 0 WITHSCORES - and the max is just a ZRANGE table -1 -1 WITHSCORES away. The only "tricky" part is making sure that the Sorted Set is kept updated, so for every new record you'll need to remove the lowest id from the set and add the new one. In pseudo Python code this would look something like the following:

    def updateMinMaxSortedSet(id, date):
        count = redis.zcount('table')
        if count > 10000:
            redis.zrem('table', id-10000)
        redis.zadd('table', id, date)