-
View process status
-
service mysqld status
-
- Print information
- The Main PID in the printed information is the process ID.
-
View the thread status corresponding to the process
-
#top命令 top -Hp PID #htop命令 htop -p PID
- Use the above command to query the thread corresponding to the process based on the PID.
- You can clearly see the occupancy of each thread from the printed information. At this time, you only need to find the PID of the thread with the highest occupancy and write it down in a notebook.
-
-
Find sql statements executed by threads
-
select a.user, a.host, a.db, b.thread_os_id, b.thread_id, a.id processlist_id, a.command, a.time, a.state, a.info from information_schema.processlist a, performance_schema.threads b where a.id = b.processlist_id and (b.thread_os_id = 记在小本本上的PID or b.thread_os_id = 可以写多个);
Through the above sql statement, the corresponding executed sql information can be queried based on the PID recorded in the notebook. The results are as follows
-
Parameter analysis:
-
user | user |
host | Address + port number |
db | database |
thread_os_id | Thread ID allocated internally by the system. Of course, the OS does not create os_id for each session. |
thread_id | Only used in performance_schema, it is an internal automatically growing counter. Both foreground and background threads have this id, and thread_id is related to thread_os_id |
processlist_id | Generally used in the mysql layer, it is associated with the front-end login session and is the real thread ID. The processlistid is directly related to the front-end user. Every time a login session is created, a new processlistid will be added. In the performance_schema.threads table, the background thread does not have a processlistID value. This value is NULL. This is because the background thread is not created by the logged-in user, but both the front and back threads have thread_os_id and thread_id values, because all threads ultimately depend on OS thread execution, thread_id and thread_os_id have a certain corresponding relationship |
command | Type of currently executing sql, query, modify, delete, add |
time | time |
state | state |
info | Specific executed sql statement |
-
Optimization suggestions
According to the sql information in info, it is used to locate the call location in our code, and then further optimize according to the actual situation.