Error Code: 2013. Lost connection to MySQL server during query
There has been a human error which lead to barcodes being used more than once and now i'm tasked to identify all the duplicates. Trying to do just that i've been having trouble with the querys timing out after 600 seconds and i need some help to optimize it or to find a new and better approach to the problem.
Localhost:
- XAMPP Version: 7.4.29 Control Panel Version: 3.3.0
- Windows Version: Enterprise 64-bit
- MySQL Workbench 8.0 CE
- 10.4.24-MariaDB
- my.ini XAMPP
DB server:
- CentOS 6.5
- MySQL Ver 14.14 Distrib 5.5.37, for Linux (x86_64) using readline 5.1
Things i've tried to make it work:
- I narrowed down the records to roughly 24'000'000 and moved them to a freshly indexed database on localhost
- I wrote a function with the hope it might be faster
- I tried to run the query with the command line tool instead of MySQL Workbench on localhost
- I tried to run the query directly on the server with the command line tool
Sadly nothing helped. Here some additional info.
The create statement of the table
The function:
CREATE DEFINER=`root`@`localhost` FUNCTION `findDuplicateRawcodesByOrders`(p_auftragsnummer INT) RETURNS varchar(256) CHARSET latin1
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE v_rawcode VARCHAR(255);
DECLARE v_count INT;
DECLARE v_result VARCHAR(255);
SELECT rawcode, COUNT(*) INTO v_rawcode, v_count
FROM ins1_labelresults
WHERE auftragsnummer = p_auftragsnummer
AND position != 0
AND error = 'ok'
GROUP BY rawcode
HAVING COUNT(*) > 1;
IF v_rawcode IS NOT NULL THEN
SET v_result = CONCAT('Duplicate rawcode found for auftragsnummer ', p_auftragsnummer, ': ', v_rawcode, ' (', v_count, ' times)');
ELSE
SET v_result = CONCAT('No duplicate rawcode found for auftragsnummer ', p_auftragsnummer);
END IF;
RETURN v_result;
END
The query:
SELECT auftragsnummer, findDuplicateRawcodesByOrders(auftragsnummer) AS result
FROM ins1_labelresults
WHERE auftragsnummer IN (
'651395', '655190', '655253', '655577',
'655827', '655828', '657505', '657729',
'657861', '659545', '660282'
);
Thanks in advance!