0

i have a grails job which is updating the totalSellCount of a product, for which i run a loop , i have a map productTotalSellCount which have the identifier of each product with its total sell count , now i am iterating the loop to update all product sell count like this

productTotalSellCount.each { k,v ->
            Product product = Product.findByIdentifier(k)
            product.totalSellCount  = productTotalSellCount.get(k)
            product.save(flush: true)
    }

i have around 50k products , and this is a daily schedule job and it always fails , help !!

user29578
  • 689
  • 7
  • 21

1 Answers1

0

Instead of using GORM, you should use batch updates: for example:

org.hibernate.StatelessSession session = grails.util.Holders.applicationContext.sessionFactory.openStatelessSession()
org.hibernate.Transaction tx = session.beginTransaction()

groovy.sql.Sql sql = new groovy.sql.Sql(session.connection())
//Create batch of 100 update statements before executing them to db
sql.withBatch(100, "update product set totalSellCount = :val0 where identifier = :val1") {
    groovy.sql.BatchingStatementWrapper stmt ->
        productTotalSellCount.each { identifier, value ->
            stmt.addBatch(val0: value, val1: identifier)
        }
}

tx.commit()
session.close()

You can also try parallel execution using GPars.

Sandeep Poonia
  • 2,158
  • 3
  • 16
  • 29
  • i tried this way, but it always thorws error as PRODUCT TABLE is not found, also i have a concern that whether we can sql batch update for mongodb database. – user29578 Jan 04 '16 at 15:11