当前位置: 首页 > news >正文

使用sys数据库分析 MySQL

使用sys数据库分析 MySQL的的运行情况

目录

开启performance_schema

找到配置文件

编辑配置文件

重启 MySQL 服务

验证是否启用

sys数据库概述

常用查询

查看每个数据库连接消耗资源

查看用户连接MySQL时消耗资源

查看当前MySQL总分配内存资源

查看MySQL中存在的冗余索引

查看MySQL中未使用到的索引

总结


MySQL的sys数据库中除了sys_config数据表外,基本上以视图的形式展示performance_schema中的其他数据。

因此,要想使用sys数据库分析MySQL,就需要先在my.cnf或者my.ini文件中开启performance_schema的配置。

开启performance_schema

以下是在配置文件中启用 performance_schema 的步骤:

找到配置文件

Linux/macOS 系统:通常是 /etc/my.cnf 或 /etc/mysql/my.cnf

Windows 系统:MySQL/my.ini

编辑配置文件

在 [mysqld] 部分添加或修改以下配置:

[mysqld]
performance_schema = ON

重启 MySQL 服务

Linux:sudo systemctl restart mysql 或 sudo service mysql restart

Windows:在服务管理器中重启 "MySQL" 服务,或使用命令 net stop mysql && net start mysql

验证是否启用

登录 MySQL 命令行后,执行以下命令检查:

mysql> SHOW VARIABLES LIKE 'performance_schema';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| performance_schema | ON    |
+--------------------+-------+
1 row in set (0.01 sec)

sys数据库概述

sys数据库中包含1个数据表、100个视图、48个存储过程和函数。

其中视图总体上可以分为两类:

以正常字母开始的视图共52个,能够显示格式化后的数据,适合开发人员或数据库维护人员阅读。

以“x$”开始的视图共48个,只显示未经处理的原始数据,适合使用工具采集数据。

常用查询

查看每个数据库连接消耗资源

mysql>  SELECT * FROM sys.host_summary \G
*************************** 1. row ***************************host: localhoststatements: 15statement_latency: 23.01 msstatement_avg_latency: 1.53 mstable_scans: 4file_ios: 35file_io_latency: 9.95 mscurrent_connections: 1total_connections: 3unique_users: 1current_memory: 1.02 MiB
total_memory_allocated: 22.84 MiB
1 row in set (0.01 sec)mysql>  SELECT * FROM sys.host_summary \G
*************************** 1. row ***************************host: localhoststatements: 52statement_latency: 33.14 msstatement_avg_latency: 637.24 ustable_scans: 5file_ios: 35file_io_latency: 9.95 mscurrent_connections: 1total_connections: 3unique_users: 1current_memory: 920.56 KiB
total_memory_allocated: 27.50 MiB
1 row in set (0.00 sec)mysql>  SELECT * FROM sys.host_summary \G
*************************** 1. row ***************************host: localhoststatements: 91statement_latency: 36.34 msstatement_avg_latency: 399.29 ustable_scans: 6file_ios: 35file_io_latency: 9.95 mscurrent_connections: 1total_connections: 3unique_users: 1current_memory: 920.59 KiB
total_memory_allocated: 32.18 MiB
1 row in set (0.00 sec)

查看用户连接MySQL时消耗资源

mysql> SELECT * FROM sys.user_summary \G
*************************** 1. row ***************************user: rootstatements: 130statement_latency: 39.35 msstatement_avg_latency: 302.68 ustable_scans: 7file_ios: 38file_io_latency: 11.95 mscurrent_connections: 1total_connections: 3unique_hosts: 1current_memory: 1.04 MiB
total_memory_allocated: 38.44 MiB
*************************** 2. row ***************************user: backgroundstatements: 1statement_latency: 150.90 usstatement_avg_latency: 150.90 ustable_scans: 0file_ios: 6805file_io_latency: 341.64 mscurrent_connections: 41total_connections: 47unique_hosts: 0current_memory: 42.34 MiB
total_memory_allocated: 395.75 MiB
2 rows in set (0.01 sec)

查看当前MySQL总分配内存资源

