mysql8数据库优化

标题调整mysql配置参数

使用explain查看sql执行过程,分析使用的索引,避免使用缓存文件排序,尽量使用主键排序
Using where; Backward index scan

标题性能分析

查看数据库当前运行情况

show processlist;

开启分析功能

set profiling=on;

执行需要分析的数据库操作

查看最新运行情况

show profiles

使用explain查看sql的执行过程

explain  xxxsql

执行完查询后,使用show profiles查看query id;
使用show profile for query query_id查看详细信息;

show profile for query  xxx

提高写入效率:
innodb_flush_log_at_trx_commit
抱怨Innodb比MyISAM慢 100倍?那么你大概是忘了调整这个值。默认值1的意思是每一次事务提交或事务外的指令都需要把日志写入(flush)硬盘,这是很费时的。特别是使用电池供电缓存(Battery backed up cache)时。设成2对于很多运用,特别是从MyISAM表转过来的是可以的,它的意思是不写入硬盘而是写入系统缓存。日志仍然会每秒flush到硬盘,所以你一般不会丢失超过1-2秒的更新。设成0会更快一点,但安全方面比较差,即使MySQL挂了也可能会丢失事务的数据。而值2只会在整个操作系统挂了时才可能丢数据。
建议设置为2
临时设置(直接在mysql客户端工具中执行,重启后设置失效):

set global innodb_flush_log_at_trx_commit=2;

查看:

select @@innodb_flush_log_at_trx_commit;

彻底设置,修改/etc/my.cnf配置文件

innodb_flush_log_at_trx_commit = 2

8G内存的服务器配置:

innodb_buffer_pool_size = 3g
tmp_table_size=1g
sort_buffer_size = 64m
read_rnd_buffer_size = 2m
max_heap_table_size=64m
join_buffer_size = 128m
innodb_flush_log_at_trx_commit = 2
innodb_log_file_size = 64m
innodb_log_buffer_size = 8m
read_buffer_size = 32m
max_allowed_packet=16m
table_open_cache=5120
max_length_for_sort_data=8192
vim /etc/my.cnf

32G内存服务器配置:

innodb_buffer_pool_size = 18g
tmp_table_size=1g
sort_buffer_size=512m
read_rnd_buffer_size=2m
read_buffer_size = 32m
max_heap_table_size=64m
join_buffer_size = 128m
innodb_flush_log_at_trx_commit = 2
innodb_log_file_size = 127m
innodb_log_buffer_size = 32m
read_buffer_size = 64m
max_allowed_packet=32m
table_open_cache=5120
max_length_for_sort_data=8192

配置完成后检查一下,总内存大小不要超过服务器的百分之八十

SELECT(@@key_buffer_size + @@innodb_buffer_pool_size + @@query_cache_size + @@tmp_table_size + @@max_connections * (@@read_buffer_size + @@read_rnd_buffer_size + @@sort_buffer_size + @@join_buffer_size + @@binlog_cache_size + @@thread_stack))/1024/1024/1024 AS result 

版权声明:本文为JHYPXS原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。