4 ms·
"Sorting and filtering are best performed in the database, if possible, using ActiveRecord's tools." this needs a big fat asterisk. sometimes sorting in the d
by zht 2y ago
"Sorting and filtering are best performed in the database, if possible, using ActiveRecord's tools."
this needs a big fat asterisk.
sometimes sorting in the database requires a filesort
if you know ahead of time roughly how many results are getting sorted in-memory it absolutely can be faster to do it in-memory than ask the database to do it
- rezonant 2y agoI'm not sure what a filesort refers to here, but there are absolutely cases where :order can cause performance problems. If there is no index on the ordered field, you have to remember that the order applies first (ie before you filter the results) even though you might have written it last in your ActiveRecord chain. However, the amount of times you want to order a subset of records independent of the complete set is quite rare, as almost always you are doing some kind of paged fetch, and it makes no sense to use an indeterminate database ordering, even if you are just ordering by primary key (which is always indexed in ActiveRecord)