Poor performance on a 'leaderboard' query

Deletion of copy-ctor & copy-assignment - public, private or protected?

How are passwords stolen from companies if they only store hashes?

Is there a term for accumulated dirt on the outside of your hands and feet?

Asserting that Atheism and Theism are both faith based positions

Why didn't Héctor fade away after this character died in the movie Coco?

Could Sinn Fein swing any Brexit vote in Parliament?

How can an organ that provides biological immortality be unable to regenerate?

What is the significance behind "40 days" that often appears in the Bible?

Do I need to be arrogant to get ahead?

Can a medieval gyroplane be built?

PTIJ: Do Irish Jews have "the luck of the Irish"?

What does Jesus mean regarding "Raca," and "you fool?" - is he contrasting them?

Variable completely messes up echoed string

Do US professors/group leaders only get a salary, but no group budget?

Bash - pair each line of file

Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?

Worshiping one God at a time?

Help rendering a complicated sum/product formula

How do hiring committees for research positions view getting "scooped"?

Do I need to consider instance restrictions when showing a language is in P?

Would it be believable to defy demographics in a story?

Loading the leaflet Map in Lightning Web Component

Describing a chess game in a novel

What does "Four-F." mean?



Poor performance on a 'leaderboard' query














0















Consider the following 2 tables:



players:  | rooms: 
id | id
name | type
room_id | embed_code_id
points |


My query needs to fetch the 100 players with the most points from a given room type and embed_code_id.



here's what I have currently:



    select `players`.`id`,
`players`.`points`
from `players`
inner join `rooms` on `players`.`room_id` = `rooms`.`id`
where `rooms`.`embed_code_id` = 678
and `rooms`.`type` = 'single'
order by `players`.`points` desc, `players`.`id` asc
limit 100;


This query results in poor performance (1.5 - 10 seconds, depends on the parameters passed)



Notes:
* Both tables have about 1M records
* when i remove the ORDER BY clause, the time drops below 100ms
* I tried setting an index on players.points & players.id, which didn't help
* I tried setting an index on embed_code_id & type which also didn't help



The EXPLAIN output suggests Using index; Using temporary; Using filesort which i don't know how to fix:



1   SIMPLE  rooms   NULL    ref PRIMARY,embed_code_id,type  type    6   const,const 478797  100.00  Using index; Using temporary; Using filesort
1 SIMPLE players NULL ref room_id room_id 4 playgo.rooms.id 1 100.00 NULL


Can this query be more efficient or i'm missing some index?



Thanks









share



























    0















    Consider the following 2 tables:



    players:  | rooms: 
    id | id
    name | type
    room_id | embed_code_id
    points |


    My query needs to fetch the 100 players with the most points from a given room type and embed_code_id.



    here's what I have currently:



        select `players`.`id`,
    `players`.`points`
    from `players`
    inner join `rooms` on `players`.`room_id` = `rooms`.`id`
    where `rooms`.`embed_code_id` = 678
    and `rooms`.`type` = 'single'
    order by `players`.`points` desc, `players`.`id` asc
    limit 100;


    This query results in poor performance (1.5 - 10 seconds, depends on the parameters passed)



    Notes:
    * Both tables have about 1M records
    * when i remove the ORDER BY clause, the time drops below 100ms
    * I tried setting an index on players.points & players.id, which didn't help
    * I tried setting an index on embed_code_id & type which also didn't help



    The EXPLAIN output suggests Using index; Using temporary; Using filesort which i don't know how to fix:



    1   SIMPLE  rooms   NULL    ref PRIMARY,embed_code_id,type  type    6   const,const 478797  100.00  Using index; Using temporary; Using filesort
    1 SIMPLE players NULL ref room_id room_id 4 playgo.rooms.id 1 100.00 NULL


    Can this query be more efficient or i'm missing some index?



    Thanks









    share

























      0












      0








      0








      Consider the following 2 tables:



      players:  | rooms: 
      id | id
      name | type
      room_id | embed_code_id
      points |


      My query needs to fetch the 100 players with the most points from a given room type and embed_code_id.



      here's what I have currently:



          select `players`.`id`,
      `players`.`points`
      from `players`
      inner join `rooms` on `players`.`room_id` = `rooms`.`id`
      where `rooms`.`embed_code_id` = 678
      and `rooms`.`type` = 'single'
      order by `players`.`points` desc, `players`.`id` asc
      limit 100;


      This query results in poor performance (1.5 - 10 seconds, depends on the parameters passed)



      Notes:
      * Both tables have about 1M records
      * when i remove the ORDER BY clause, the time drops below 100ms
      * I tried setting an index on players.points & players.id, which didn't help
      * I tried setting an index on embed_code_id & type which also didn't help



      The EXPLAIN output suggests Using index; Using temporary; Using filesort which i don't know how to fix:



      1   SIMPLE  rooms   NULL    ref PRIMARY,embed_code_id,type  type    6   const,const 478797  100.00  Using index; Using temporary; Using filesort
      1 SIMPLE players NULL ref room_id room_id 4 playgo.rooms.id 1 100.00 NULL


      Can this query be more efficient or i'm missing some index?



      Thanks









      share














      Consider the following 2 tables:



      players:  | rooms: 
      id | id
      name | type
      room_id | embed_code_id
      points |


      My query needs to fetch the 100 players with the most points from a given room type and embed_code_id.



      here's what I have currently:



          select `players`.`id`,
      `players`.`points`
      from `players`
      inner join `rooms` on `players`.`room_id` = `rooms`.`id`
      where `rooms`.`embed_code_id` = 678
      and `rooms`.`type` = 'single'
      order by `players`.`points` desc, `players`.`id` asc
      limit 100;


      This query results in poor performance (1.5 - 10 seconds, depends on the parameters passed)



      Notes:
      * Both tables have about 1M records
      * when i remove the ORDER BY clause, the time drops below 100ms
      * I tried setting an index on players.points & players.id, which didn't help
      * I tried setting an index on embed_code_id & type which also didn't help



      The EXPLAIN output suggests Using index; Using temporary; Using filesort which i don't know how to fix:



      1   SIMPLE  rooms   NULL    ref PRIMARY,embed_code_id,type  type    6   const,const 478797  100.00  Using index; Using temporary; Using filesort
      1 SIMPLE players NULL ref room_id room_id 4 playgo.rooms.id 1 100.00 NULL


      Can this query be more efficient or i'm missing some index?



      Thanks







      mysql index join order-by





      share












      share










      share



      share










      asked 1 min ago









      Idan PtichiIdan Ptichi

      31




      31






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fdba.stackexchange.com%2fquestions%2f232399%2fpoor-performance-on-a-leaderboard-query%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • 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%2fdba.stackexchange.com%2fquestions%2f232399%2fpoor-performance-on-a-leaderboard-query%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

          ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...