Chapter 3 Mysql database performance
3.1 Performance monitoring of Mysql database
3.1.1 Number of mysql connections
The number of connections refers to the number of connections the user has created. MySQL outputs the details of the number of threads running in the database by executing the SHOW PROCESSLIST command. SHOW PROCESSLIST only displays 100 by default. If you need to display more than 100 records, you need to execute SHOW FULL PROCESSLIST
The maximum number of connections supported by the database: SHOW VARIABLES like'max_connections'
The maximum number of connections currently used: SHOW GLOBAL STATUS like'Max_used_connections'
3.1.2 Transactions and locks currently running in the mysql database
A transaction is a data operation with atomicity, consistency, isolation, and durability performed on the database.
If transactions are required in Mysql, the innodb engine of Mysql must be used when storing the database. After using the Innodb engine, the innodb_trx table of information_schmea in the mysql system database records the running transactions of the database.
Field | Description |
---|---|
trx_id | Transaction ID |
trx_state | The status of the transaction: Running, Lock Wait, Rolling Back and Commiting, etc. |
trx_started | Transaction start time |
trx_requested_lock_id | The resource id that the transaction needs to wait for but has been locked by other programs can generally be associated with the innodb_locks table to obtain more detailed information about the locked resource |
trx_wait_started | Transaction start waiting time |
trx_weight | Transaction weight |
trx_mysql_thread_id | MySQL thread id corresponding to the transaction |
trx_query | The SQL statement that the transaction is executing |
trx_operation_state | The status of the transaction operation |
trx_tables_in_use | The number of database tables used by the transaction |
trx_tables_locked | The number of database tables locked by the transaction |
trx_lock_structs | |
trx_lock_memory_bytes | The amount of memory locked by the transaction |
trx_rows_locked | The number of data record rows locked by the transaction |
trx_rows_modified | The number of data record rows changed by the transaction |
trx_concurrency_tickets | Number of transactions and invoices |
trx_isolation_level | Transaction isolation level: generally divided into four different levels: Read Uncommited, Read Commited, Repeated Read, and Serializable |
trx_unique_checks | Whether the transaction is opened for uniqueness check ID |
trx_foreign_key_checks | Whether the transaction opens the foreign key check mark |
trx_last_foreign_key_error | The last foreign key check error of the transaction |
trx_adaptive_hash_latched | |
trx_adaptive_hash_timeout | |
trx_is_read_only | |
trx_autocommit_non_locking |
The innodb_locks table of the Mysql system database information_schmea records the locks currently generated by the innodb database engine.
Observation of Mysql 8.0 lock https://blog.csdn.net/n88Lpo/article/details/108211411
Field | Description |
---|---|
lock_id | Lock ID |
lock_trx_id | The transaction ID that owns the lock can be queried with the Innodb_trx linked list to obtain transaction details |
lock_mode | Lock mode: Row-level locks: including S (shared lock), X (exclusive lock), IS (intentional shared lock), IX (intentional exclusive lock) Table-level locks: including S_GAP (shared gap lock), X_GAP (exclusive gap lock), IS_GAP (intention shared gap lock), IX_GAP (intention exclusive gap lock), AUTO_INC (automatic incremental lock) Page-level lock: a lock between row-level locks and table-level locks |
lock_type | 锁的类型,包括RECORD(行级锁),TABLE(表级锁),PAGE(页级锁),innodb引擎中主要采用行级锁; |
lock_table | 当前被锁定的或者包含锁定记录的表的名称 |
lock_index | 当lock_type为RECORD时,表示锁定的索引的名称,否则直接返回NULL |
lock_space | 当lock_type为RECORD时,表示锁定的表空间ID,否则直接返回NULL |
lock_page | 当lock_type为RECORD时,表示锁定的记录行的页数,否则直接返回NULL |
lock_rec | 当lock_type为RECORD时,表示锁定的数据行的数量 |
lock_data | 当lock_type为RECORD时,表示锁定的记录行的主键 |
Mysql系统数据库information_schmea的innodb_lock_waits表中记录innodb数据库引擎当前运行的数据库事务等待锁的情况。
字段 | 说明 |
---|---|
requesting_trx_id | 请求事务的id |
requested_lock_id | 事务所等待的锁的id,可以和innodb_locks表关联查询 |
blocking_trx_id | 阻塞事务的ID |
blocking_lock_id | 阻塞了另一事务的、正在运行的事务的锁的ID |
数据库中出现死锁时,经常需要通过查询innodb_trx,innodb_locks和innodb_lock_waits来找出在执行什么事务操作时导致了死锁,例如执行如下SQL语句可以列出数据中所有事务的等待和锁定记录
--这里有sql语句