CREATE TABLE AS SELECT kills MySQL

We run a MySQL server with moderate load (200-300 QPS) on fairly powerful hardware (HP DL360 with 8 Xeon cores, 8 GB RAM and RAID10). All tables are innodb and the active dataset fits into the highlighted one innodb_buffer_pool_size.

Our database is normalized and to reduce the number of joins, we use materialized views to smooth the data set. Since data is added in batches several times a day, MV: s is regenerated using CREATE TABLE AS SELECTdynamic updates instead of using complex triggers.

The problem is that sometimes when these queries are executed CREATE(each of which takes from 5 to 50 seconds), other unrelated server requests seem to be queued for the request CREATE, which leads to database immunity.

To (re) create MV: s, we use something like this:

BEGIN TRANSACTION;
DROP TABLE IF EXISTS TableName_TMP;
CREATE TABLE TableName_TMP ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_swedish_ci AS 
    SELECT about100columns, and10Expressions 
    FROM Table1 
    JOIN Table2 ON Table1.fk = Table2.pk 
    /* join up to 13 other tables */
    WHERE ((removed IS NULL OR removed = 0)) 
    ORDER BY created DESC, id ASC;
ALTER TABLE TableName_TMP ADD PRIMARY KEY(id), INDEX(created);
DROP TABLE IF EXISTS TableName;
ALTER TABLE TableName_TMP RENAME TO TableName;
COMMIT;

EXPLAIN from SELECT produces something like:

+----+-------------+------------------+-------------+---------------+------------+---------+------------------------------+-------+-----------------------------+
| id | select_type | table            | type        | possible_keys | key        | key_len | ref                          | rows  | Extra                       |
+----+-------------+------------------+-------------+---------------+------------+---------+    ------------------------------+-------+-----------------------------+
|  1 | SIMPLE      | Table1           | ref_or_null | removed       | removed    | 5       | const                        | 76093 | Using where; Using filesort | 
|  1 | SIMPLE      | Table2           | eq_ref      | PRIMARY       | PRIMARY    | 4       | Table1.fk1                   |     1 |                             | 
|  1 | SIMPLE      | Table3           | eq_ref      | PRIMARY       | PRIMARY    | 4       | Table1.fk2                   |     1 |                             | 
/* More of the same */
|  1 | SIMPLE      | TableN           | eq_ref      | PRIMARY       | PRIMARY    | 4        | TableM.fk                    |     1 | Using index                 | 
|  1 | SIMPLE      | TableX           | eq_ref      | PRIMARY       | PRIMARY    | 4       | TableY.fk                    |     1 |                             | 
/* More of the same */    
+----+-------------+------------------+-------------+---------------+------------+---------+------------------------------+-------+-----------------------------+

Any ideas why CREATE TABLE ASour server is completely overloaded and how can I prevent it?

Hello,

+3
source share

Source: https://habr.com/ru/post/1717500/


All Articles