Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: Updating a single object in a domain, ".save" Vs ".executeUpdate"

Tags:

save

grails

hql

I'm wondering what the most efficient way of updating a single value in a domain class from a row in the database. Lets say the domain class has 20+ fields

    def itemId = 1

    def item = Item.get(itemId)
    itemId.itemUsage += 1
    Item.executeUpdate("update Item set itemUsage=(:itemUsage) where id =(:itemId)", [usageCount: itemId.itemUsage, itemId: itemId.id])

    vs

    def item = Item.get(itemId)
    itemId.itemUsage += 1
    itemId.save(flush:true)
like image 657
Daxon Avatar asked Dec 07 '22 01:12

Daxon


2 Answers

executeUpdate is more efficient if the size and number of the un-updated fields is large (and this is subjective). It's how I often delete instances too, running 'delete from Foo where id=123' since it seems wasteful to me to load the instance fully just to call delete() on it.

If you have large strings in your domain class and use the get() and save() approach then you serialize all of that data from the database to the web server twice unnecessarily when all you need to change is one field.

The effect on the 2nd-level cache needs to be considered if you're using it (and if you edit instances a lot you probably shouldn't). With executeUpdate it will flush all instances previously loaded with get() but if you update with get + save if flushes just that one instance. This gets worse if you're clustered since after executeUpdate you'd clear all of the various cluster node caches vs flushing the one instance on all nodes.

Your best bet is to benchmark both approaches. If you're not overloading the database then you may be prematurely optimizing and using the standard approach might be best to keep things simple while you solve other problems.

like image 118
Burt Beckwith Avatar answered Dec 11 '22 08:12

Burt Beckwith


If you use get/save, you'll get the maximum advantage of the hibernate cache. executeUpdate might force more selects and updates.

The way executeUpdate interacts with the hibernate cache makes a difference here. The hibernate cache gets invalidated on executeUpdate. The next access of that Item after the executeUpdate would have to go to the database (and possibly more, I think hibernate might invalidate all Items in the cache).

Your best bet is to turn on debug logging for 'org.hibernate' in your Config.groovy and examine the SQL calls.

like image 27
ataylor Avatar answered Dec 11 '22 09:12

ataylor