Error
Error Code:
3150
MySQL Error 3150: JSON Value Too Large
Description
This error indicates that an attempt was made to store a JSON value in a MySQL JSON column, but the value's size exceeded the allowed limit. It typically occurs during INSERT or UPDATE operations when working with large JSON documents.
Error Message
The JSON value is too big to be stored in a JSON column.
Known Causes
3 known causesJSON Data Exceeds Column Limit
The size of the JSON document being written surpasses the maximum storage capacity for a JSON data type in MySQL, which is implicitly limited to 1GB.
Server Resource Constraints
During the processing of a large JSON value, the MySQL server or underlying system encountered memory or buffer limitations, preventing successful storage.
Inefficient JSON Construction
The application is generating overly verbose or deeply nested JSON structures, leading to documents that become too large for practical database storage.
Solutions
3 solutions available1. Increase JSON Column Size Limit (if applicable) easy
Temporarily increase the maximum size of JSON documents if the data is only slightly over the limit.
1
Check the current `max_allowed_packet` setting. This is the primary server-side limit for individual packet sizes, including JSON documents.
SHOW VARIABLES LIKE 'max_allowed_packet';
2
If the `max_allowed_packet` is too low, increase it. This can be done dynamically for the current session or permanently in the MySQL configuration file.
-- For current session (resets on server restart):
SET GLOBAL max_allowed_packet = 67108864; -- Set to 64MB (adjust as needed)
-- For permanent change, edit my.cnf or my.ini:
-- [mysqld]
-- max_allowed_packet = 67108864
3
After changing `max_allowed_packet` permanently, restart the MySQL server for the changes to take effect.
sudo systemctl restart mysql # Or your specific restart command
2. Optimize JSON Data Structure medium
Reduce the size of your JSON documents by restructuring them or removing redundant data.
1
Analyze the JSON data that is causing the error. Identify large arrays, deeply nested objects, or repetitive data.
SELECT your_json_column FROM your_table WHERE JSON_LENGTH(your_json_column) > <some_threshold>;
-- Also, consider using tools to pretty-print and analyze the JSON structure externally.
2
Refactor the JSON to remove unnecessary fields, compress arrays (e.g., by using unique identifiers instead of full objects), or flatten nested structures where appropriate.
N/A (This is a data modeling/application logic task)
3
If possible, consider if a relational approach is more suitable for parts of the data that are growing excessively large within the JSON.
N/A
3. Store Large JSON in a Separate Table or BLOB/TEXT Column medium
Move excessively large JSON data to a different storage mechanism to avoid hitting JSON column limits.
1
Create a new table to store the large JSON documents, or add a `LONGTEXT` or `BLOB` column to the existing table.
CREATE TABLE large_json_data (
id INT AUTO_INCREMENT PRIMARY KEY,
parent_id INT,
large_json LONGTEXT,
FOREIGN KEY (parent_id) REFERENCES your_table(id)
);
-- OR, add to existing table:
ALTER TABLE your_table ADD COLUMN large_json LONGTEXT;
2
Modify your application logic to store the large JSON documents in this new column or table.
N/A (Application code modification)
3
If using a separate table, update your queries to join with `large_json_data` when you need to access the large JSON content.
SELECT t.*, lj.large_json FROM your_table t JOIN large_json_data lj ON t.id = lj.parent_id WHERE ...