mysql> SELECT * FROM sys.memory_global_total;
+-----------------+
| total_allocated |
+-----------------+
| 344.69 MiB      |
+-----------------+
1 row in set (0.00 sec)

当前MySQL总共分配了344.69 MiB的内存资源。

查看MySQL中存在的冗余索引

mysql>  SELECT * FROM sys.schema_redundant_indexes \G
*************************** 1. row ***************************table_schema: books.comtable_name: fa_auth_group_accessredundant_index_name: uidredundant_index_columns: uid
redundant_index_non_unique: 1dominant_index_name: uid_group_iddominant_index_columns: uid,group_iddominant_index_non_unique: 0subpart_exists: 0sql_drop_index: ALTER TABLE `books.com`.`fa_auth_group_access` DROP INDEX `uid`
*************************** 2. row ***************************table_schema: cpoemtable_name: album_0redundant_index_name: idredundant_index_columns: id
redundant_index_non_unique: 1dominant_index_name: PRIMARYdominant_index_columns: iddominant_index_non_unique: 0subpart_exists: 0sql_drop_index: ALTER TABLE `cpoem`.`album_0` DROP INDEX `id`
*************************** 3. row ***************************table_schema: cpoemtable_name: album_1redundant_index_name: idredundant_index_columns: id
redundant_index_non_unique: 1dominant_index_name: PRIMARYdominant_index_columns: iddominant_index_non_unique: 0subpart_exists: 0sql_drop_index: ALTER TABLE `cpoem`.`album_1` DROP INDEX `id`
*************************** 4. row ***************************table_schema: cpoemtable_name: album_2redundant_index_name: idredundant_index_columns: id
redundant_index_non_unique: 1dominant_index_name: PRIMARYdominant_index_columns: iddominant_index_non_unique: 0subpart_exists: 0sql_drop_index: ALTER TABLE `cpoem`.`album_2` DROP INDEX `id`
######################## 省略n行 ##############################
249 rows in set (0.11 sec)

结果会显示出当前MySQL中所有的冗余索引。

查看MySQL中未使用到的索引

mysql> SELECT * FROM sys.schema_unused_indexes \G
Empty set, 3 warnings (0.00 sec)

注意:这里简单列举了几个常用的sys数据库的查询操作,更多可到MySQL官网进一步学习sys数据库的使用。地址为https://dev.mysql.com/doc/refman/8.0/en/sys-schema.html

总结

sys数据库比performance_schema数据库中的数据更加友好,

可以直接读取sys数据库中的数据来了解MySQL的运行情况。

http://www.dtcms.com/a/347653.html

相关文章:

  • 2015-2018年咸海流域1km归一化植被指数8天合成数据集
  • 【大模型应用开发 4.RAG高级技术与实践】
  • LeetCode算法日记 - Day 20: 两整数之和、只出现一次的数字II
  • 《P3623 [APIO2008] 免费道路》
  • Java22 stream 新特性 窗口算子 与 虚拟线程map操作:Gatherer 和 Gatherers工具类
  • 告别静态网页:我用Firefly AI + Spline,构建次世代交互式Web体验
  • 学习Java24天
  • React学习(十二)
  • IDEA相关的设置和技巧
  • C语言第十一章内存在数据中的存储
  • Redis资料
  • JAVA读取项目内的文件或图片
  • springboot项目结构
  • Axure:如何打开自定义操作界面
  • 顺序表(ArrayList)
  • 刷题日记0823
  • [特殊字符] 数据库知识点总结(SQL Server 方向)
  • MySQL:事务管理
  • games101 作业0 环境搭建与熟悉线性代数库
  • H264编解码过程简述
  • 数据结构 -- 哈希表
  • RAGFlow (一) 开发环境搭建
  • imx6ull-驱动开发篇37——Linux MISC 驱动实验
  • [机械结构设计-18]:Solidworks - 特征(Feature)是构成三维模型的基本单元,是设计意图的载体,也是参数化设计的核心。
  • 深入剖析分布式事务的Java实现:从理论到Seata实战
  • c语言中enum与#define的用法区别
  • 算法题(189):食物链
  • 如何利用数据库事务,来防止数据不一致的问题
  • 云原生概述
  • [e3nn] 归一化 | BatchNorm normalize2mom