小编Wei*_*der的帖子

Golang 慢扫描()多行

我在 Golang 中运行一个查询,我从我的 Postgresql 数据库中选择多行。

我正在为我的查询使用以下导入

"database/sql"
"github.com/lib/pq"
Run Code Online (Sandbox Code Playgroud)

我已经缩小到我的循环以将结果扫描到我的结构中。

// Returns about 400 rows
rows, err = db.Query('SELECT * FROM infrastructure')
if err != nil {
    return nil, err
}

var arrOfInfra []model.Infrastructure
for rows.Next() {
    obj, ptrs := model.InfrastructureInit()
    rows.Scan(ptrs...)
    arrOfInfra = append(arrOfInfra, *obj)
}
rows.Close()
Run Code Online (Sandbox Code Playgroud)

上面的代码运行大约需要 8 秒,虽然查询速度很快,但 rows.Next() 中的循环需要整个 8 秒才能完成。

有任何想法吗?我做错了什么,还是有更好的方法?

我的数据库配置

// host, port, dbname, user, password masked for obvious reasons
db, err := sql.Open("postgres", "host=... port=... dbname=... user=... password=... sslmode=require")
if err != nil …
Run Code Online (Sandbox Code Playgroud)

postgresql loops go pq

5
推荐指数
1
解决办法
2551
查看次数

标签 统计

go ×1

loops ×1

postgresql ×1

pq ×1