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\n", 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 \n", err)
continue
}
fmt.Printf("%+v \n", signLog)
//NewInsertData(signLog)
subTotal++
}
defer rows.Close()
startId += pageSize
}
}(i, startId)
}
}