goプロセスバッチクエリーデータベース
1667 ワード
goプロセスバッチクエリーデータベース
func init() {
dsn := fmt.Sprintf("%s:%s@%s(%s:%d)/%s", USERNAME, PASSWORD, NETWORK, SERVER, PORT, DATABASE)
NEWDB, err = sql.Open("mysql", dsn)
if err != nil {
fmt.Printf("Open mysql failed,err:%v
", err)
return
}
//DB.SetConnMaxLifetime(1 * time.Second) // , close
NEWDB.SetMaxOpenConns(8) //
NEWDB.SetMaxIdleConns(5) //
fmt.Printf(" ")
}
func process() {
//DB := NEWDB
//4 ,4 , , 8
total := 100
process := 4
pageSize := 10
eachTimes := total / process //
for i := 0; i < process; i++ {
startId := i * eachTimes
endId := eachTimes * (i + 1)
go func(i int, startId int) {
subTotal := 0
for {
if startId >= endId {
fmt.Println(" :", subTotal)
break
}
fmt.Println("[process]:", i, ",start_id: ", startId, "end_id:", endId, "limit: ", pageSize)
signLog := new(SignInLog)
rows, err := DB.Query("select * from sign_in_log where id > ? and id limit ?", startId, endId, pageSize)
if err != nil {
fmt.Println(err)
}
for rows.Next() {
rows.Scan()
err := rows.Scan(
&signLog.ID,
&signLog.UserId,
&signLog.RecentSignTime,
&signLog.FirstSignTime,
&signLog.SignType
)
if err != nil {
fmt.Printf("err : %v
", err)
continue
}
fmt.Printf("%+v
", signLog)
//NewInsertData(signLog)
subTotal++
}
defer rows.Close()
startId += pageSize
}
}(i, startId)
}
}