On Sunday, April 10, 2016 at 1:38:47 PM UTC+1, Xin Liu wrote:
>
> Now, Let's look at the following scenario:
> A website manage 10,000 students, and show top 100 of student's 
> achievement.
> I use mysql storage every student personal information(name email sex and 
> achievement)
> but I have to get every student achievement and caculate Top 100, so I use 
> redis to solve this problem, cache the Top 100 with redis and storage it in 
> sorted set data type.
>
> But now, the problem is: if one student achievement is change, should I 
> update the redis‘s Top 100 every time??
>
> this case may be good, you can think it as a game score Top 100,
>

This looks like a case where relying on some properties of your problem 
domain can help.

One thing you could do is to store the score/grade of the lowest-ranking 
student who is still in the top 100, ie the "threshold" score that puts you 
in to the top 100. You could store the "threshold" value in your redis 
cache as well.

Then each time a student's score changes, you can check whether
  (a) the student's previous score was above the threshold and their score 
has decreased to be below the threshold (ie they've dropped out of the top 
100),
  or  (b) if the student's previous score was below the threshold and is 
now above it (ie they've just entered the top 100).

If either of those is true, you will need to recalculate and cache the top 
100 list. But if neither condition is true (which should be most of the 
time), you can leave the cache untouched.

There are probably other smart things you can do so that you don't have to 
re-execute a MySQL query each time you rebuild the top 100 list, but this 
optimisation should save you a lot of queries.

Hope that helps,

Brendan.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c4c6c600-dbe1-4d09-aa72-2962e82e65e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to