Need suggestion on picking fast aggregation framework or solution












0












$begingroup$


To state the problem, this is what I have in the MariaDB/MySQL.



I have the follow table structure, scores



| student_id | x1 | x2 | x3 | y1 | y2 | z1 | z2 | z3 | z4 |
| ---------- | -- | -- | -- | -- | -- | -- | -- | -- | -- |
| 1 | 5 | 3 | 1 | 4 | 3 | 3 | 4 | 1 | 2 |
| 2 | 5 | 3 | 3 | 4 | 2 | 1 | 5 | 2 | 3 |
| 3 | 4 | 2 | 2 | 1 | 1 | 3 | 4 | 3 | 4 |
| 4 | 1 | 4 | 5 | 4 | 5 | 3 | 5 | 5 | 4 |


student_id is the PRIMARY_KEY. Other columns x1, x2... is TINYINT(1) range from 1..5 (inclusive).



The goal:




  • To calculate score of one given student_id against a given list of student_ids.

  • Result set should have two columns: student_id (exclude the given one), and final_score. It must be sorted by final_score DESC.


The formula to calculate final_score of student A against student B.




  • Given: two records of student A and B with list of scores in different categories. For example: category X that have 3 questions, category Y has 2 questions, category Z has 4 questions.


  • First, calculate the average score in each category first.



    AVG_X = ( ABS(XA1 - XB1) + ABS(XA2 - XB2) + ABS(XA3 - XB3) ) / 3



    AVG_Y = ( ABS(YA1 - YB1) + ABS(YA2 - YB2) ) / 2



    AVG_Z = ( ABS(ZA1 - ZB1) + ABS(ZA2 - ZB2) + ABS(ZA3 - ZB3) + ABS(ZA4 - ZB4) ) / 4




where as: AVG is the average value of a category. ABS is to get absolute value.





  • Last, the final score is calculated by:



    FINAL_SCORE = 5 - ((AVG_X + AVG_Y + AVG_Z) / 3)




Based on this, I've made the following SQL query.



SELECT 
f.student_id,
5 - ( avg_cate_x + avg_cate_y + avg_cate_z ) / 3 as final_score
FROM
(
SELECT
s.student_id,
(
ABS(s.x1 - u.x1) + ABS(s.x2 - u.x2) + ABS(s.x3 - u.x3)
) / 3 AS avg_cate_x,
(
ABS(s.y1 - u.y1) + ABS(s.y2 - u.y2)
) / 2 AS avg_cate_y,
(
ABS(s.z1 - u.z1) + ABS(s.z2 - u.z2) + ABS(s.z3 - u.z3) + ABS(s.z4 - u.z4)
) / 4 AS avg_cate_z,

FROM scores AS s

JOIN
( SELECT * FROM scores WHERE scores.student_id = 1 ) AS u
) AS f

ORDER by final_score DESC;


The performance is very slow when I execute it to get final score of student_id = 1 against the rest of table with 50k records, it takes 970ms.



This is the EXPLAIN



+----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+
| 1 | SIMPLE | scores | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using filesort |
| 1 | SIMPLE | s | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 4 | 100.00 | Using index condition |
+----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+


I actually posted the question here to get supports on SQL query optimization.



However, I'm wondering if there is any fast aggregation framework that can help me to achieve this.



I tried this with MongoDB using MapReduce but it doesn't seem to fit the requirements. MongoDB processes very slow on MapReduce, and it requires to store the final results into another collections for reading next time. In term of space/disk, the cost is high because I will need to generate a result column for each user.



To re-start the question, I would like to aggregate the table and output a list of values via request, you can read how it works in above sections.



Any guide, suggestion and recommendation is really appreciate.
Thank you :)










share|improve this question







