新手问题 分页查询效率问题的疑问

luffycn · October 06, 2014 · Last by wym205 replied at October 06, 2014 · 2197 hits

我用的是 kaminari,

如果 Article model 有个 scope,比如 scope by_category

一般的写法是 Article.by_category.page params[:page]

如果是先一次性把所有的 article list 查询出来获取一个数组列表,再用 page 把数组分割 一旦数据量很大时,效率岂不是很慢?

如果是类似 object.collections.next_collections.next_next_collections active record 在查询时是先进行 object.collections,再进行 object.collections.next_collections. 依此类推嘛? 如果是这样,只要当中有一步骤数据量巨大时,应该会很慢吧?

不是的,scope 是可串的,SQL 直到最后需要 array 的时候才会生成。

另外,page 的实现一般是用 offset, 就是单页的数据量。

where 方法会产生一个新的 ActiveRecord::Relation 对象实例,多个 where 方法串联只会产生一堆中间对象,最后再交给 arel 生成 sql 比如

@articles1 = Article.where(category: 'foo') #=> ActiveRecord::Relation#{where_values={category: 'foo'}}
@articles2 = @articles1.where(title: 'bar') #=> ActiveRecord::Relation#{where_values={category: 'foo'. title: 'bar'}}
@articles2.to_a #=> generate sql...

要了解具体实现可以翻翻 ActiveRecord::QueryingActiveRecord::RelationActiveRecord::FinderMethods 这三个模块的代码。 另外推荐 pat 写的那篇 20000 leagues under ActiveRecord

kaminari 的问题是会附属一个 select count(*) from xxx where ... 的 sql 语句,在数据量大的时候这个语句很慢

You need to Sign in before reply, if you don't have an account, please Sign up first.