2

使用 Rails 3.2、Ruby 1.9、will_paginate。我有 100,000 条记录,并测试了以下查询:

# 1000+ms to load
@shops = Shop.where(:shop_type => @type).paginate(:include => :photos, :page => params[:page], :per_page => 25, :order => 'created_at DESC')

# 1000+ms to load
@shops = Shop.paginate(:include => :photos, :page => params[:page], :per_page => 25, :order => 'created_at DESC')

# 1000+ms to load
@shops = Shop.order('created_at DESC').limit(25)

# 1+ms to load
@shops = Shop.paginate(:include => :photos, :page => params[:page], :per_page => 25)

我注意到该order选项非常慢。测试结果表明这will_paginate不是问题。

我应该如何更改它以便order可以加快查询速度。

4

1 回答 1

5

是的,尝试在订单列上创建索引。它会加快速度。

于 2013-10-02T15:24:28.357 回答