首页 > 数据库 >mysql巡检脚本sql补充

mysql巡检脚本sql补充

时间:2023-11-19 21:08:02浏览次数:43  
标签:巡检 name index sql mysql test table schema

-- 没有主键索引
mysql [localhost:8028] {root} (test) > SELECT t.table_schema,t.table_name,t.engine
    -> FROM information_schema.tables t
    -> JOIN information_schema.columns c
    -> ON t.table_schema=c.table_schema
    -> AND t.table_name=c.table_name
    -> WHERE t.table_schema NOT IN ('mysql', 'information_schema', 'sys', 'performance_schema')
    -> AND t.table_type = 'BASE TABLE'
    -> GROUP BY t.table_schema,t.table_name, t.engine
    -> HAVING SUM(IF(column_key IN ('PRI','UNI'), 1,0)) = 0;
+--------------+------------+--------+
| TABLE_SCHEMA | TABLE_NAME | ENGINE |
+--------------+------------+--------+
| test         | test6      | InnoDB |
+--------------+------------+--------+
1 row in set (0.01 sec)

– 主键不是整型(在高并发场景下,自增id主键就有性能问题,产生热点块争用,这种情况下,uuid可能更好些,解决了热点争用问题。分布式高并发下,可能用区别号+机房号+主机序号+流水号,可能会更好些。主键要根据实际的业务场景设计。)

