Because MySQL's concurrent data insertion capability is not well evaluatedProgramMySQL congestion is ignored.
As a result, the program is blocked by MySQL from time to time, causing sub-processes to wait for MySQL to release the plug and complete the INSERT command. Fault symptom:
- All blocked sub-processes are in sbwait status.
- Parent process, waiting for the child process to end, is in the Wait Status
- If you do not manually kill the blocked sub-processes, these processes will always exist.
Cause troubleshooting: start to suspect that it is a socket problem. This is because the connection to the server is blocked while waiting for the other party to close the connection. It took a long time to check and debug the socket part. Code When I thought it had been solved several times, and there was another fault, it all ended in failure. This weekend, we will re-connect the entire socket and check the database connections one by one. It is found that the sbwait status is caused by MySQL blocking. In the case of multi-process concurrency, MySQL resources are also preemptible. MySQL locks the table by default. When a sub-process is locked for insertion, B sub-process can only wait. As well as concurrency congestion. Solution:
- Optimize table structure and data structure
- Change Insert into to insert delayed
- Change the program structure so that each sub-process opens a MySQL connection
Description: insert delayed into: the client submits data to MySQL, and MySQL returns OK to the client. This is not to insert data into the table, but to store the data in the memory and wait for the queue. When MySQL is free, insert it again. The advantage is that the client does not need to wait too long to increase the insert speed. The disadvantage is that you cannot return an auto-incrementing ID, and if the system crashes, MySQL will lose the data before it can be inserted. Observation: After these adjustments, the system runs for one day without blocking. The running time is also shortened. Use phpMyAdmin to observe the MySQL process. After submission, some processes whose users are delayed and whose status is waiting for insert are displayed. After a while, the data will disappear after being completely inserted. Summary: For a system, we need to consider the existing and possible problems. We should not be too one-sided and self-righteous.