I have a periodically running task that performs a lot of write calls to the database on existing data. In most cases, records that are written don't contain a change, so the update queries are redundant. The redundant write calls do put an unnecessarily high load on our database replication, though.
This is roughly the algorithm:
- Fetch existing row from database
- Potentially modify a lot of fields based on parameters that were passed to the algorithm
- Save the updated row to database using
instance.save()
Since it's easier to scale our task processors than our database replication, I thought I'd shift some of the processing load towards code by doing this:
if original_instance != possibly_updated_instance:
possibly_updated_instance.save()
Thing is: Django ORM's equality operator only seems to compare primary keys, so we'd incorrectly be dropping changes.
Is there a better way to handle this?