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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...