XML将性能插入MYSQL

问题描述

我有一些代码可将记录插入数据库

代码应该在数据库上插入1500万条记录,现在,在AWS t2.large实例上需要60个小时。我正在寻找使插入DB的速度更快,同时又不重复记录的方法

你们对我有建议吗?

我正在使用Gorm和MysqL


// InsertJob will insert job into database,by checking its hash.
func InsertJob(job XMLJob,oid int,ResourceID int) (Job,error) {
    db := globalDBConnection
    cleanJobDescription := job.Body

    hashString := GetMD5Hash(job.Title + job.Body + job.Location + job.Zip)
    JobDescriptionHash := GetMD5Hash(job.Body)
    empty := sql.NullString{String: "",Valid: true}
    j := Job{
        CurrencyID:              1,//USD

        //other fields here elided for brevity

        PrimaryIndustry: sql.NullString{String: job.PrimaryIndustry,Valid: true},}

    err := db.Where("hash = ?",hashString).Find(&j).Error
    if err != nil {
        if err.Error() != "record not found" {
            return j,err
        }

        err2 := db.Create(&j).Error
        if err2 != nil {
            log.Println("Unable to create job:" + err.Error())
            return j,err2
        }
    }

    return j,nil
}

解决方法

您可以使用信号量模式来加快速度。

https://play.golang.org/p/OxO8pNy3bc6

灵感来自这里。

https://gist.github.com/montanaflynn/ea4b92ed640f790c4b9cee36046a5383