2
當通過我們的短語表分頁時,返回結果需要很長時間。rails belongs_to sql語句使用NULL id
在SQL日誌,我們看到許多SQL請求它沒有任何意義,以我們:
Phrase Load (7.4ms) SELECT "phrases".* FROM "phrases" WHERE "phrases"."id" IS NULL LIMIT 1
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
這些加起來顯著。有沒有辦法來防止查詢空id ID?
這是基本的模式:
class Phrase < ActiveRecord::Base
belongs_to :user
belongs_to :response, :class_name => "Phrase", :foreign_key => "next_id"
end