设置Mysql sort_buffer_size参数
按照官网的解释:
Each session that must perform a sort allocates a buffer of this size. sort_buffer_size is not specific to any storage engine and applies in a general manner for optimization. At minimum the sort_buffer_size value must be large enough to accommodate fifteen tuples in the sort buffer. Also, increasing the value of max_sort_length may require increasing the value of sort_buffer_size. For more information, see Section 8.2.1.14, “ORDER BY Optimization”
sort_buffer_size 是一个connection级参数,在每个connection需要buffer的时候,一次性分配的内存。
sort_buffer_size 并不是越大越好,过大的设置+高并发可能会耗尽系统内存资源。
1.查看sort_buffer_size默认值,默认值为256K
mysql> show variables like 'sort_buffer_size'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | sort_buffer_size | 262144 | +------------------+--------+ 1 row in set (0.00 sec)
2.设置global级别的sort_buffer_size值,设置sort_buffer_size=1M
mysql> SET GLOBAL sort_buffer_size = 1024*1024; Query OK, 0 rows affected (0.00 sec)
3.设置session级别的sort_buffer_size值,设置sort_buffer_size=2M
mysql> SET session sort_buffer_size = 2*1024*1024; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'sort_buffer_size'; +------------------+---------+ | Variable_name | Value | +------------------+---------+ | sort_buffer_size | 2097152 | +------------------+---------+ 1 row in set (0.00 sec)
4.设置永久生效,需要修改/etc/my.cnf文件,重启Mysql后生效
[mysqld] # 固定Mysql sort_buffer_size参数 sort_buffer_size=1M # 设置3306端口 port=3306 # 设置mysql的安装目录 basedir=D:\mysql-8.0.25-winx64 # 切记此处一定要用双斜杠\\,单斜杠我这里会出错 # 设置mysql数据库的数据的存放目录 datadir=D:\mysql-8.0.25-winx64\\data # data目录之后初始化会自动创建 # 允许最大连接数 max_connections=200 # 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统 max_connect_errors=10 # 服务端使用的字符集默认为UTF8 character-set-server=utf8mb4 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB # 默认使用“mysql_native_password”插件认证 default_authentication_plugin=mysql_native_password [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8mb4 [client] # 设置mysql客户端连接服务端时默认使用的端口 port=3306 default-character-set=utf8mb4