一、给目的数据库表建索引

此方法有一点用,在一次数据量不大的情况下效果很好,随着数据量的增大速度急剧下降。

二、随时清理缓存

如下代码所示在服务类中定时情况缓存,效果非常好

class BookService {

     def sessionFactory

     def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP

     def importBooksInLibrary(library) {

         library.eachWithIndex { Map bookValueMap, index ->

             updateOrInsertBook(bookValueMap)

             if (index % 100 == 0) cleanUpGorm()

        }

     }

     def cleanUpGorm() {

         def session = sessionFactory.currentSession

         session.flush()

         session.clear()

         propertyInstanceMap.get().clear()

     }

     def updateOrInsertBook(Map bookValueMap) {

         //导入数据代码

    }

 }

引用Batch Import Performance With Grails and MySQL.note