How to make Django modifiy JSONField value effictively

With MySQL8.0.25 as database backend, I make Django ORM to update the following JSONField value

{
  "01-task1": {"data":"huge data here"}
}

just add a new element , "02-testing": {}

but I got the following sql log

(0.000) UPDATE `by_jsontree_store1` SET `store_type` = 1, `user_id` = 2, `data` = '{\"01-task1\": {\"data\": \"huge data here\"}, \"02-testing\": {}}' 

WHERE `by_jsontree_store1`.`id` = 1; args=(1, 2, '{"01-task1": {"data": "huge data here"}, "02-testing": {}}', 1)

It looks like Django write the whole content of that JSONField.

Imaging if the original content is huge, like 100MB, what could happen.

Could Django update JSON field partially, just update what it need to?

Or for time being, Django can not convert ORM JSONField updating to to JSON_SET, JSON_REPLACE and JSON_REMOVE in MYSQL8.0 ? So we have to write raw sql to do that?

1 Like

This is still an open issue, see #32519 (Add support for using key and path transforms in update() for JSONFields.) – Django.

1 Like

I know you were using MYSQL, but if you know the raw sql you might be able to stitch something together in the mean time similar to:

Although, I’m not sure about the internals, and what kind of memory requirements would be needed to make it happen. Might be worth looking into for the time being.