简体   繁体   English

Rails排序或排序查询很慢

[英]Rails sorting or ordering query is slow

Using Rails 3.2, Ruby 1.9, will_paginate. 使用Rails 3.2,Ruby 1.9和will_paginate。 I have 100,000 records, and tested the following queries: 我有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)

I noticed that the order option is incredibly slow. 我注意到order选项的速度非常慢。 Test results show that will_paginate is not a problem. 测试结果表明will_paginate不是问题。

How should I change it so that the query with order can be sped up. 我应该如何更改它order可以加快查询order

Yes, try to create index on the order column. 是的,尝试在订单列上创建索引。 It will speed it up. 它将加快速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM