解决MySQL的Out of Sort Memory错误

silverwq
2023-02-03 / 0 评论 / 349 阅读 / 正在检测是否收录...

概述

今天发现一些 SQL 报错“ERROR 1038 (HY001): Out of sort memory, consider increasing server sort buffer size”,之前版本没有问题,google 了一下发现了这个 BUG https://bugs.mysql.com/bug.php?id=103225,官方的回复说这不是一个 BUG,是一个新特性:

A potentially relevant change is that somewhere between those versions, we started sorting small blobs, such as TEXT, as addon fields instead of always doing sort-by-rowid. This is the reason why there's now more pressure on the sort buffer (but for most cases, sorts should still be faster).

原因

在 8.0 的某个版本,MySQL 将 text 字段也保存到 sort buffer 中,导致 8.0 版本需要配置比之前版本更大的 sort buffer,至于需要配置多大,官方建议至少能够容纳 15 条记录。

At minimum the sort_buffer_size value must be large enough to accommodate fifteen tuples in the sort buffer

如果不想增加 sort_buffer_size,也可以将 text 字段改为 longtext 类型,longtext 类型与之前版本一样不会保存到 sort buffer。需要注意的是,这 text 字段不一定是排序字段,要把表里所有 text 改成 long text

解决思路

调整 sort_buffer_size

show variables like "sort_buffer_size" ;
-- 调整到合适的值
set sort_buffer_size = 524288;
0

评论 (0)

取消