jeudi 5 février 2015

How to recalculate guilds top based on their users xp faster?


The User have XP and GuildId fields. I need to obtain Guild top based on their users XP sum.


So I periodically query:



UPDATE Guild as gg set gg.PersistentXpSum = (SELECT coalesce(sum(coalesce(uu.XP,0)),0) FROM User as uu WHERE uu.GuildId = gg.Id)


And than just SELECT * FROM Guild order by PersistentXpSum desc limit 50.


I have a lot of users and many of them are collaborating in guilds so the first query runs very slowly. Of course I have indexes on XP, GuildId, PersistentXpSum fields.


Except than modifying user current guild PersistentXpSum each time I change user xp what can I do to get this kind of top faster?





Aucun commentaire:

Enregistrer un commentaire