如何在Go语言中快速获取多行数据?

huangapple go评论85阅读模式
英文:

How get fast multiple rows in Go?

问题

我在使用SELECT * FROM table查询语句中,当获取20k-50k甚至更多行数据并使用rows.scan(&a, &b...)方法进行扫描时,遇到了性能问题。我不知道如何在这种情况下使用并发,因为我需要使用rows.Next()进行迭代,而无法在并发中执行此操作。

性能下降只发生在将查询结果从行扫描到结构体字段中的rows.Scan操作上。

查询本身只需要5-15毫秒,但扫描操作却需要(40k行数据)800-2000毫秒。

谢谢!

英文:

I have troubles with performance when I get 20k-50k and more rows from SELECT * FROM table in rows.scan(&a, &b...). I don't know how to use concurrency in this case because I need to use rows.Next() for iterating, and I can't do it in concurrency.

Performance drops only when I scan results from rows to structure's fields in rows.Scan.

The query takes 5-15 ms, but scanning takes (40k rows) 800-2000ms.

Thank you!

答案1

得分: 1

由于'rows.Next()'是顺序执行的,你需要将查询拆分为多个语句,然后并发地处理它们。

func main() {

    queries := []string{
        "SELECT * FROM abc where a < 10000",
        "SELECT * FROM abc where a >= 10000 && a <= 30000",
        "SELECT * FROM abc where a > 30000",
    }

    for _, query := range queries {
        go dbCall(query)
    }
}

func dbCall(query string) {

    rows, _ := db.Query(query)
    for rows.Next() {
        var a, b, c int
        _ = rows.Scan(&a, &b, &c)
        // 处理行数据
    }
}

根据你的使用情况,根据需要使用通道、锁等。

英文:

Since 'rows.Next()' is sequential, you would need to split your query into multiple statements, and then work concurrently on them.

func main() {

	queries := []string{
		&quot;SELECT * FROM abc where a &lt; 10000&quot;,
		&quot;SELECT * FROM abc where a &gt;= 10000 &amp;&amp; a &lt;= 30000&quot;,
		&quot;SELECT * FROM abc where a &gt; 30000&quot;,
	}

	for _, query := range queries {
		go dbCall(query)
	}
}

func dbCall(query string) {

	rows, _ := db.Query(query)
	for rows.Next() {
		var a, b, c int
		_ = rows.Scan(&amp;a, &amp;b, &amp;c)
		// Process row
	}
}

Use channels, locks, etc when necessary, based on your use case.

huangapple
  • 本文由 发表于 2017年1月6日 03:07:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/41492793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定