Custom query ActiveRecord vs find_by_sql Download

I have a user request similar to this

self.account.websites.find(:all,:joins => [:group_websites => {:group => :users}],:conditions=>["users.id =?",self])

where self is a custom object

I manage to create equivalent SQL for the same

This is how it looks

sql = "select * from websites INNER JOIN group_websites on group_websites.website_id = websites.id INNER JOIN groups on groups.id = group_websites.group_id INNER JOIN group_users ON (groups.id = group_users.group_id) INNER JOIN users on (users.id = group_users.user_id) where (websites.account_id = #{account_id} AND (users.id = #{user_id}))"

With a good understanding of SQL and ActiveRecord, I assumed that (which most agree) the result obtained from the query above may take longer than the result obtained from find_by_sql (sql) one .

But amazing

When I ran these two I found that a custom ActiveRecord query leads the path from ActiveRecord "find_by_sql" during load time, here is the test result

ActiveRecord

(0,9 )

(1.0 )

find_by_sql

(1,3 )

(1.0 )

, ( , )

, , , find_by_sql , Custom Query

- .

+3
2

; , .

find_by_sql . , , , .

, : find_by_sql ():

User.find_by_sql(["select * from websites INNER JOIN group_websites on group_websites.website_id = websites.id INNER JOIN groups on groups.id = group_websites.group_id INNER JOIN group_users ON (groups.id = group_users.group_id) INNER JOIN users on (users.id = group_users.user_id) where (websites.account_id = ? AND (users.id = ?))", account_id, users.id])
+1

, , , - SQL SQL db . , Ruby , Rails SQL- ORM, , db . , 0,1 - , .

0

Source: https://habr.com/ru/post/1793802/


All Articles