新手问题 加 Index 对范围搜索有帮助吗?

cqcn1991 · May 23, 2015 · Last by azhao replied at May 23, 2015 · 1298 hits

比如,我会用到这样的方法来提取内容

scope :best_of_the_week, ->(time) do
   start_time = time.beginning_of_week
   end_time = time.end_of_week
   where("created_at > ? AND created_at < ?", start_time, end_time).where('votes_count > ?', 300).order('votes_count DESC').first(8)
 end

不知道加 index 有没有帮助?

另外,想问下可以把两个where合并起来么?会不会好一点?

explain

#1 楼 @hooooopo 我自己的试了下,效果是没有...

vote_count 经常变化吧,加 index 不划算

试一下 加一个 created_at 和 vote_count 的联合索引

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