batchputs

Super fast batch inserts/updates to sql database, by packing data into sql with the least database roundtrip

  • 所有者: theplant/batchputs
  • 平台:
  • 许可证:
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

batchputs.Put utilize delete and insert sql with multiple values to do updates to database:

DELETE FROM tab WHERE c1 IN ("11", "21", "31"...)
INSERT INTO tab (c1, c2) VALUES ("11", "12"),("21", "22"),("31", "32"),(...)

With the minimum numbers of sqls (but very large body) sent to database for inserts/deletes, It can achieve great performance.

Variables

var Verbose bool

Collect Change Put

func CollectChangePut(
    db sq.BaseRunner,
    driverName string,
    tableName string,
    primaryKeyColumn string,
    columns []string,
    rows [][]interface{},
    rowWillChange RowWillChange,
) (err error)

Put

func Put(
    db sq.BaseRunner,
    driverName string,
    tableName string,
    primaryKeyColumn string,
    columns []string,
    rows [][]interface{}) (err error)

With this example, We created 30k records with 3 columns each, and inserts it into database in batch. and then we updates 20k records.

semaphoreci.com runs this example for inserting 30k records and updates 20k records totally less than 2 seconds.

=== RUN   ExamplePut_perf
--- PASS: ExamplePut_perf (1.73s)

Build Status

	db := openAndMigrate()
	// with table
	// CREATE TABLE countries
	// (
	//     code VARCHAR(50) PRIMARY KEY NOT NULL,
	//     short_name TEXT,
	//     special_notes TEXT,
	//     region TEXT,
	//     income_group TEXT,
	//     count INTEGER,
	//     avg_age NUMERIC
	// );
	
	rows := [][]interface{}{}
	for i := 0; i < 30000; i++ {
	    rows = append(rows, []interface{}{
	        fmt.Sprintf("CODE_%d", i),
	        fmt.Sprintf("short name %d", i),
	        i,
	    })
	}
	columns := []string{"code", "short_name", "count"}
	dialect := os.Getenv("DB_DIALECT")
	if len(dialect) == 0 {
	    dialect = "postgres"
	}
	
	start := time.Now()
	err := batchputs.Put(db.DB(), dialect, "countries", "code", columns, rows)
	if err != nil {
	    panic(err)
	}
	duration := time.Since(start)
	fmt.Println("Inserts 30000 records using less than 3 seconds:", duration.Seconds() < 3)
	
	rows = [][]interface{}{}
	for i := 0; i < 20000; i++ {
	    rows = append(rows, []interface{}{
	        fmt.Sprintf("CODE_%d", i),
	        fmt.Sprintf("short name %d", i),
	        i + 1,
	    })
	}
	start = time.Now()
	err = batchputs.Put(db.DB(), dialect, "countries", "code", columns, rows)
	if err != nil {
	    panic(err)
	}
	duration = time.Since(start)
	fmt.Println("Updates 20000 records using less than 3 seconds:", duration.Seconds() < 3)
	
	//Output:
	// Inserts 30000 records using less than 3 seconds: true
	// Updates 20000 records using less than 3 seconds: true

Type: Row Will Change

type RowWillChange func(row []interface{}, columns []string)

主要指标

概览
名称与所有者theplant/batchputs
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证
所有者活动
创建于2017-01-30 19:04:12
推送于2018-07-19 09:07:35
最后一次提交2018-07-19 17:07:09
发布数0
用户参与
星数65
关注者数44
派生数7
提交数9
已启用问题?
问题数4
打开的问题数4
拉请求数0
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?