mysql takes CPU too high a solution for of to add indexes

  • 2020-05-15 02:22:20
  • OfStack

The following is an example of MYSQL taking up CPU high processing. I hope it will be a little enlightening for those of you who are faced with similar problems. 1 generally speaking, MYQL occupies CPU high, which is mostly a problem of database query code and too much database query. So one aspect is to streamline the code, and the other aspect is better to index frequently used code.

This morning, the machine alarm 1 check load 1 straight above 4

top took 1 and found mysql firmly in first place and quite stable
Rebooting mysql for 1 won't work
mysql > show processlist; Under 1
I found that xxx had two query statements (1) directly in the column, and I wiped the site with more than 300,000 records, which was not too big. It could not be a problem of machine performance

I remember reading on the Internet that tmp_table_size is too small to cause this;
So mysql-pxxx-e "show variables;" > tmp
1 is the default 32M.
So weng happily changed it up to 256 and restarted mysql. It was a disappointment

No, we have to come again
select 1 found that it was full of forum comments
So:
mysql > show columns from bbs_message;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| msg_id | int(11) | NO | PRI | NULL | auto_increment |
| board_id | int(11) | NO | MUL | 0 | |
| parent_id | int(11) | NO | MUL | 0 | |
| root_id | int(11) | NO | MUL | 0 | |

1 in show processlist is select * from bbs_message board_id=xxx and parent_id=xxx
And select * from bbs_where parent_id=xxx
As soon as these two ones show up, cpu goes up
So start with the index:
Add two indexes
mysql > alter table bbs_message add index parentid(parent_id);
alter table bbs_message add index chaxunid(board_id,parent_id);
Finally, see 1 index results:
mysql > show index from bbs_message;
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| bbs_message | 0 | PRIMARY | 1 | msg_id | A | 2037 | NULL | NULL | | BTREE | |
| bbs_message | 1 | rootid | 1 | root_id | A | 49 | NULL | NULL | | BTREE | |
| bbs_message | 1 | chaxunid | 1 | board_id | A | 3 | NULL | NULL | | BTREE | |
| bbs_message | 1 | chaxunid | 2 | parent_id | A | 135 | NULL | NULL | | BTREE | |
| bbs_message | 1 | parentid | 1 | parent_id | A | 127 | NULL | NULL | | BTREE | |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
5 rows in set (0.00 sec)
Exit under top 1 load 1 straight at 0.x very stable

Related articles: