使用 sqlx 批量插入

Bulk insert with sqlx

我正在尝试使用 sqlx 和 golang 进行批量插入:

    for _, result := range results {
            queryInsert := `INSERT INTO "DataCom_travel" (com1,com2,path,time) VALUES (,,,)`
            db.MustExec(queryInsert,result.Com1,result.Com2,result.Path,result.Time)
            queryUpdate := `UPDATE "DataCom_commcombinaison" set done = TRUE WHERE com1 = and com2 =`
            db.MustExec(queryUpdate,result.Com1,result.Com2)
        }

该代码有效,但速度很慢。

我已经试过了:

tx := db.MustBegin()
for _, result := range results {
        queryInsert := `INSERT INTO "DataCom_travel" (com1,com2,path,time) VALUES (,,,)`
        tx.MustExec(queryInsert,result.Com1,result.Com2,result.Path,result.Time)
        queryUpdate := `UPDATE "DataCom_commcombinaison" set done = TRUE WHERE com1 = and com2 =`
        tx.MustExec(queryUpdate,result.Com1,result.Com2)
    }
tx.Commit()

但是当我查看我的记录时它什么也没做我没有看到任何记录。

此致

编辑:

INSERT INTO "DataCom_travel" (com1,com2,path,time) VALUES (,,,),(,,,),(,,,),(,,,),(,,,),(,,,),(,,,),(,,,),(,,,),(,,,)
UPDATE "DataCom_commcombinaison" set done = TRUE WHERE (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  )  OR (com1 =  AND com2 =  ) 
panic: pq: column "com2" is of type integer but expression is of type text

您可以使用循环只构建一个插入并将参数聚合到一个切片中,然后只执行一次查询。像这样:

queryInsert := `INSERT INTO "DataCom_travel" (com1,com2,path,time) VALUES `
queryUpdate := `UPDATE "DataCom_commcombinaison" set done = TRUE WHERE `

insertparams := []interface{}{}
updateparams := []interface{}{}

for i, result := range results {
    p1 := i * 4 // starting position for insert params
    p2 := i * 2 // starting position for update params

    queryInsert += fmt.Sprintf("($%d,$%d,$%d,$%d),", p1+1,p1+2,p1+3,p1+4)
    queryUpdate += fmt.Sprintf("(com1=$%d AND com2=$%d) OR ", p2+1, p2+2)

    insertparams = append(insertparams, result.Com1, result.Com2, result.Path, result.Time)
    updateparams = append(updateparams, result.Com1, result.Com2)
}

queryInsert = queryInsert[:len(queryInsert)-1] // remove trailing ","
queryUpdate = queryUpdate[:len(queryUpdate)-4] // remove trailing " OR "

db.MustExec(queryInsert, insertparams...)
db.MustExec(queryUpdate, updateparams...)

用另一个库生成查询,然后使用 sqlx 执行查询可能会有用。

例如,使用squirrel:

// Build query with Squirrel
q := squirrel.Insert("DataCom_travel").Columns("com1", "com2", "path", "time")
for _, result := range results  {
  q = q.Values(result.Com1, result.Com2, result.Path, result.Time)
}
sql, args, err := q.ToSql()
if err != nil {
  return err
}

// Execure query with sqlx
res, err := db.MustExec(sql, args...)

您可以使用 NamedExec 而不是字符串连接。 sqlx README 底部有一个示例:https://github.com/jmoiron/sqlx/blob/master/README.md