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 秒。而如果我使用“數(shù)據(jù)庫/sql”mysql.QueryRow()它不到 500 毫秒。知道為什么嗎?更新:減速發(fā)生在這個callback_query.go看起來這個函數(shù)正在循環(huán)遍歷整個 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條經(jīng)驗 獲得超8個贊
你是倒著做的。Find
執(zhí)行查詢并獲取行。您想在獲取所有內(nèi)容之前db.Table("Kijiji").Where("adId = ?", m["id"][0]).Find(&listing)
應(yīng)用 where 條件。
- 1 回答
- 0 關(guān)注
- 283 瀏覽
添加回答
舉報
0/150
提交
取消