在性能和邏輯方面,Rails的Active Record查詢中「排序」「過濾器」(Desc或Asc)的位置是否重要?可交換性和活動記錄範圍的順序
例如是1的SAM以下範圍作爲範圍2
SCOPE 1
scope :default_stream, -> { order(deal_end_date: :asc) } # this is the "sorting query"
scope :scope_1,
lambda { default_stream.where('deal_start_date <= ? AND deal_end_date >= ?', Time.zone.now, Time.zone.now).where(is_cool: true) }
SCOPE 2
scope :scope_2,
lambda { Deal.all.where('deal_start_date <= ? AND deal_end_date >= ?', Time.zone.now, Time.zone.now).where(is_cool: true).order(deal_end_date: :asc) }
我應該先用「排序查詢「那麼其他過濾器(scope1)還是相反(scope2)?
不知道是否有衝擊,但讓我說,交易的數量是非常重要的(> 100000)
所以先把非排序查詢(其中('deal _..... Time.zone.now).where(is_cool:true))不會更快?我認爲,因爲它會輸出有限/更少的行數,所以排序查詢(order(deal_end_date::asc))作爲第二步將比排序整個數據庫行更快...你說的實際上是(bar)'和'.where(foo).order(bar)的結果,它是通過活動記錄自動爲此優化的? – Mathieu
@Mathieu這根本不是那種工作方式。 ''會做**完全相同的事情**,它們將產生*相同* SQL和*相同*記錄。排序/過濾由數據庫完成,而不是由Ruby完成。'order'或'where'發生「第一次」; Rails將它們全部(不管你以什麼樣的順序調用它們)並將它們變成**一個** SQL查詢 – meagar
好吧,我明白了,將會檢查.to_sql的疑問:)。非常感謝我指出了正確的方向 – Mathieu