Error Code: 2013. Lost connection to MySQL server during query
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
Changed the value to 6000.
Also unchecked limit rows as putting a limit in every time I want to search the whole data set gets tiresome.
MySQL(Cursors/stored procedures): Error Code: 2013. Lost connection to MySQL server during query 6000.015 sec
The actual select/insert code looks straightforward and should not take that much time.
Do following:
- Create the index outside the procedure. Indexes are created once whereas a procedure can be called many times
- Do not serialize the queries when you do not need to. Use a join in a update instead of a cursor
SQL:
UPDATE telefon
JOIN personalstamm ON personalstamm.persnr = telefon.persnr
SET telefon.gueltig_bis = personalstamm.austritt
WHERE telefon.gueltig_bis is null
getting Error Code: 2013 Lost connection to MySQL server during query while running recursive stored procedure
The normalized way to store hierarchical data is to store a reference to the parent, not store a comma-separated list of the children.
id | parent_id |
---|---|
1 | NULL |
c1 | 1 |
c2 | 1 |
c3 | 1 |
c11 | c1 |
c12 | c1 |
c13 | c1 |
c21 | c2 |
c22 | c2 |
MySQL Stored Procedure fails with Error Code 2013 (Lost connection to MySQL server during query)
Firstly, you are serializing a query that you do not need to. The cursor with 143115 rows causes 1 select and 1 update for each row resulting 143115*2 + 1 = 286231 SQL operations. This will take so much time that your execution times out (error 2013).
Secondly, your cursor logic seems to be bit off. When the cursor loop ends (no more rows to handle), it will turn the exit_loop
variable to true
. Yet you are trying to make last update and turn the exit_loop
back to false
as if the cursor should continue.
You probably can do the same with single SQL operation where you have all the logic:
UPDATE mdl_edulevel2_log l
INNER JOIN mdl_enrollments e ON e.m_relateduserid = l.userid
AND ((l.timecreated >= e.m_timecreated)
AND (l.timecreated <= e.dm_timecreated OR e.dm_timecreated = 0)
)
SET enrollmentid = ifnull(e.enrollmentid, "0 - 0")
WHERE l.userid not in (0, 2, 3);
Related Topics
You Can't Specify Target Table For Update in from Clause
SQL Update from One Table to Another Based on a Id Match
What Is the Meaning of the Prefix N in T-SQL Statements and When Should I Use It
Solutions For Insert or Update on SQL Server
How to Perform an If...Then in an SQL Select
Refactor a Pl/Pgsql Function to Return the Output of Various Select Queries
Two SQL Left Joins Produce Incorrect Result
Inserting Multiple Rows in a Single SQL Query
Create a Cumulative Sum Column in MySQL
Sqlite - Upsert *Not* Insert or Replace
Split Comma Separated Values to Columns in Oracle
How to Concatenate Strings of a String Field in a Postgresql 'Group By' Query
How to Paginate Results in SQL Server