为了账号安全,请及时绑定邮箱和手机立即绑定

Gorm 非常慢

Gorm 非常慢

Go
开满天机 2022-05-18 13:56:46
Gorm 似乎非常缓慢。type Ad struct {    AdId        string `json:"adId" gorm:"column:adId"`    Attributes  string `json:"attributes" gorm:"column:attributes"`    Title       string `json:"title" gorm:"column:title"`    Description string `json:"description" gorm:"column:description"`    Image       string `json:"image" gorm:"column:image"`    Url         string `json:"url" gorm:"column:url"`    Price       int    `json:"price" gorm:"column:price"`    Address    string    `json:"address" gorm:"column:address"`    Latitude   float64   `json:"latitude" gorm:"column:latitude"`    Longitude  float64   `json:"longitude" gorm:"column:longitude"`    PostedDate time.Time `json:"postedDate" gorm:"column:postedDate"`}db.Table("Kijiji").Find(&listing).Where("adId = ?", m["id"][0])最后一次查询大约需要 60 秒。而如果我使用“数据库/sql”mysql.QueryRow()它不到 500 毫秒。知道为什么吗?更新:减速发生在这个callback_query.go看起来这个函数正在循环遍历整个 20k 记录表。func queryCallback(scope *Scope) {    ..........        if rows, err := scope.SQLDB().Query(scope.SQL, scope.SQLVars...); scope.Err(err) == nil {            defer rows.Close()            columns, _ := rows.Columns()            for rows.Next() {                scope.db.RowsAffected++                elem := results                if isSlice {                    elem = reflect.New(resultType).Elem()                }                scope.scan(rows, columns, scope.New(elem.Addr().Interface()).Fields())            ..................        }    }}我该如何解决?
查看完整描述

1 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

你是倒着做的。Find执行查询并获取行。您想在获取所有内容之前db.Table("Kijiji").Where("adId = ?", m["id"][0]).Find(&listing)应用 where 条件。



查看完整回答
反对 回复 2022-05-18
  • 1 回答
  • 0 关注
  • 295 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号