Mysql CPU-intensive solution (add index) _mysql

Source: Internet
Author: User
Tags mul
The following is an example of the CPU-intensive processing of MySQL, hoping to be a bit of a inspiration to friends who encounter similar problems. Generally speaking, MYQL occupies a high CPU, most of the database query code problems, too much query database. So on the one hand, to streamline the code, on the other hand it is best to index frequently used code.

Wake up this morning. Machine alarm Check the load has been more than 4

Top a little bit found MySQL stable first and pretty steady I Rub
Restart MySQL, no.
Mysql> show Processlist;
found that the XXX site has two query statements have been in the column, I wipe the station also on the 30多万条 record volume is not likely to be machine performance problems

Suddenly remember to read on the Internet that is tmp_table_size value too small will cause this situation;
So Mysql-pxxx-e "show variables;" >tmp
One look is the default 32M (the number of bytes displayed)
So Weng happily changed up to 256 to restart MySQL. It turned out to be disappointing.

No, I have to come back.
Select the table found that there are forum messages in the volume is quite large
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 | |

What has been appearing in show Processlist is the select * from Bbs_message where board_id=xxx and parent_id=xxx
and select * from Bbs_message where parent_id=xxx
As long as the two-piece comes up, the CPU goes up.
So start with the index:
Add two Index
Mysql> ALTER TABLE Bbs_message Add index ParentID (parent_id);
ALTER TABLE Bbs_message Add index Chaxunid (board_id,parent_id);
Finally look at the 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 on top and load is stable at 0.x
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.