mysql [localhost:8028] {root} (test) > SELECT table_schema, table_name, column_name, data_type, character_maximum_length
    -> FROM information_schema.columns
    -> WHERE column_key IN ('PRI','UNI')
    -> AND ordinal_position=1
    -> AND data_type NOT IN ('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'timestamp', 'datetime')
    -> AND table_schema NOT IN ('mysql', 'information_schema', 'sys', 'performance_schema');
+--------------+------------+-------------+-----------+--------------------------+
| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH |
+--------------+------------+-------------+-----------+--------------------------+
| test         | test7      | id          | varchar   |                       20 |
+--------------+------------+-------------+-----------+--------------------------+
1 row in set (0.00 sec)

– 存储引警不是innodb表

mysql [localhost:8028] {root} (test) > SELECT t.table_schema,t.table_name,t.engine
    -> FROM information_schema.tables t
    -> WHERE t.table_schema NOT IN ('mysql', 'information_schema', 'sys', 'performance_schema')
    -> AND t.engine <> 'InnoDB'
    -> AND t.table_type = 'BASE TABLE';
+--------------+------------+--------+
| TABLE_SCHEMA | TABLE_NAME | ENGINE |
+--------------+------------+--------+
| test         | test8      | MyISAM |
+--------------+------------+--------+
1 row in set (0.00 sec)

– 延迟最长的表和索引

SELECT * FROM sys.schema_table_statistics
WHERE table_schema='test'
AND table_schema NOT IN ('mysql', 'information_schema', 'sys', 'performance_schema')
Order by total_latency desc;

SELECT *
FROM sys.schema_index_statistics
WHERE table_schema='test'
AND table_schema NOT IN ('mysql', 'information_schema', 'sys', 'performance_schema');

– 索引数据大于数据的50%

mysql [localhost:8028] {root} (test) > SELECT table_schema, table_name, index_length, data_length, index_length/data_length AS index_to_data_ratio
    -> FROM information_schema.tables
    -> WHERE table_schema NOT IN ('mysql', 'information_schema', 'sys', 'performance_schema')
    -> AND INDEX_LENGTH > DATA_LENGTH*1.5;
+--------------+------------+--------------+-------------+---------------------+
| TABLE_SCHEMA | TABLE_NAME | INDEX_LENGTH | DATA_LENGTH | index_to_data_ratio |
+--------------+------------+--------------+-------------+---------------------+
| test         | test8      |         2048 |          20 |            102.4000 |
+--------------+------------+--------------+-------------+---------------------+

– 表有重复索引的

mysql [localhost:8028] {root} (test) > SELECT table_schema,table_name,redundant_index_name AS redundant_index, redundant_index_columns AS redundant_columns, dominant_index_name AS covered_by_index,sql_drop_index
    -> FROM sys.schema_redundant_indexes
    -> WHERE table_schema NOT IN ('mysql', 'information_schema', 'sys', 'performance_schema');
+--------------+------------+-----------------+-------------------+------------------+-------------------------------------------------+
| table_schema | table_name | redundant_index | redundant_columns | covered_by_index | sql_drop_index                                  |
+--------------+------------+-----------------+-------------------+------------------+-------------------------------------------------+
| test         | test8      | idx_seq         | seq_no            | idx_seq2         | ALTER TABLE `test`.`test8` DROP INDEX `idx_seq` |
+--------------+------------+-----------------+-------------------+------------------+-------------------------------------------------+
1 row in set (0.01 sec)

– 没用到索引的表

mysql [localhost:8028] {root} (test) > SELECT *
    -> FROM sys.schema_unused_indexes
    -> WHERE object_schema NOT IN ('mysql', 'information_schema', 'sys', 'performance_schema');
+---------------+-------------+------------+
| object_schema | object_name | index_name |
+---------------+-------------+------------+
| test          | test8       | idx_seq    |
| test          | test8       | idx_seq2   |
+---------------+-------------+------------+
2 rows in set (0.01 sec)

标签:巡检,name,index,sql,mysql,test,table,schema
From: https://blog.51cto.com/u_13482808/8475239

相关文章

  • INFINI Labs 产品更新 | 发布 Easysearch Java 客户端,Console 支持 SQL 查询等功能
    近年来,日志管理平台越来越流行。使用日志管理平台可以实时地、统一地、方便地管理和查看日志,挖掘日志数据价值,驱动运维、运营,提升服务管理效率。方案架构Beats是轻量级采集器,包括Filebeat、Metricbeat等。Easysearch是个分布式搜索引擎,提供搜集、分析、存储数据等主要功能。Con......
  • INFINI Labs 产品更新 | 发布 Easysearch Java 客户端,Console 支持 SQL 查询等功能
    近年来,日志管理平台越来越流行。使用日志管理平台可以实时地、统一地、方便地管理和查看日志,挖掘日志数据价值,驱动运维、运营,提升服务管理效率。方案架构Beats是轻量级采集器,包括Filebeat、Metricbeat等。Easysearch是个分布式搜索引擎,提供搜集、分析、存储数据等主要功......
  • 查看SQLServer平均最耗资源时间的SQL语句
    SELECT(total_elapsed_time/execution_count)/1000N'平均时间ms',total_elapsed_time/1000N'总花费时间ms',total_worker_time/1000N'所用的CPU总时间ms',total_physical_reads......
  • Linux安装MySQL
    本文使用的Linux发行版本为AlmaLinux9.264位(CentOS停止更新后的完美替代发行版本)。本文安装的MySQL版本为8.1.0,其他版本方法类似。MySQL源码编译时间太长了,需要3到4小时,使用官网编译好的rpm更简单快捷。操作步骤更新系统。dnf-yupdate查看是否已经有安装了的mysql......
  • 从 Mysql 架构上如何查询 sql 的执行过程?
    MySQL是一种流行的关系型数据库管理系统(RDBMS),由瑞典MySQLAB公司开发,目前属于甲骨文公司(Oracle)旗下。MySQL是基于SQL语言的一个开源数据库,可以用于存储、管理、检索数据。它支持大量的并发用户连接,并且提供了灵活的数据类型和索引机制。MySQL有多个版本,包括社区版和商业版,提供了不......
  • Docker - Run PostgreSQL database
    zzh@ZZHPC:~$dockerpullpostgres:alpinezzh@ZZHPC:~$dockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEpostgresalpine642d75c6be0c9daysago245MB zzh@ZZHPC:~$dockerrun--namepostgres16-p5432:5432-ePOSTGRES_......
  • postgresql 16主要新特性
    继postgresql15新特性后,pg16已经发布,这个版本的主要特性如下:1、逻辑复制支持级联,不一定要从primary开始;2、逻辑复制支持订阅自己,也就是真正的双主可以通过逻辑复制内置订阅实现了,不需要依赖三方,如pglogical3、copy性能提升明显,某些场景下300%4、SIMD支持数组和json、子事务搜......
  • SQL INSERT INTO 语句详解:插入新记录、多行插入和自增字段
    SQLINSERTINTO语句用于在表中插入新记录。INSERTINTO语法可以以两种方式编写INSERTINTO语句:指定要插入的列名和值:INSERTINTO表名(列1,列2,列3,...)VALUES(值1,值2,值3,...);如果要为表的所有列添加值,则无需在SQL查询中指定列名。但是,请确保值的顺序与表......
  • web sql 的查询的参数类型需要为字符串类型
    var grade = (info.split('')[3]-2)一开始的grade的类型是数字,websql怎么也无法按grade查询出来数据,左思右想,后来把它变成了字符串类型,终于成功查询出来了结果. var grade = (info.split('')[3]-2).toString() resultshowselect2(db, 'users', [grade, part......
  • SQL 的 AND、OR 和 NOT 运算符:条件筛选的高级用法
    AND运算符SQL的AND运算符用于根据多个条件筛选记录,确保所有条件都为TRUE才返回记录。下面是AND运算符的基本语法:SELECTcolumn1,column2,...FROMtable_nameWHEREcondition1ANDcondition2ANDcondition3...;column1,column2,等是您要选择的列名称。table_name是您......