3 ms·
Some things are harder to do in SQL then in code. For example, assigning a rank based on a field in MYSQL. I did this for production code in a rails app, and
by lsiebert 8y ago
Some things are harder to do in SQL then in code. For example, assigning a rank based on a field in MYSQL. I did this for production code in a rails app, and it wasn't just ugly (requiring separate DB calls for variable initialization and output and updating) but it ran slow enough that it had to be scheduled for off peak time, and it requires temporary variables (or you can do it with whole temporary tables, I believe).
https://stackoverflow.com/questions/3333665/rank-function-in-mysql https://stackoverflow.com/questions/3333665/rank-function-in...
That's not to say that I would do it in Ruby if I had it to do over again, I'd probably use Redis, but still, sometimes SQL isn't as fast as other solutions.
I like SQL. Optimizing DB calls to speed up stuff is fun for me. But it's not always the right choice.
(There's probably a clever and fast way to do it in postgres, but DB choices were not mine to make).
edit: Incidentally, if you just need to know 1 record's rank at a time, like getting a player's position on a leaderboard, instead of including each record in the DB, you can do a select on scores (or distinct scores) greater then the current record's score and add 1, and that can be much faster.