关于 innodb_stats_on_metadata 的设置问题

在一次测试中发现,InnoDB数据库出现性能大幅波动,并且Innodb_buffer_pool_pages_free为0。经过调查发现,一些特定的监控查询触发了InnoDB进行索引统计更新,导致了不必要的磁盘I/O。

这个问题来自冷之同学测试时候碰到的一个“诡异现象”。

 

1、 测试现象       

测试的库有很多数据,但是重启之后,只对一个表的5w条记录作查询。查询条件客户端控制,确保查询范围。innodb_buffer_pool_size设置为35G

现象1:查询性能会出现大幅度抖动;

现象2:介入追查后发现,Innodb_buffer_pool_pages_free = 0

 

         其中bp剩余量这个是最直观异常的,因为访问的5w行记录撑死也不可能把35G内存吃光的。在QA同学确认没有别人在使用这个库的情况下。

 

2、过程和原因

         其实几乎确定还是有别的查询在访问的。所以打开general_log 发现除了QA同学压的语句外,这个Server上还有一些监控语句。

         其中一个语句如下

select constraint_schema,table_name,constraint_name,constraint_type from information_schema.table_constraints where table_schema not in ('information_schema', 'mysql', 'test');  

         这个语句访问了表 information_schema.table_constraints.

跟踪发现这个语句触发了读盘操作。原因是需要访问引擎的info()接口,而InnoDB此时又“顺手”做了更新索引统计的操作dict_update_statistics

更新索引统计的基本流程是随机读取部分demo行。所以这个操作实际上是访问了这个Server里面的所有表,因此不只是访问5w

而且由于别的表事先没有被访问,就会导致读盘操作,也包括BPLRU更新。

 

3、哪些表会触发

         不只是上面提到的table_constraintsinformation_schema库下的一下几个表,访问时候都会触发这个“顺手”操作。

information_schema.TABLES

information_schema.STATISTICS

information_schema.PARTITIONS

information_schema.KEY_COLUMN_USAGE

information_schema.TABLE_CONSTRAINTS

information_schema.REFERENTIAL_CONSTRAINTS

         其实还有 show table status ,也会触发这个操作,只是只处理单表,所以影响没那么明显。

 

4、修改

头痛医头的方法是把这些监控去掉。但实际上像TABLESTABLE_CONSTRAINTS这些表,都是静态数据,访问时不作索引统计也没关系的。

另外一个方法就是把innodb_stats_on_metadata设置成off,这样上述说到的这些表访问都不会触发索引统计。

         实际上这个动态统计的功能已经不推荐了,官方已经在6.0以后增加参数控制DML期间也不作动态统计了。因此这个参数配置成off更合理些(默认是on.

 

    之前有一篇文章也与此相关

[client] port=3306 [mysql] no-beep default-character-set = utf8mb4 [mysqld] init_connect='SET autocommit=0;' init_file=E:\\MySQL\\init.sql # 基础配置 port=3306 datadir=E:\MySQL\Data default-storage-engine=INNODB server-id=1 lower_case_table_names=1 secure-file-priv="C:/ProgramData/MySQL/MySQL Server 8.2/Uploads" # 修复连接问题的关键设置 bind-address=0.0.0.0 # 允许所有IP连接 skip_name_resolve=ON # 禁用DNS反向解析 # 认证策略修复 authentication_policy=mysql_native_password, # 字符集 character-set-server=utf8mb4 collation-server=utf8mb4_0900_ai_ci # SQL模式 sql-mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION" # 日志配置 log-output=FILE general-log=0 general_log_file="XIANJH.log" slow-query-log=1 slow_query_log_file="XIANJH-slow.log" long_query_time=3 log-error="XIANJH.err" log-bin="XIANJH-bin" binlog_expire_logs_seconds=604800 # 内存优化 max_connections=300 table_open_cache=5000 table_open_cache_instances=16 temptable_max_ram=2G tmp_table_size=512M max_heap_table_size=512M internal_tmp_mem_storage_engine=TempTable # InnoDB优化 innodb_buffer_pool_size=40G innodb_log_file_size=2G innodb_buffer_pool_instances=16 innodb_log_buffer_size=256M innodb_redo_log_capacity=4G innodb_flush_log_at_trx_commit=2 innodb_autoinc_lock_mode=2 bulk_insert_buffer_size=256M innodb_doublewrite=ON innodb_io_capacity=8000 innodb_io_capacity_max=16000 innodb_use_native_aio=ON innodb_flush_method=normal # 线程优化 innodb_thread_concurrency=0 innodb_parallel_read_threads=10 innodb_read_io_threads=10 innodb_write_io_threads=10 thread_cache_size=50 innodb_purge_threads=4 # 连接与网络 max_allowed_packet=256M net_buffer_length=32K max_connect_errors=1000 wait_timeout=300 interactive_timeout=300 # 文件与缓存 open_files_limit=10000 innodb_open_files=5000 innodb_file_per_table=ON innodb_autoextend_increment=128 # 查询优化 join_buffer_size=4M sort_buffer_size=4M read_buffer_size=1M read_rnd_buffer_size=1M # 性能与安全 performance_schema=ON innodb_checksum_algorithm=0 innodb_old_blocks_time=1000 innodb_stats_on_metadata=0 flush_time=0 # 复制相关 binlog_row_event_max_size=8K sync_source_info=10000 sync_relay_log=10000 # MySQL X Protocol loose_mysqlx_port=33060 这是我目前的my.ini的配置,我电脑是WIN10的操作系统,MYSQL版本是8.2,CPU是10核20线程的E5 2666V3,运存是4通道共64G服务器DD3内存,硬盘是SATA协议的1T SSD
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值