0

When finding objects in Rails, is it better to combine query conditions first then performing the query or start with a less strict criteria and perform array operations on the results to narrow down what I want. I want to know which one would perform faster and/or the standard use.

1 Answer 1

1

If possible, narrowing results in SQL is generally faster, so add as many query conditions as you need, if the query is not too complex. Running code in Ruby to narrow results is not faster than SQL because Ruby is interpreted anyways.

However, narrowing results with SQL benefits from:

  • Searching results via indices which is much faster, if there is a query condition on a column which is indexed (eg. add an index for created_by, then find results created within a specific timeframe), or a sort clause on that column
  • Less communication from the SQL database to Ruby/Rails (if on the same computer, then a small improvement in speed, otherwise where the database is on a separate computer, uses less bandwidth - results returned faster)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.