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

  • SEARCH
  • Home
  • 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 135143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:44:26+00:00 2026-05-11T06:44:26+00:00

I want grouped ranking on a very large table, I’ve found a couple of

  • 0

I want grouped ranking on a very large table, I’ve found a couple of solutions for this problem e.g. in this post and other places on the web. I am, however, unable to figure out the worst case complexity of these solutions. The specific problem consists of a table where each row has a number of points and a name associated. I want to be able to request rank intervals such as 1-4. Here are some data examples:

name | points Ab     14 Ac     14 B      16 C      16 Da     15 De     13 

With these values the following ‘ranking’ is created:

Query id | Rank | Name 1          1      B 2          1      C 3          3      Da 4          4      Ab 5          4      Ac 6          6      De 

And it should be possible to create the following interval on query-id’s: 2-5 giving rank: 1,3,4 and 4.

The database holds about 3 million records so if possible I want to avoid a solution with complexity greater than log(n). There are constantly updates and inserts on the database so these actions should preferably be performed in log(n) complexity as well. I am not sure it’s possible though and I’ve tried wrapping my head around it for some time. I’ve come to the conclusion that a binary search should be possible but I haven’t been able to create a query that does this. I am using a MySQL server.

I will elaborate on how the pseudo code for the filtering could work. Firstly, an index on (points, name) is needed. As input you give a fromrank and a tillrank. The total number of records in the database is n. The pseudocode should look something like this:

Find median point value, count rows less than this value (the count gives a rough estimate of rank, not considering those with same amount of points). If the number returned is greater than the fromrank delimiter, we subdivide the first half and find median of it. We keep doing this until we are pinpointed to the amount of points where fromrank should start. then we do the same within that amount of points with the name index, and find median until we have reached the correct row. We do the exact same thing for tillrank.

The result should be log(n) number of subdivisions. So given the median and count can be made in log(n) time it should be possible to solve the problem in worst case complexity log(n). Correct me if I am wrong.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T06:44:26+00:00Added an answer on May 11, 2026 at 6:44 am

    You need a stored procedure to be able to call this with parameters:

    CREATE TABLE rank (name VARCHAR(20) NOT NULL, points INTEGER NOT NULL);  CREATE INDEX ix_rank_points ON rank(points, name);  CREATE PROCEDURE prc_ranks(fromrank INT, tillrank INT) BEGIN   SET @fromrank = fromrank;   SET @tillrank = tillrank;   PREPARE STMT FROM   '   SELECT  rn, rank, name, points   FROM  (     SELECT  CASE WHEN @cp = points THEN @rank ELSE @rank := @rn + 1 END AS rank,             @rn := @rn + 1 AS rn,             @cp := points,             r.*     FROM (          SELECT @cp := -1, @rn := 0, @rank = 1          ) var,          (          SELECT *          FROM rank          FORCE INDEX (ix_rank_points)          ORDER BY            points DESC, name DESC          LIMIT ?          ) r     ) o   WHERE rn >= ?   ';   EXECUTE STMT USING @tillrank, @fromrank; END;  CALL prc_ranks (2, 5); 

    If you create the index and force MySQL to use it (as in my query), then the complexity of the query will not depend on the number of rows at all, it will depend only on tillrank.

    It will actually take last tillrank values from the index, perform some simple calculations on them and filter out first fromrank values.

    Time of this operation, as you can see, depends only on tillrank, it does not depend on how many records are there.

    I just checked in on 400,000 rows, it selects ranks from 5 to 100 in 0,004 seconds (that is, instantly)

    Important: this only works if you sort on names in DESCENDING order. MySQL does not support DESC clause in the indices, that means that the points and name must be sorted in one order for INDEX SORT to be usable (either both ASCENDING or both DESCENDING). If you want fast ASC sorting by name, you will need to keep negative points in the database, and change the sign in the SELECT clause.

    You may also remove name from the index at all, and perform a final ORDER‘ing without using an index:

    CREATE INDEX ix_rank_points ON rank(points);  CREATE PROCEDURE prc_ranks(fromrank INT, tillrank INT) BEGIN   SET @fromrank = fromrank;   SET @tillrank = tillrank;   PREPARE STMT FROM   '   SELECT  rn, rank, name, points   FROM  (     SELECT  CASE WHEN @cp = points THEN @rank ELSE @rank := @rn + 1 END AS rank,             @rn := @rn + 1 AS rn,             @cp := points,             r.*     FROM (          SELECT @cp := -1, @rn := 0, @rank = 1          ) var,          (          SELECT *          FROM rank          FORCE INDEX (ix_rank_points)          ORDER BY            points DESC          LIMIT ?          ) r     ) o   WHERE rn >= ?   ORDER BY rank, name   ';   EXECUTE STMT USING @tillrank, @fromrank; END; 

    That will impact performance on big ranges, but you will hardly notice it on small ranges.

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

Sidebar

Related Questions

I want to use my dictionary keys for output in a grouped table. This
I have Grouped Table View in which I want to display Contact details. But
Instead of the plain type of table I want a Grouped one in the
I have a grouped table view where I want to reload a particular row.
I working on grouped table view based work in that in that i want
I want to create different custom items for a small grouped table of 4-5
I want to populate data from database in to a table view in grouped
Basically, I want to create a button underneath a grouped table view, like contacts.app
I want to change the background of a grouped table view cell. Say you
I want to set a custom background (image) to my grouped table view. In

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.