How to speed up insertion performance in PostgreSQL

后端 未结 7 665
走了就别回头了
走了就别回头了 2020-11-22 06:25

I am testing Postgres insertion performance. I have a table with one column with number as its data type. There is an index on it as well. I filled the database up using thi

相关标签:
7条回答
  • 2020-11-22 07:14

    I encountered this insertion performance problem as well. My solution is spawn some go routines to finish the insertion work. In the meantime, SetMaxOpenConns should be given a proper number otherwise too many open connection error would be alerted.

    db, _ := sql.open() 
    db.SetMaxOpenConns(SOME CONFIG INTEGER NUMBER) 
    var wg sync.WaitGroup
    for _, query := range queries {
        wg.Add(1)
        go func(msg string) {
            defer wg.Done()
            _, err := db.Exec(msg)
            if err != nil {
                fmt.Println(err)
            }
        }(query)
    }
    wg.Wait()
    

    The loading speed is much faster for my project. This code snippet just gave an idea how it works. Readers should be able to modify it easily.

    0 讨论(0)
提交回复
热议问题