count rank in mysql instead of python for speed boost