How do I batch sql statements with package database/sql

前端 未结 12 2670
深忆病人
深忆病人 2020-12-04 16:46

How do I batch sql statements with Go\'s database/sql package?

In Java I would do it like this :

// Create a prepared statement
String sql = \"INSERT         


        
12条回答
  •  暖寄归人
    2020-12-04 17:16

    Since the db.Exec function is variadic, one option (that actually does only make a single network roundtrip) is to construct the statement yourself and explode the arguments and pass them in.

    Sample code:

    func BulkInsert(unsavedRows []*ExampleRowStruct) error {
        valueStrings := make([]string, 0, len(unsavedRows))
        valueArgs := make([]interface{}, 0, len(unsavedRows) * 3)
        for _, post := range unsavedRows {
            valueStrings = append(valueStrings, "(?, ?, ?)")
            valueArgs = append(valueArgs, post.Column1)
            valueArgs = append(valueArgs, post.Column2)
            valueArgs = append(valueArgs, post.Column3)
        }
        stmt := fmt.Sprintf("INSERT INTO my_sample_table (column1, column2, column3) VALUES %s", 
                            strings.Join(valueStrings, ","))
        _, err := db.Exec(stmt, valueArgs...)
        return err
    }
    

    In a simple test I ran, this solution is about 4 times faster at inserting 10,000 rows than the Begin, Prepare, Commit presented in the other answer - though the actual improvement will depend a lot on your individual setup, network latencies, etc.

提交回复
热议问题