1

I have an old project that I am trying to upgrade to a newer version of SQL. It is currently running MySql 5.5 and is working just fine. I have migrated som test data to MariaDB 10.5.9, but when I try running a query (which works fine on MySql 5.5), MariaDB crashes.

The query is quite big, and uses WHERE IN extensively. Currently I am unable to refactor the query sadly, so I am instead trying to figure out what causes the crash.

It has 3 WHERE IN. First is 24 items, second is 696 and third is 2. If I remove just one item from either the first or second WHERE IN, it works are returns data instantly. The answers table is MyISAM

The error I am getting

SQL Error [08S01]: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

And the query

SELECT
    definition_id,
    answer AS value
FROM
    answers
WHERE
    definition_id IN (...)
    AND respondent_id in (...)
    AND context IN (1, 0)
LIMIT 50

I have already tried changing max_allowed_packet to something higher (it was 16MB in 5.5), but it sadly changes nothing.

Result of EXPLAIN SQL_NO_CACHE (if I remove alot of the data in the WHERE IN to avoid crash)

id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY ALL 824 Using temporary; Using filesort
2 DERIVED s1 range definition_respondent_context,respondent_id definition_respondent_context 12 824 Using index condition; Using temporary; Using filesort
2 DERIVED s2 eq_ref definition_respondent_context,respondent_id definition_respondent_context 12 const,database_name.s1.respondent_id,const 1

EDIT: I managed to get it working by using a join on the definitions table

SELECT
    a.definition_id,
    a.answer AS value
FROM
    answers AS a
JOIN definitions AS d ON a.definition_id = d.id
WHERE
    d.id IN (...)
    AND a.respondent_id in (...)
    AND a.context IN (1, 0)
LIMIT 50
Sinnbeck
  • 617
  • 4
  • 20
  • 56
  • Additional information request. RAM size, # cores, any SSD or NVME devices on MySQL Host server? Post on pastebin.com and share the links. From your SSH login root, Text results of: B) SHOW GLOBAL STATUS; after minimum 24 hours UPTIME C) SHOW GLOBAL VARIABLES; D) SHOW FULL PROCESSLIST; AND Optional very helpful information, if available includes - htop OR top for most active apps, ulimit -a for a Linux/Unix list of limits, iostat -xm 5 3 for IOPS by device and core/cpu count, for server workload tuning analysis to provide suggestions. – Wilson Hauck Mar 01 '21 at 19:36
  • Please post EXPLAIN SELECT SQL_NO_CACHE (your failing query); for analysis of missing indexes. – Wilson Hauck Mar 01 '21 at 19:37
  • The `SQL_NO_CACHE` has been deprecated for some time now...., see: https://stackoverflow.com/questions/58584596/mysql-8-warning-sql-no-cache-is-deprecated/66491613#66491613 – Luuk Mar 05 '21 at 11:23
  • This bug is reported at https://jira.mariadb.org/browse/MDEV-21603 and appears to exist in MariaDB v10.3 and up, not in v10.2. – lmeurs Apr 12 '21 at 08:44

1 Answers1

1

One solution to your problem is to change your design/approach such that you don't have WHERE IN (...) clauses with 500-1000 items in them. For one thing, it's doubtful that you would ever have some application passing so many parameters back to your database instance. So, assuming this data is not coming from the outside, then it should be possible to maintain it in a separate table. Assuming you had two tables for this, your query could then become:

SELECT a.definition_id, a.answer AS value
FROM answers a
INNER JOIN definitions d
    ON d.id = a.definition_id
INNER JOIN respondents r
    ON r.id = a.respondent_id
WHERE
    context IN (1, 0)
-- ORDER BY <something>
LIMIT 50;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • It is a legacy code base, so it would take a lot to change it everywhere. And the query only takes 21ms on MySql 5.5. So my hope was to find a way to upgrade mysql without having to rewrite the whole thing – Sinnbeck Feb 25 '21 at 09:57
  • Then consider upgrading to MySQL 8+, or the latest version of MariaDB. I have given as an answer what I believe the best long term fix to be. – Tim Biegeleisen Feb 25 '21 at 10:05
  • I actually managed to get this working, by just joining the definitions table and using the WHERE IN on that – Sinnbeck Mar 05 '21 at 10:20