Reputation: 665
I am experiencing performance issues when using limit
on thousands of records.
Are there any alternatives too limit
that active record provides or workarounds?
My query is Spot.where(<where_conditions>).limit(20)
Some solutions I have thought about;
Using a where
clause to limit the records instead where("created_at > ? and created_at < ?", 10.days.ago, 10.days.ago)
Pluck the ids
from the main where
query and then make an additional query to take the top 20 ids = where(<where_conditions>).order('created_at ASC').pluck(:id)
then where(id: ids.take(20)).order('created_at ASC')
. The concern there would be memory usage.
Upvotes: 0
Views: 285
Reputation: 322
Consider your where_condition
:
All columns in where_condition
indexed
? Did them indexed
in right order as in where_condition
?
If you did not index all columns in where_condition
. You can use deferred_join
to improve performance
Upvotes: 1