Lost connection during query MySQL












0















I am pretty new to SQL and I recently had a SQL test. In the test, I was given a laptop and was asked to solve some SQL problems in MySQL. The data for testing is a dataset which records when each user logs into a website and it looks like this (the dataset I was given was much larger than this though):



CREATE TABLE table1 (id INT, login TIMESTAMP);
INSERT INTO table1 (id, login) VALUES
(1,'2018-11-01 00:00:01'),
(1,'2018-11-02 11:00:01'),
(1,'2018-11-03 13:00:01'),
(1,'2018-11-04 15:00:01'),
(1,'2018-11-05 17:00:01'),
(2,'2018-10-01 00:00:01'),
(2,'2018-10-11 10:00:01'),
(2,'2018-10-17 09:00:01'),
(2,'2018-11-11 08:00:01'),
(3,'2018-09-03 14:00:01'),
(3,'2018-09-04 15:00:01'),
(3,'2018-09-04 16:00:01'),
(3,'2018-09-06 18:00:01'),
(3,'2018-09-06 19:00:01'),
(3,'2018-09-06 20:00:01'),
(3,'2018-09-13 04:00:01'),
(3,'2018-09-13 14:00:01'),
(3,'2018-09-23 14:00:01'),
(4,'2018-10-03 11:00:01'),
(4,'2018-11-03 12:00:01'),
(5,'2018-09-01 08:00:01'),
(5,'2018-09-02 09:00:01'),
(5,'2018-09-12 09:00:01'),
(5,'2018-09-22 10:00:01'),
(5,'2018-09-22 19:00:01'),
(6,'2018-10-15 06:00:01'),
(6,'2018-10-18 09:00:01'),
(6,'2018-10-18 10:00:01'),
(6,'2018-10-18 11:00:01'),
(6,'2018-10-19 12:00:01');


And I need to figure out the difference in terms of no. of days between a user's first time visit and each of his/her subsequent visits. The result table should look like:



id, date of first time visit, current visit, difference between first time and current



And I wrote the following codes:



SELECT t1.id, t1.first_time, t2.login AS cur_time, DATEDIFF(t2.login, t1.first_time) AS diff
FROM
(SELECT id, min(login) as first_time
FROM table1
GROUP BY id) t1 JOIN table1 t2
ON t1.id=t2.id;


When I tried to run the code using the laptop provided, I got error message saying 'lost connection during query'.



But I didn't have issues running other code like:



SELECT id, min(login) as first_time
FROM table1
GROUP BY id;


However, I got the same error message even for some simple code like this:



SELECT *
FROM
(SELECT id, min(login) as first_time
FROM table1
GROUP BY id) t1;


or when I tried to create view or create temporary table.



Also, when I tried the code on my own laptop, I didnt get the error message.



My question is:




  1. Why did I get the error message when I was using the testing laptop? I tried Google the error message and I guess that's because my query takes too long to run and it exceeds the time limit that the MySQL version of that laptop allows. I guess whoever sets up the test only wants some efficient code?

  2. Is there a way that I can improve my code so that it can still run in situation like this?


Thank you!










