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:



enter image description here










share|improve this question























  • 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










  • 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 TABLE statement result. Also, EXPLAIN statement results can be helpful as well.
    – Madhur Bhaiya
    Nov 10 at 5:56














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:



enter image description here










share|improve this question























  • 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










  • 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 TABLE statement result. Also, EXPLAIN statement results can be helpful as well.
    – Madhur Bhaiya
    Nov 10 at 5:56












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:



enter image description here










share|improve this question















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:



enter image description here







mysql database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • 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 TABLE statement result. Also, EXPLAIN statement 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 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 TABLE statement result. Also, EXPLAIN statement 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












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;





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    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;





    share|improve this answer
























      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;





      share|improve this answer






















        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;





        share|improve this answer












        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;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 18:52









        Bob

        5841517




        5841517



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus