79.如何获取当前连接id的线程id?
以下演示是在mysql8.0.25版本中
1. PS_CURRENT_THREAD_ID()
root@mysqldb 21:46: [(none)]> \s; -------------- mysql Ver 8.0.25 for Linux on x86_64 (MySQL Community Server - GPL) Connection id: 10 #这里可以看到当前的连接id是10 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 8.0.25 MySQL Community Server - GPL Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8mb4 Db characterset: utf8mb4 Client characterset: utf8mb4 Conn. characterset: utf8mb4 UNIX socket: /data/mysql/3306/data/mysql_3306.sock Uptime: 10 min 47 sec Threads: 3 Questions: 327 Slow queries: 0 Opens: 165 Flush tables: 3 Open tables: 84 Queries per second avg: 0.505 -------------- root@mysqldb 21:46: [(none)]> select ps_current_thread_id(); +------------------------+ | ps_current_thread_id() | +------------------------+ | 52 | # 这里可以看到连接id对应的mysql的线程id是52. +------------------------+
2.PS_THREAD_ID(proceslist_id)
root@mysqldb 21:49: [(none)]> select ps_thread_id(10); +------------------+ | ps_thread_id(10) | +------------------+ | 52 | +------------------+ 1 row in set (0.00 sec)
这里也可以通过连接id,用函数ps_thread_id(),查看到它的线程id.
3. 几张关于连接的表或者视图
其中有threads表,sys.processlist或者sys.session视图
thread表这里就不说了,主要看看这两张视图,其实这两张视图的字段信息是完全一样的。
这里就展示一下:
root@mysqldb 21:40: [(none)]> select * from sys.processlist where thd_id=50 \G; *************************** 1. row *************************** thd_id: 50 conn_id: 8 user: root@localhost db: test command: Sleep state: NULL time: 51 current_statement: NULL statement_latency: NULL progress: NULL lock_latency: 355.00 us rows_examined: 4 #扫描的行数 rows_sent: 4 rows_affected: 0 tmp_tables: 0 tmp_disk_tables: 0 full_scan: YES last_statement: select * from t #这里还展示了最新执行的动作。 last_statement_latency: 1.96 ms current_memory: 1.03 MiB #当前该线程占用的内存大小(这个挺重要的) last_wait: NULL last_wait_latency: NULL source: NULL trx_latency: 83.68 us trx_state: COMMITTED trx_autocommit: YES pid: 4126 program_name: mysql