There was noticed that Cloud SQL (MySQL) consumed to much CPU (~100%) during several days.
Firstly, I decided that we have many user online or background jobs. But it was regular workload, nothing special. Query Insights showed CPU under 20% for all users in DB and for all DBs in the MySQL instance.
Secondly, I decided that we have connections or memory leaks. Also, nothing special.
And, lastly, what I checked was output from SHOW FULL PROCESSLIST. I noticed some process which lasts too long (~8days) (see Time field) and State was 'statistics'. This workload was not shown in the CPU chart of Query Insights but belongs to my sql user. And, actually, this is strange for me.
The 'Info' field contains a 'SELECT ...' query. I was able to kill such processes. And the CPU came to normal levels.
UPDATED:
- https://bugs.mysql.com/bug.php?id=20932
- https://bugs.mysql.com/bug.php?id=26339
- https://bugs.mysql.com/bug.php?id=21153
- https://www.linuxtopia.org/online_books/database_guides/mysql_5.1_database_reference_guide/controlling-optimizer.html?
Description
Looks like, it was combination of two bugs (1) and (2). It seems that the current implementation of search query by tags, with dynamic (depends on amount of selected tags' categories) combination of 'IN ()' clauses where greater than 2 tags and 'INNER JOIN' clauses, leads to performance degradation of MySQL query optimizer.
Solution (in progress)
- Replace
INNER JOIN
withINTERSECT
(MySQL 8.0.31). - Replace
IN (<values>)
withUNION DISTINCT
. - Add suitable indexes (with help of
EXPLAIN ANALYSE
).
SELECT
and theCREATE TABLE
for any table in that query.