New contributor




Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    To state the problem, this is what I have in the MariaDB/MySQL.



    I have the follow table structure, scores



    | student_id | x1 | x2 | x3 | y1 | y2 | z1 | z2 | z3 | z4 |
    | ---------- | -- | -- | -- | -- | -- | -- | -- | -- | -- |
    | 1 | 5 | 3 | 1 | 4 | 3 | 3 | 4 | 1 | 2 |
    | 2 | 5 | 3 | 3 | 4 | 2 | 1 | 5 | 2 | 3 |
    | 3 | 4 | 2 | 2 | 1 | 1 | 3 | 4 | 3 | 4 |
    | 4 | 1 | 4 | 5 | 4 | 5 | 3 | 5 | 5 | 4 |


    student_id is the PRIMARY_KEY. Other columns x1, x2... is TINYINT(1) range from 1..5 (inclusive).



    The goal:




    • To calculate score of one given student_id against a given list of student_ids.

    • Result set should have two columns: student_id (exclude the given one), and final_score. It must be sorted by final_score DESC.


    The formula to calculate final_score of student A against student B.




    • Given: two records of student A and B with list of scores in different categories. For example: category X that have 3 questions, category Y has 2 questions, category Z has 4 questions.


    • First, calculate the average score in each category first.



      AVG_X = ( ABS(XA1 - XB1) + ABS(XA2 - XB2) + ABS(XA3 - XB3) ) / 3



      AVG_Y = ( ABS(YA1 - YB1) + ABS(YA2 - YB2) ) / 2



      AVG_Z = ( ABS(ZA1 - ZB1) + ABS(ZA2 - ZB2) + ABS(ZA3 - ZB3) + ABS(ZA4 - ZB4) ) / 4




    where as: AVG is the average value of a category. ABS is to get absolute value.





    • Last, the final score is calculated by:



      FINAL_SCORE = 5 - ((AVG_X + AVG_Y + AVG_Z) / 3)




    Based on this, I've made the following SQL query.



    SELECT 
    f.student_id,
    5 - ( avg_cate_x + avg_cate_y + avg_cate_z ) / 3 as final_score
    FROM
    (
    SELECT
    s.student_id,
    (
    ABS(s.x1 - u.x1) + ABS(s.x2 - u.x2) + ABS(s.x3 - u.x3)
    ) / 3 AS avg_cate_x,
    (
    ABS(s.y1 - u.y1) + ABS(s.y2 - u.y2)
    ) / 2 AS avg_cate_y,
    (
    ABS(s.z1 - u.z1) + ABS(s.z2 - u.z2) + ABS(s.z3 - u.z3) + ABS(s.z4 - u.z4)
    ) / 4 AS avg_cate_z,

    FROM scores AS s

    JOIN
    ( SELECT * FROM scores WHERE scores.student_id = 1 ) AS u
    ) AS f

    ORDER by final_score DESC;


    The performance is very slow when I execute it to get final score of student_id = 1 against the rest of table with 50k records, it takes 970ms.



    This is the EXPLAIN



    +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+
    | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
    +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+
    | 1 | SIMPLE | scores | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using filesort |
    | 1 | SIMPLE | s | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 4 | 100.00 | Using index condition |
    +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+


    I actually posted the question here to get supports on SQL query optimization.



    However, I'm wondering if there is any fast aggregation framework that can help me to achieve this.



    I tried this with MongoDB using MapReduce but it doesn't seem to fit the requirements. MongoDB processes very slow on MapReduce, and it requires to store the final results into another collections for reading next time. In term of space/disk, the cost is high because I will need to generate a result column for each user.



    To re-start the question, I would like to aggregate the table and output a list of values via request, you can read how it works in above sections.



    Any guide, suggestion and recommendation is really appreciate.
    Thank you :)










    share|improve this question







    New contributor




    Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      To state the problem, this is what I have in the MariaDB/MySQL.



      I have the follow table structure, scores



      | student_id | x1 | x2 | x3 | y1 | y2 | z1 | z2 | z3 | z4 |
      | ---------- | -- | -- | -- | -- | -- | -- | -- | -- | -- |
      | 1 | 5 | 3 | 1 | 4 | 3 | 3 | 4 | 1 | 2 |
      | 2 | 5 | 3 | 3 | 4 | 2 | 1 | 5 | 2 | 3 |
      | 3 | 4 | 2 | 2 | 1 | 1 | 3 | 4 | 3 | 4 |
      | 4 | 1 | 4 | 5 | 4 | 5 | 3 | 5 | 5 | 4 |


      student_id is the PRIMARY_KEY. Other columns x1, x2... is TINYINT(1) range from 1..5 (inclusive).



      The goal:




      • To calculate score of one given student_id against a given list of student_ids.

      • Result set should have two columns: student_id (exclude the given one), and final_score. It must be sorted by final_score DESC.


      The formula to calculate final_score of student A against student B.




      • Given: two records of student A and B with list of scores in different categories. For example: category X that have 3 questions, category Y has 2 questions, category Z has 4 questions.


      • First, calculate the average score in each category first.



        AVG_X = ( ABS(XA1 - XB1) + ABS(XA2 - XB2) + ABS(XA3 - XB3) ) / 3



        AVG_Y = ( ABS(YA1 - YB1) + ABS(YA2 - YB2) ) / 2



        AVG_Z = ( ABS(ZA1 - ZB1) + ABS(ZA2 - ZB2) + ABS(ZA3 - ZB3) + ABS(ZA4 - ZB4) ) / 4




      where as: AVG is the average value of a category. ABS is to get absolute value.





      • Last, the final score is calculated by:



        FINAL_SCORE = 5 - ((AVG_X + AVG_Y + AVG_Z) / 3)




      Based on this, I've made the following SQL query.



      SELECT 
      f.student_id,
      5 - ( avg_cate_x + avg_cate_y + avg_cate_z ) / 3 as final_score
      FROM
      (
      SELECT
      s.student_id,
      (
      ABS(s.x1 - u.x1) + ABS(s.x2 - u.x2) + ABS(s.x3 - u.x3)
      ) / 3 AS avg_cate_x,
      (
      ABS(s.y1 - u.y1) + ABS(s.y2 - u.y2)
      ) / 2 AS avg_cate_y,
      (
      ABS(s.z1 - u.z1) + ABS(s.z2 - u.z2) + ABS(s.z3 - u.z3) + ABS(s.z4 - u.z4)
      ) / 4 AS avg_cate_z,

      FROM scores AS s

      JOIN
      ( SELECT * FROM scores WHERE scores.student_id = 1 ) AS u
      ) AS f

      ORDER by final_score DESC;


      The performance is very slow when I execute it to get final score of student_id = 1 against the rest of table with 50k records, it takes 970ms.



      This is the EXPLAIN



      +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+
      | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
      +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+
      | 1 | SIMPLE | scores | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using filesort |
      | 1 | SIMPLE | s | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 4 | 100.00 | Using index condition |
      +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+


      I actually posted the question here to get supports on SQL query optimization.



      However, I'm wondering if there is any fast aggregation framework that can help me to achieve this.



      I tried this with MongoDB using MapReduce but it doesn't seem to fit the requirements. MongoDB processes very slow on MapReduce, and it requires to store the final results into another collections for reading next time. In term of space/disk, the cost is high because I will need to generate a result column for each user.



      To re-start the question, I would like to aggregate the table and output a list of values via request, you can read how it works in above sections.



      Any guide, suggestion and recommendation is really appreciate.
      Thank you :)










      share|improve this question







      New contributor




      Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      To state the problem, this is what I have in the MariaDB/MySQL.



      I have the follow table structure, scores



      | student_id | x1 | x2 | x3 | y1 | y2 | z1 | z2 | z3 | z4 |
      | ---------- | -- | -- | -- | -- | -- | -- | -- | -- | -- |
      | 1 | 5 | 3 | 1 | 4 | 3 | 3 | 4 | 1 | 2 |
      | 2 | 5 | 3 | 3 | 4 | 2 | 1 | 5 | 2 | 3 |
      | 3 | 4 | 2 | 2 | 1 | 1 | 3 | 4 | 3 | 4 |
      | 4 | 1 | 4 | 5 | 4 | 5 | 3 | 5 | 5 | 4 |


      student_id is the PRIMARY_KEY. Other columns x1, x2... is TINYINT(1) range from 1..5 (inclusive).



      The goal:




      • To calculate score of one given student_id against a given list of student_ids.

      • Result set should have two columns: student_id (exclude the given one), and final_score. It must be sorted by final_score DESC.


      The formula to calculate final_score of student A against student B.




      • Given: two records of student A and B with list of scores in different categories. For example: category X that have 3 questions, category Y has 2 questions, category Z has 4 questions.


      • First, calculate the average score in each category first.



        AVG_X = ( ABS(XA1 - XB1) + ABS(XA2 - XB2) + ABS(XA3 - XB3) ) / 3



        AVG_Y = ( ABS(YA1 - YB1) + ABS(YA2 - YB2) ) / 2



        AVG_Z = ( ABS(ZA1 - ZB1) + ABS(ZA2 - ZB2) + ABS(ZA3 - ZB3) + ABS(ZA4 - ZB4) ) / 4




      where as: AVG is the average value of a category. ABS is to get absolute value.





      • Last, the final score is calculated by:



        FINAL_SCORE = 5 - ((AVG_X + AVG_Y + AVG_Z) / 3)




      Based on this, I've made the following SQL query.



      SELECT 
      f.student_id,
      5 - ( avg_cate_x + avg_cate_y + avg_cate_z ) / 3 as final_score
      FROM
      (
      SELECT
      s.student_id,
      (
      ABS(s.x1 - u.x1) + ABS(s.x2 - u.x2) + ABS(s.x3 - u.x3)
      ) / 3 AS avg_cate_x,
      (
      ABS(s.y1 - u.y1) + ABS(s.y2 - u.y2)
      ) / 2 AS avg_cate_y,
      (
      ABS(s.z1 - u.z1) + ABS(s.z2 - u.z2) + ABS(s.z3 - u.z3) + ABS(s.z4 - u.z4)
      ) / 4 AS avg_cate_z,

      FROM scores AS s

      JOIN
      ( SELECT * FROM scores WHERE scores.student_id = 1 ) AS u
      ) AS f

      ORDER by final_score DESC;


      The performance is very slow when I execute it to get final score of student_id = 1 against the rest of table with 50k records, it takes 970ms.



      This is the EXPLAIN



      +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+
      | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
      +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+
      | 1 | SIMPLE | scores | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using filesort |
      | 1 | SIMPLE | s | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 4 | 100.00 | Using index condition |
      +----+-------------+--------+------------+-------+---------------+---------+---------+-------+------+----------+-----------------------+


      I actually posted the question here to get supports on SQL query optimization.



      However, I'm wondering if there is any fast aggregation framework that can help me to achieve this.



      I tried this with MongoDB using MapReduce but it doesn't seem to fit the requirements. MongoDB processes very slow on MapReduce, and it requires to store the final results into another collections for reading next time. In term of space/disk, the cost is high because I will need to generate a result column for each user.



      To re-start the question, I would like to aggregate the table and output a list of values via request, you can read how it works in above sections.



      Any guide, suggestion and recommendation is really appreciate.
      Thank you :)







      bigdata data aggregation






      share|improve this question







      New contributor




      Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 21 hours ago









      Pete HoustonPete Houston

      1013




      1013




      New contributor




      Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Pete Houston is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          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
          });


          }
          });






          Pete Houston is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f44174%2fneed-suggestion-on-picking-fast-aggregation-framework-or-solution%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








          Pete Houston is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Pete Houston is a new contributor. Be nice, and check out our Code of Conduct.













          Pete Houston is a new contributor. Be nice, and check out our Code of Conduct.












          Pete Houston is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Data Science 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.


          Use MathJax to format equations. MathJax reference.


          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%2fdatascience.stackexchange.com%2fquestions%2f44174%2fneed-suggestion-on-picking-fast-aggregation-framework-or-solution%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

          Ponta tanko

          Tantalo (mitologio)

          Erzsébet Schaár