share|improve this question



























    0















    I am pretty new to SQL and I recently had a SQL test. In the test, I was given a laptop and was asked to solve some SQL problems in MySQL. The data for testing is a dataset which records when each user logs into a website and it looks like this (the dataset I was given was much larger than this though):



    CREATE TABLE table1 (id INT, login TIMESTAMP);
    INSERT INTO table1 (id, login) VALUES
    (1,'2018-11-01 00:00:01'),
    (1,'2018-11-02 11:00:01'),
    (1,'2018-11-03 13:00:01'),
    (1,'2018-11-04 15:00:01'),
    (1,'2018-11-05 17:00:01'),
    (2,'2018-10-01 00:00:01'),
    (2,'2018-10-11 10:00:01'),
    (2,'2018-10-17 09:00:01'),
    (2,'2018-11-11 08:00:01'),
    (3,'2018-09-03 14:00:01'),
    (3,'2018-09-04 15:00:01'),
    (3,'2018-09-04 16:00:01'),
    (3,'2018-09-06 18:00:01'),
    (3,'2018-09-06 19:00:01'),
    (3,'2018-09-06 20:00:01'),
    (3,'2018-09-13 04:00:01'),
    (3,'2018-09-13 14:00:01'),
    (3,'2018-09-23 14:00:01'),
    (4,'2018-10-03 11:00:01'),
    (4,'2018-11-03 12:00:01'),
    (5,'2018-09-01 08:00:01'),
    (5,'2018-09-02 09:00:01'),
    (5,'2018-09-12 09:00:01'),
    (5,'2018-09-22 10:00:01'),
    (5,'2018-09-22 19:00:01'),
    (6,'2018-10-15 06:00:01'),
    (6,'2018-10-18 09:00:01'),
    (6,'2018-10-18 10:00:01'),
    (6,'2018-10-18 11:00:01'),
    (6,'2018-10-19 12:00:01');


    And I need to figure out the difference in terms of no. of days between a user's first time visit and each of his/her subsequent visits. The result table should look like:



    id, date of first time visit, current visit, difference between first time and current



    And I wrote the following codes:



    SELECT t1.id, t1.first_time, t2.login AS cur_time, DATEDIFF(t2.login, t1.first_time) AS diff
    FROM
    (SELECT id, min(login) as first_time
    FROM table1
    GROUP BY id) t1 JOIN table1 t2
    ON t1.id=t2.id;


    When I tried to run the code using the laptop provided, I got error message saying 'lost connection during query'.



    But I didn't have issues running other code like:



    SELECT id, min(login) as first_time
    FROM table1
    GROUP BY id;


    However, I got the same error message even for some simple code like this:



    SELECT *
    FROM
    (SELECT id, min(login) as first_time
    FROM table1
    GROUP BY id) t1;


    or when I tried to create view or create temporary table.



    Also, when I tried the code on my own laptop, I didnt get the error message.



    My question is:




    1. Why did I get the error message when I was using the testing laptop? I tried Google the error message and I guess that's because my query takes too long to run and it exceeds the time limit that the MySQL version of that laptop allows. I guess whoever sets up the test only wants some efficient code?

    2. Is there a way that I can improve my code so that it can still run in situation like this?


    Thank you!










    share|improve this question

























      0












      0








      0








      I am pretty new to SQL and I recently had a SQL test. In the test, I was given a laptop and was asked to solve some SQL problems in MySQL. The data for testing is a dataset which records when each user logs into a website and it looks like this (the dataset I was given was much larger than this though):



      CREATE TABLE table1 (id INT, login TIMESTAMP);
      INSERT INTO table1 (id, login) VALUES
      (1,'2018-11-01 00:00:01'),
      (1,'2018-11-02 11:00:01'),
      (1,'2018-11-03 13:00:01'),
      (1,'2018-11-04 15:00:01'),
      (1,'2018-11-05 17:00:01'),
      (2,'2018-10-01 00:00:01'),
      (2,'2018-10-11 10:00:01'),
      (2,'2018-10-17 09:00:01'),
      (2,'2018-11-11 08:00:01'),
      (3,'2018-09-03 14:00:01'),
      (3,'2018-09-04 15:00:01'),
      (3,'2018-09-04 16:00:01'),
      (3,'2018-09-06 18:00:01'),
      (3,'2018-09-06 19:00:01'),
      (3,'2018-09-06 20:00:01'),
      (3,'2018-09-13 04:00:01'),
      (3,'2018-09-13 14:00:01'),
      (3,'2018-09-23 14:00:01'),
      (4,'2018-10-03 11:00:01'),
      (4,'2018-11-03 12:00:01'),
      (5,'2018-09-01 08:00:01'),
      (5,'2018-09-02 09:00:01'),
      (5,'2018-09-12 09:00:01'),
      (5,'2018-09-22 10:00:01'),
      (5,'2018-09-22 19:00:01'),
      (6,'2018-10-15 06:00:01'),
      (6,'2018-10-18 09:00:01'),
      (6,'2018-10-18 10:00:01'),
      (6,'2018-10-18 11:00:01'),
      (6,'2018-10-19 12:00:01');


      And I need to figure out the difference in terms of no. of days between a user's first time visit and each of his/her subsequent visits. The result table should look like:



      id, date of first time visit, current visit, difference between first time and current



      And I wrote the following codes:



      SELECT t1.id, t1.first_time, t2.login AS cur_time, DATEDIFF(t2.login, t1.first_time) AS diff
      FROM
      (SELECT id, min(login) as first_time
      FROM table1
      GROUP BY id) t1 JOIN table1 t2
      ON t1.id=t2.id;


      When I tried to run the code using the laptop provided, I got error message saying 'lost connection during query'.



      But I didn't have issues running other code like:



      SELECT id, min(login) as first_time
      FROM table1
      GROUP BY id;


      However, I got the same error message even for some simple code like this:



      SELECT *
      FROM
      (SELECT id, min(login) as first_time
      FROM table1
      GROUP BY id) t1;


      or when I tried to create view or create temporary table.



      Also, when I tried the code on my own laptop, I didnt get the error message.



      My question is:




      1. Why did I get the error message when I was using the testing laptop? I tried Google the error message and I guess that's because my query takes too long to run and it exceeds the time limit that the MySQL version of that laptop allows. I guess whoever sets up the test only wants some efficient code?

      2. Is there a way that I can improve my code so that it can still run in situation like this?


      Thank you!










      share|improve this question














      I am pretty new to SQL and I recently had a SQL test. In the test, I was given a laptop and was asked to solve some SQL problems in MySQL. The data for testing is a dataset which records when each user logs into a website and it looks like this (the dataset I was given was much larger than this though):



      CREATE TABLE table1 (id INT, login TIMESTAMP);
      INSERT INTO table1 (id, login) VALUES
      (1,'2018-11-01 00:00:01'),
      (1,'2018-11-02 11:00:01'),
      (1,'2018-11-03 13:00:01'),
      (1,'2018-11-04 15:00:01'),
      (1,'2018-11-05 17:00:01'),
      (2,'2018-10-01 00:00:01'),
      (2,'2018-10-11 10:00:01'),
      (2,'2018-10-17 09:00:01'),
      (2,'2018-11-11 08:00:01'),
      (3,'2018-09-03 14:00:01'),
      (3,'2018-09-04 15:00:01'),
      (3,'2018-09-04 16:00:01'),
      (3,'2018-09-06 18:00:01'),
      (3,'2018-09-06 19:00:01'),
      (3,'2018-09-06 20:00:01'),
      (3,'2018-09-13 04:00:01'),
      (3,'2018-09-13 14:00:01'),
      (3,'2018-09-23 14:00:01'),
      (4,'2018-10-03 11:00:01'),
      (4,'2018-11-03 12:00:01'),
      (5,'2018-09-01 08:00:01'),
      (5,'2018-09-02 09:00:01'),
      (5,'2018-09-12 09:00:01'),
      (5,'2018-09-22 10:00:01'),
      (5,'2018-09-22 19:00:01'),
      (6,'2018-10-15 06:00:01'),
      (6,'2018-10-18 09:00:01'),
      (6,'2018-10-18 10:00:01'),
      (6,'2018-10-18 11:00:01'),
      (6,'2018-10-19 12:00:01');


      And I need to figure out the difference in terms of no. of days between a user's first time visit and each of his/her subsequent visits. The result table should look like:



      id, date of first time visit, current visit, difference between first time and current



      And I wrote the following codes:



      SELECT t1.id, t1.first_time, t2.login AS cur_time, DATEDIFF(t2.login, t1.first_time) AS diff
      FROM
      (SELECT id, min(login) as first_time
      FROM table1
      GROUP BY id) t1 JOIN table1 t2
      ON t1.id=t2.id;


      When I tried to run the code using the laptop provided, I got error message saying 'lost connection during query'.



      But I didn't have issues running other code like:



      SELECT id, min(login) as first_time
      FROM table1
      GROUP BY id;


      However, I got the same error message even for some simple code like this:



      SELECT *
      FROM
      (SELECT id, min(login) as first_time
      FROM table1
      GROUP BY id) t1;


      or when I tried to create view or create temporary table.



      Also, when I tried the code on my own laptop, I didnt get the error message.



      My question is:




      1. Why did I get the error message when I was using the testing laptop? I tried Google the error message and I guess that's because my query takes too long to run and it exceeds the time limit that the MySQL version of that laptop allows. I guess whoever sets up the test only wants some efficient code?

      2. Is there a way that I can improve my code so that it can still run in situation like this?


      Thank you!







      mysql database-connection






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 7:00









      Data Science BeginnerData Science Beginner

      194




      194
























          1 Answer
          1






          active

          oldest

          votes


















          0














          try this



          New versions of MySQL WorkBench have an option to change specific timeouts.



          For me it was under Edit → Preferences → SQL Editor → DBMS connection read time out (in seconds): 600


          Reference : here






          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',
            autoActivateHeartbeat: false,
            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%2f53332919%2flost-connection-during-query-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









            0














            try this



            New versions of MySQL WorkBench have an option to change specific timeouts.



            For me it was under Edit → Preferences → SQL Editor → DBMS connection read time out (in seconds): 600


            Reference : here






            share|improve this answer




























              0














              try this



              New versions of MySQL WorkBench have an option to change specific timeouts.



              For me it was under Edit → Preferences → SQL Editor → DBMS connection read time out (in seconds): 600


              Reference : here






              share|improve this answer


























                0












                0








                0







                try this



                New versions of MySQL WorkBench have an option to change specific timeouts.



                For me it was under Edit → Preferences → SQL Editor → DBMS connection read time out (in seconds): 600


                Reference : here






                share|improve this answer













                try this



                New versions of MySQL WorkBench have an option to change specific timeouts.



                For me it was under Edit → Preferences → SQL Editor → DBMS connection read time out (in seconds): 600


                Reference : here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 7:04









                Bhargav ChudasamaBhargav Chudasama

                4,3902927




                4,3902927
































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332919%2flost-connection-during-query-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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma