I was wondering if it's faster to process data in MySQL or a server language like PHP or Python. I'm sure native functions like ORDER will be faster in MySQL due to indexing, caching, etc, but actually calculating the rank (including ties returning multiple entries as having the same rank):
SELECT TORCH_ID, distance AS thisscore, (SELECT COUNT(distinct(distance))+1 FROM torch_info WHERE distance > thisscore) AS rank FROM torch_info ORDER BY rank
...as opposed to just doing a
SELECT TORCH_ID FROM torch_info ORDER BY score DESC and then figure out rank in PHP on the web server.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `torch_info` ADD INDEX `torch_info_idx_distance` (`distance`);
SELECT torch_info.TORCH_ID, torch_info.distance AS thisscore, (SELECT COUNT(DISTINCT (torch_info.distance)) + 1 FROM torch_info WHERE torch_info.distance > thisscore) AS rank FROM torch_info ORDER BY rank