天天看點

grails批量導入資料

一、給目的資料庫表建索引

此方法有一點用,在一次資料量不大的情況下效果很好,随着資料量的增大速度急劇下降。

二、随時清理緩存

如下代碼所示在服務類中定時情況緩存,效果非常好

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) {

         //導入資料代碼

    }