Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:44:55+00:00 2026-06-13T05:44:55+00:00

Possible Duplicate: Mysql rank function I am making a rank website and each user

  • 0

Possible Duplicate:
Mysql rank function

I am making a rank website and each user will have a unique rank based on level. #1 is the best rank and since there’s 350,000 users, the worst rank will be #350000.

No user can have the same rank as anyone else. Now, when a new user is added, their level is calculated. After calculation a script will “rebuild” the ranks, by going through each user one by one, and calculating their new rank.

Here’s an explanation of the queries:

  • “Id” is the member’s ID in the database
  • “RankNum” is their level. It needs to be renamed.
  • “Rank” is their unique rank, #1 to #350000.

Here’s my current script:

function RebuildRanks() {
    $qD = mysql_query("SELECT `Id`,`RankNum` FROM `Members` ORDER BY `Rank` DESC, `Id` ASC");
    $rowsD = mysql_num_rows($qD);

    $curRank = 0;

    for($x = 1; $x <= $rowsD; $x++) {
        $rowD = mysql_fetch_array($qD);

        $curRank++;
        if($rowD['RankNum'] != $curRank) {
            if($curRank != 0) {
                mysql_query("UPDATE `Members` SET `RankNum`='$curRank' WHERE `Id`='".$rowD['Id']."'");
            }
        }
    }

    return true;
}

With 350,000 users, this tends to run very slow. Essentially, in the database the Rank (“ORDER BY `Rank` DESC”) is their level, so the query will order them. Unfortunately, the rest of the process is slow.

It takes about 97 seconds to process all 350,000 users this way. Is there any possible solution to running this more efficiently and quickly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:44:57+00:00Added an answer on June 13, 2026 at 5:44 am

    Your RebuildRanks() function will continue to be a performance bottleneck for you as your userbase grows; you best bet is to devise a system that avoids re-ranking every time a new user joins. Here is an example solution that might help:

    Add a new table (ScoreMap) that is a sorted hash of your users by their Score (using this term instead of “Rank” to disambiguate).

    CREATE TABLE ScoreMap (
      Score BIGINT NOT NULL, 
      Id BIGINT NOT NULL, 
      UNIQUE INDEX (Score), 
      UNIQUE INDEX (Id)
    );
    

    When a new user joins, compute their Score and insert into this new table. If there’s a collision on Score, you can solve for that (if no two users can have the same RankNum, you must have a tie-breaking function of some sort for Score; move users around in this table as necessary).

    Now query the ScoreMap table:

    SELECT COUNT(*) FROM ScoreMap WHERE Score >= [the score you just calculated]
    

    That is your new user’s RankNum. Everyone with a worse Score than the new user gets their Rank increased by 1, which you can update immediately, periodically, as they log in, put a cache in front of, etc.

    PS: Normalize your Rank scoring function to be a BIGINT between 0 and a very high number (e.g. a trillion). This will help you avoid collisions when inserting into the new table. You want to shoot for an even distribution over this range, since you’re essentially using score as a hashing function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: MySQL password function I am making tables for my homepage's registration fields
Possible Duplicate: mysql custom sort I have an array of IDs: $ids = array(5,
Possible Duplicate: Format mysql datetime with php Well.. I have stored date(c) in Mysql
Possible Duplicate: php/Mysql query with inserting date fails I have a datepicker code below:
Possible Duplicate: MySQL check if a table exists without throwing an exception I have
Possible Duplicate: MySQL date format i have this date which i need to make
Possible Duplicate: mySQL “Rank in Highscore”-Query I was just wondering if there is any
Possible Duplicate: MySQL GUI Programs I have installed MySql 5.5 on my PC (win7),
Possible Duplicate: MySQL delete row from multiple tables I have 5 tables: members member_videos
Possible Duplicate: MySQL dynamic cross tab I have student_record table ------------------------------------------------------------------- student_id | semester

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.