postgresql - 从查询到 c.JSON 非常慢

标签 postgresql go go-gin

我正在使用 Gorm 查询我的数据库,然后使用 gin 的 c.JSON 将结构编码为 json。

这是一个大型查询,结果不多 (< 100k),我对整理数据所需的时间(6-10 秒)有疑问。

我不知道从哪里开始解决这个问题。

    [2019-07-02 14:41:04]  [946.63ms]   SELECT  big slow query
    [62861 rows affected or returned ]
    [GIN] 2019/07/02 - 14:41:11 | 200 |   7.92347114s |  ip | GET      /api/date/2019-05-30


    [2019-07-02 14:40:44]  [660.47ms]   SELECT big slow query
    [7583 rows affected or returned ]
    [GIN] 2019/07/02 - 14:40:54 | 200 | 10.841096216s |  ip | GET      /api/dailies

    [2019-07-02 14:43:49]  [154.13ms]   SELECT simple query
    [11 rows affected or returned ]
    [GIN] 2019/07/02 - 14:43:49 | 200 |  158.256792ms |  ip | GET      /api/dailycount

如您所见,查询 1 和 2 在 600-900 毫秒内解析,速度很慢,但可以单独优化。 问题是服务器的响应需要 7.9 和 10.8 秒 ..! 对于较小的查询,没有太大区别,但我不明白为什么会这样。

其中一条路线的代码非常简单,所有路线都相似:

    var alertList []AlertJson
    dbInstance.Debug().Raw("SELECT big query").Scan(&alertList)
    c.JSON(http.StatusOK, gin.H{"alerts": alertList}) 

第二个查询需要 10.2 秒,要编码 7583 行,这对我来说太疯狂了。

最佳答案

我的服务器提供商存在巨大的延迟,事实上 10 秒确实是我收到 peter 提到的数据的延迟。

关于postgresql - 从查询到 c.JSON 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855368/

相关文章:

PostgreSQL 按名称不明确的新定义列分组

戈朗 : Type assign with another struct

Go 语言 : running routine on different process

go - 我如何知道两台服务器何时准备好接收请求?

go - 获取/文章/:article_id Not working Gin-Gonic/Gin

json - Golang/gin 从 gin.Context 解析 JSON

postgresql - 在 PostgreSQL 中解码十六进制 - 出现错误 "odd number of digits"

postgresql - 选择两个表中的所有列

sql - 如何使用 ozzo-dbx 包加入 PostgreSQL?

json - 来自结构指针的字符串