Bug #103465 MySQL 8.0.24 out of sort memory error 1038 when sorting table with JSON fields
Submitted: 25 Apr 2021 6:51 Modified: 25 Apr 2021 7:14
Reporter: Chris Rollins Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: Errors Severity:S3 (Non-critical)
Version:8.0.24 OS:Windows (Windows 10)
Assigned to: CPU Architecture:x86
Tags: json, mysql-8, Out of sort memory

[25 Apr 2021 6:51] Chris Rollins
Description:
As reported on StackOverflow here: https://stackoverflow.com/questions/67242726/mysql-8-order-by-created-at-out-of-sort-memor...

I've just recently upgraded to MySQL 8 and suddenly I'm getting this "Out of Sort Memory" error when sorting by a non-JSON field in a table with 2 JSON fields.

The error: SQL Error (1038): Out of sort memory, consider increasing server sort buffer size

My sort_buffer_size is set to 1M, which was always fine before upgrading to version 8.

The column I tried sorting by ("created_at") is a non-indexed timestamp.

After reading up a bit I found this release note on a change to the optimizer:

"One effect of this enhancement is that it is now possible for Out of memory errors to occur when trying to sort rows containing very large (multi-megabtye) JSON or GEOMETRY column values if the sort buffers are of insufficient size; this can be compensated for in the usual fashion by increasing the value of the sort_buffer_size system variable."
https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-20.html

But in my case the largest JSON value is far less than 1MB, so I understand that my case should not fall in this situation.

If I add an index to the `created_at` field, then I'm able to sort by it. But I don't think I should have to add indexes to every field in the table (except the JSON fields) in order to be able to sort by any of them whenever I wish. I generally only add indexes to fields that are frequently searched/sorted.

How to repeat:
I'm happy to send a dump of the table that is having the problems (I don't see a way to attach it to this report).

From there you would just import it and run a query such as:

SELECT * FROM mytable order by created_at;

Using the latest version of MySQL at the time of this writing: 8.0.24

Suggested fix:
Rick James recommends the following:

This error seems to be caused by an "improvement" in the Optimizer.  The "right" way to deal with the error is to do one of these:

* Use statistics on the table to conservatively estimate that the JSON (etc) could be too big to avoid the error, then use the old sorting method.  Or
* Catch the "out of memory" and revert to the old way.

One reverts unnecessarily in some cases (where the stats are not precise enough); the other has to undo some work (making it slower).

He mentions this with more detail here:

https://bugs.mysql.com/bug.php?id=103225

https://stackoverflow.com/questions/67242726/mysql-8-order-by-created-at-out-of-sort-memor...
[25 Apr 2021 6:53] Chris Rollins
I tried to attach the dump but there's a 3MB limit. So I uploaded it to Google Drive: https://drive.google.com/file/d/1qI9BTz8MFk3Bxw_K4qLWQ33_wqTPrht6/view?usp=sharing
[25 Apr 2021 7:14] MySQL Verification Team
Hello Chris Rollins,

Thank you for the report.
Imho this is duplicate of Bug #103225. Even if you feel that your issue is somewhat different, the resolution is likely to be the same. Because of this, we hope you add your comments to the original bug instead.

Thank you for your interest in MySQL.

regards,
Umesh
[26 Apr 2021 0:10] Rick James
Another apparent victim:
https://dba.stackexchange.com/questions/290441/unexpected-hy001-memory-allocation-error-af...