# Difference between find\_by\_sql and Ruby find?

**URL:** <https://www.ruby-forum.com/t/difference-between-find-by-sql-and-ruby-find/145710>\
**Category:** Rails\
**Created:** [August 13, 2008, 7:21am UTC](https://www.ruby-forum.com/t/difference-between-find-by-sql-and-ruby-find/145710 "2008-08-13T07:21:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 7:21am UTC](https://www.ruby-forum.com/t/difference-between-find-by-sql-and-ruby-find/145710/1 "2008-08-13T07:21:08Z")

</div>

Hi,  
What is the difference between find\_by\_sql and ruby find method?  
Where I use find\_by\_sql ? and Where I use find query?  
Is there any performance related difference between them? If yes then,  
which is feasible to use?

Thanks & Regards,  
Tushar

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 7:37am UTC](https://www.ruby-forum.com/t/difference-between-find-by-sql-and-ruby-find/145710/2 "2008-08-13T07:37:02Z")

</div>

It’s not the Ruby find, it’s the Rails find method. find\_by\_sql will  
let you write your own SQL query for especially complex queries or  
perhaps you don’t like the way Rails is doing it. the find method just  
puts in all the stuff like "SELECT \* FROM `table`"… I suggest getting  
yourself a good ruby book and a good rails book and learning about  
each and the differences.

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 11:55am UTC](https://www.ruby-forum.com/t/difference-between-find-by-sql-and-ruby-find/145710/3 "2008-08-13T11:55:56Z")

</div>

Hi Tushar

The call which one to use and when is completely developer’s call.  
Of course find\_by\_sql is faster than Rails finder method but it is  
subjected to scalability.

check-out this link for more:

> **[Optimization Tips: Ruby on Rails and MySQL](https://garrickvanburen.com/optimization-tips-ruby-on-rails-and-mysql/)**
>
> Stop. I’ve uncovered these tips after (at least) the 3rd refactoring effort of some fairly simple, straight-forward Rails code. Rails is great for getting ideas prototyped super fast. These t…

P.S. It’s better to use Rails finder methods so that your application  
will remain database independent.

On Aug 13, 1:21 am, Tushar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/difference-between-find-by-sql-and-ruby-find/145710/4 "2008-08-13T12:09:25Z")

</div>

On 13 Aug 2008, at 10:55, Gourav T. wrote:

> Hi Tushar
> 
> The call which one to use and when is completely developer’s call.  
> Of course find\_by\_sql is faster than Rails finder method but it is  
> subjected to scalability.

find just calls find\_by\_sql so there’s not going to be a big speed  
difference due to the use of find itself (for equivalent sql,  
obviously if you can write a faster query than the one find generates  
then find\_by\_sql will be faster)

Fred

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/difference-between-find-by-sql-and-ruby-find/145710/5 "2008-08-13T16:31:05Z")

</div>

Unless that find is in a loop that is called million times per request  
i believe performance is not an issue. However find\_by\_sql makes code  
uglier so unless you really need it don’t use it.

On Aug 13, 1:08 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
