Query a ranking number extremely fast in MySQL?
up vote
0
down vote
favorite
Database has the following columns:
id, uuid, trophies
In order to get the ranking number of a player based on trophies, I currently use this query:
SELECT rank FROM (SELECT (@num := @num +1) as rank, uuid FROM User,
(SELECT @num := 0) x ORDER BY trophies DESC) as TMP WHERE
uuid=UNHEX('7c6d37759dd749349f2c32303a367226');
However, the query takes around 1.3 seconds due to the size of the database (300,000 players).
Is there a more efficient method on getting the rank number in MySQL?
Here's the SHOW CREATE TABLE statement as requested:
CREATE TABLE `User` (`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` binary(16) DEFAULT NULL, `trophies` int(11) DEFAULT NULL,
PRIMARY KEY (`id`), UNIQUE KEY `uuid` (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=57917982 DEFAULT CHARSET=latin1
EXPLAIN statement:

mysql database
|
show 4 more comments
up vote
0
down vote
favorite
Database has the following columns:
id, uuid, trophies
In order to get the ranking number of a player based on trophies, I currently use this query:
SELECT rank FROM (SELECT (@num := @num +1) as rank, uuid FROM User,
(SELECT @num := 0) x ORDER BY trophies DESC) as TMP WHERE
uuid=UNHEX('7c6d37759dd749349f2c32303a367226');
However, the query takes around 1.3 seconds due to the size of the database (300,000 players).
Is there a more efficient method on getting the rank number in MySQL?
Here's the SHOW CREATE TABLE statement as requested:
CREATE TABLE `User` (`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` binary(16) DEFAULT NULL, `trophies` int(11) DEFAULT NULL,
PRIMARY KEY (`id`), UNIQUE KEY `uuid` (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=57917982 DEFAULT CHARSET=latin1
EXPLAIN statement:

mysql database
Please don't use Old comma based Implicit joins and use Modern ExplicitJoinbased syntax
– Madhur Bhaiya
Nov 10 at 4:55
Please share the indexing details on your database. Do you have an integer Primary key ?
– Madhur Bhaiya
Nov 10 at 4:55
What version of MySQL are you using (5.7, 8.0 ...)?.
– wchiquito
Nov 10 at 5:44
@MadhurBhaiya Yes, I have a primary key. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
– Bob
Nov 10 at 5:50
1
@Bob do you have any indexes defined ? Please edit the question and addSHOW CREATE TABLEstatement result. Also,EXPLAINstatement results can be helpful as well.
– Madhur Bhaiya
Nov 10 at 5:56
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Database has the following columns:
id, uuid, trophies
In order to get the ranking number of a player based on trophies, I currently use this query:
SELECT rank FROM (SELECT (@num := @num +1) as rank, uuid FROM User,
(SELECT @num := 0) x ORDER BY trophies DESC) as TMP WHERE
uuid=UNHEX('7c6d37759dd749349f2c32303a367226');
However, the query takes around 1.3 seconds due to the size of the database (300,000 players).
Is there a more efficient method on getting the rank number in MySQL?
Here's the SHOW CREATE TABLE statement as requested:
CREATE TABLE `User` (`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` binary(16) DEFAULT NULL, `trophies` int(11) DEFAULT NULL,
PRIMARY KEY (`id`), UNIQUE KEY `uuid` (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=57917982 DEFAULT CHARSET=latin1
EXPLAIN statement:

mysql database
Database has the following columns:
id, uuid, trophies
In order to get the ranking number of a player based on trophies, I currently use this query:
SELECT rank FROM (SELECT (@num := @num +1) as rank, uuid FROM User,
(SELECT @num := 0) x ORDER BY trophies DESC) as TMP WHERE
uuid=UNHEX('7c6d37759dd749349f2c32303a367226');
However, the query takes around 1.3 seconds due to the size of the database (300,000 players).
Is there a more efficient method on getting the rank number in MySQL?
Here's the SHOW CREATE TABLE statement as requested:
CREATE TABLE `User` (`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` binary(16) DEFAULT NULL, `trophies` int(11) DEFAULT NULL,
PRIMARY KEY (`id`), UNIQUE KEY `uuid` (`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=57917982 DEFAULT CHARSET=latin1
EXPLAIN statement:

mysql database
mysql database
edited Nov 10 at 6:05
asked Nov 10 at 4:30
Bob
5841517
5841517
Please don't use Old comma based Implicit joins and use Modern ExplicitJoinbased syntax
– Madhur Bhaiya
Nov 10 at 4:55
Please share the indexing details on your database. Do you have an integer Primary key ?
– Madhur Bhaiya
Nov 10 at 4:55
What version of MySQL are you using (5.7, 8.0 ...)?.
– wchiquito
Nov 10 at 5:44
@MadhurBhaiya Yes, I have a primary key. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
– Bob
Nov 10 at 5:50
1
@Bob do you have any indexes defined ? Please edit the question and addSHOW CREATE TABLEstatement result. Also,EXPLAINstatement results can be helpful as well.
– Madhur Bhaiya
Nov 10 at 5:56
|
show 4 more comments
Please don't use Old comma based Implicit joins and use Modern ExplicitJoinbased syntax
– Madhur Bhaiya
Nov 10 at 4:55
Please share the indexing details on your database. Do you have an integer Primary key ?
– Madhur Bhaiya
Nov 10 at 4:55
What version of MySQL are you using (5.7, 8.0 ...)?.
– wchiquito
Nov 10 at 5:44
@MadhurBhaiya Yes, I have a primary key. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
– Bob
Nov 10 at 5:50
1
@Bob do you have any indexes defined ? Please edit the question and addSHOW CREATE TABLEstatement result. Also,EXPLAINstatement results can be helpful as well.
– Madhur Bhaiya
Nov 10 at 5:56
Please don't use Old comma based Implicit joins and use Modern Explicit
Join based syntax– Madhur Bhaiya
Nov 10 at 4:55
Please don't use Old comma based Implicit joins and use Modern Explicit
Join based syntax– Madhur Bhaiya
Nov 10 at 4:55
Please share the indexing details on your database. Do you have an integer Primary key ?
– Madhur Bhaiya
Nov 10 at 4:55
Please share the indexing details on your database. Do you have an integer Primary key ?
– Madhur Bhaiya
Nov 10 at 4:55
What version of MySQL are you using (5.7, 8.0 ...)?.
– wchiquito
Nov 10 at 5:44
What version of MySQL are you using (5.7, 8.0 ...)?.
– wchiquito
Nov 10 at 5:44
@MadhurBhaiya Yes, I have a primary key. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
– Bob
Nov 10 at 5:50
@MadhurBhaiya Yes, I have a primary key. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
– Bob
Nov 10 at 5:50
1
1
@Bob do you have any indexes defined ? Please edit the question and add
SHOW CREATE TABLE statement result. Also, EXPLAIN statement results can be helpful as well.– Madhur Bhaiya
Nov 10 at 5:56
@Bob do you have any indexes defined ? Please edit the question and add
SHOW CREATE TABLE statement result. Also, EXPLAIN statement results can be helpful as well.– Madhur Bhaiya
Nov 10 at 5:56
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Found the solution:
This query is at least 10,000x faster.
SELECT 1 + (SELECT COUNT(*) FROM User a WHERE a.trophies > b.trophies) AS rank
FROM User b WHERE uuid = UNHEX('7c6d37759dd749349f2c32303a367226') ORDER BY
rank LIMIT 1;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Found the solution:
This query is at least 10,000x faster.
SELECT 1 + (SELECT COUNT(*) FROM User a WHERE a.trophies > b.trophies) AS rank
FROM User b WHERE uuid = UNHEX('7c6d37759dd749349f2c32303a367226') ORDER BY
rank LIMIT 1;
add a comment |
up vote
0
down vote
accepted
Found the solution:
This query is at least 10,000x faster.
SELECT 1 + (SELECT COUNT(*) FROM User a WHERE a.trophies > b.trophies) AS rank
FROM User b WHERE uuid = UNHEX('7c6d37759dd749349f2c32303a367226') ORDER BY
rank LIMIT 1;
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Found the solution:
This query is at least 10,000x faster.
SELECT 1 + (SELECT COUNT(*) FROM User a WHERE a.trophies > b.trophies) AS rank
FROM User b WHERE uuid = UNHEX('7c6d37759dd749349f2c32303a367226') ORDER BY
rank LIMIT 1;
Found the solution:
This query is at least 10,000x faster.
SELECT 1 + (SELECT COUNT(*) FROM User a WHERE a.trophies > b.trophies) AS rank
FROM User b WHERE uuid = UNHEX('7c6d37759dd749349f2c32303a367226') ORDER BY
rank LIMIT 1;
answered Nov 10 at 18:52
Bob
5841517
5841517
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236003%2fquery-a-ranking-number-extremely-fast-in-mysql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please don't use Old comma based Implicit joins and use Modern Explicit
Joinbased syntax– Madhur Bhaiya
Nov 10 at 4:55
Please share the indexing details on your database. Do you have an integer Primary key ?
– Madhur Bhaiya
Nov 10 at 4:55
What version of MySQL are you using (5.7, 8.0 ...)?.
– wchiquito
Nov 10 at 5:44
@MadhurBhaiya Yes, I have a primary key. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
– Bob
Nov 10 at 5:50
1
@Bob do you have any indexes defined ? Please edit the question and add
SHOW CREATE TABLEstatement result. Also,EXPLAINstatement results can be helpful as well.– Madhur Bhaiya
Nov 10 at 5:56