我同时在单独的go例程中运行mysql“ select”查询。但是,mysql服务似乎会收集这些查询,依次(不是同时运行)运行它们,然后仅在运行所有查询之后,才同时返回所有结果集。
我的问题是:
1.为什么这些查询按顺序而不是同时运行?
2.为什么mysql为什么要等到所有查询都运行后才能同时返回每个查询的结果集(即使每个结果集都属于一个不同的go例程,并且据说还使用单独的连接)?
另一件事:当我设置“ SetMaxOpenConns(2)”时,它将同时返回两个结果集。如果设置为3,则同时返回3个结果集。但是,它们仍然始终按顺序运行。
有人知道这是怎么回事吗?
package main
import (
"database/sql"
"fmt"
"sync"
"time"
_ "github.com/go-sql-driver/mysql"
)
var database *sql.DB
func init() {
var err error
databaseURI := "root:toor@tcp(192.168.200.10:3306)/ahc"
database, err = sql.Open("mysql", databaseURI)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("DB Connection Established")
//database.SetMaxIdleConns(0)
//database.SetMaxOpenConns(2)
}
}
func test(device string, wg *sync.WaitGroup) {
fmt.Println("Thread: " + device + " started")
start := time.Now()
var count string
//using go-sql-driver
sqlStatement, err := database.Prepare("select count(cpeName) from report where errMessage <> \"ok\" and cpeName = ? and jobID = ?")
if err != nil {
fmt.Println(err)
}
defer sqlStatement.Close()
err = sqlStatement.QueryRow(device, "11534").Scan(&count)
sqlStatement.Close()
duration := time.Since(start)
fmt.Println("Thread: " + device + " Duration: " + duration.String() + "\n")
wg.Done()
}
func main() {
var wg sync.WaitGroup
var deviceList = []string{"xx-swrk-ca-gen-s-002", "xx-leus-ca-ust-ap-068", "xx-sgvn-ca-lug-ap-004", "xx-swrk-ca-vez-s-005", "xx-swrk-ca-vez-ap-006", "xx-leus-ca-ust-ap-065", "xx-leus-ca-ust-ap-073", "xx-leus-ca-ust-ap-076", "xx-leus-ca-ust-ap-077", "xx-swrk-ca-gen-s-001"}
total := time.Now()
for _, device := range deviceList {
wg.Add(1)
go test(device, &wg)
}
wg.Wait()
duration := time.Since(total)
fmt.Println("\n\nTotal: Duration: " + duration.String() + "\n")
}
这是输出
DB Connection Established
Thread: xx-leus-ca-ust-ap-068 started
Thread: xx-sgvn-ca-lug-ap-004 started
Thread: xx-leus-ca-ust-ap-065 started
Thread: xx-leus-ca-ust-ap-073 started
Thread: xx-swrk-ca-vez-ap-006 started
Thread: xx-swrk-ca-vez-s-005 started
Thread: xx-leus-ca-ust-ap-076 started
Thread: xx-leus-ca-ust-ap-077 started
Thread: xx-swrk-ca-gen-s-002 started
Thread: xx-swrk-ca-gen-s-001 started
Thread: xx-leus-ca-ust-ap-076 Duration: 7.291656143s
Thread: xx-swrk-ca-gen-s-002 Duration: 7.304134404s
Thread: xx-leus-ca-ust-ap-065 Duration: 7.307958641s
Thread: xx-swrk-ca-vez-s-005 Duration: 7.313591747s
Thread: xx-leus-ca-ust-ap-077 Duration: 7.313992638s
Thread: xx-swrk-ca-vez-ap-006 Duration: 7.314905664s
Thread: xx-swrk-ca-gen-s-001 Duration: 7.320466323s
Thread: xx-leus-ca-ust-ap-073 Duration: 7.322158337s
Thread: xx-leus-ca-ust-ap-068 Duration: 7.324745097s
Thread: xx-sgvn-ca-lug-ap-004 Duration: 7.326001783s
Total: Duration: 7.326096238s
使用database.SetMaxOpenConns(1)时,输出为:
DB Connection Established
Thread: xx-leus-ca-ust-ap-068 started
Thread: xx-swrk-ca-gen-s-001 started
Thread: xx-swrk-ca-vez-ap-006 started
Thread: xx-leus-ca-ust-ap-065 started
Thread: xx-leus-ca-ust-ap-073 started
Thread: xx-swrk-ca-gen-s-002 started
Thread: xx-leus-ca-ust-ap-077 started
Thread: xx-sgvn-ca-lug-ap-004 started
Thread: xx-leus-ca-ust-ap-076 started
Thread: xx-swrk-ca-vez-s-005 started
Thread: xx-leus-ca-ust-ap-068 Duration: 1.131790286s
Thread: xx-leus-ca-ust-ap-077 Duration: 2.128919333s
Thread: xx-swrk-ca-gen-s-001 Duration: 3.073559464s
Thread: xx-leus-ca-ust-ap-073 Duration: 4.002964333s
Thread: xx-swrk-ca-vez-s-005 Duration: 4.932256684s
Thread: xx-sgvn-ca-lug-ap-004 Duration: 5.853361245s
Thread: xx-swrk-ca-gen-s-002 Duration: 6.785042625s
Thread: xx-leus-ca-ust-ap-065 Duration: 7.705957815s
Thread: xx-swrk-ca-vez-ap-006 Duration: 8.633000734s
Thread: xx-leus-ca-ust-ap-076 Duration: 9.550948572s
Total: Duration: 9.551103129s
答案 0 :(得分:0)
查看它们是并行还是串行运行的简单技术:让每个连接都可以运行
SELECT SLEEP(1);
如果是并行的,则设置不会超过1秒。如果是连续的,则为N秒。
如果它们按顺序运行,但是直到所有输出都完成才输出,那将是一个GO问题。
如果您的7.3s确实是并行的,而9.5是串行的,那么这说明了为什么不值得并行运行东西-它们会互相踩踏而不能更快地完成。冲突可能是CPU或I / O或互斥锁或网络或其他问题。这取决于查询。 (我的sleep
测试是非侵入性的,但是需要可预测的时间。)