Gaurav Sharma
Gaurav Sharma

Reputation: 23

Which is more efficient, order by in the database or sort in Ruby?

I have a heavy query that takes a lot of time. It has multiple joins, and I am doing group by and order by on multiple table's columns. Should I do this order by or group by at ruby application level, or continue doing it in a MySQL query?

Upvotes: 2

Views: 280

Answers (1)

tadman
tadman

Reputation: 211600

Generally you should do as much filtering and ordering in the database as possible, and this is especially true when you're only displaying a small subset of all possible records as is the case when paginating.

You'll want to do ordering manually if and only if the logic for ordering is so crazy convoluted it's impractical or impossible to do it in the database itself.

It's worth mentioning that database indexes help considerably when accessing ordered information. Use them whenever you have an ORDER BY clause on your query for best results.

Upvotes: 4

Related Questions