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 条件。
- 1 回答
- 0 关注
- 295 浏览
添加回答
举报
0/150
提交
取消