Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • When to use includes or joins in rails

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 173
    Comment on it

    While working with a large relational database in rails we come to point where we have to access data from a large number of tables in a single query. Well for this rails provide us with two very powerfull active records methods i.e joins and includes. But the use of these two methods i.e joins or includes depends on the way you want you data to be accessed in your code logic. There is a basic difference between the two methods although some developers use them without keeping note of that. The most important thing to take note of over here is to find the most optimal case for using includes or joins based on their use cases.

    Includes uses eager loading where as joins uses lazy loading, both of which are powerful but can easily be abused to reduce or overkill performance.

    According to rails documentation "With includes, Active Record ensures that all of the specified associations are loaded using the minimum possible number of queries." i.e when you use includes while making query on number of models all the records associated in those models also get loaded in the server memory. Thus if you want to access data related to any included model it gets accessed without making another query on database thus reducing the amount on queries.

    User.includes(:students).where(:students => { active: true } ).all
    
    @users.each do |user|
         user.student.school_name
    end

    When iterating through each of the users and displaying the students school_name, we would normally have to retrieve the students school_name with a separate database query each time. However, when using the includes method, it has already eagerly loaded the associated students table, so this block only required a single query.

    But there is an another case where we want data to be fetched on some condition match between different table but we are not concerned about the data present in those tables. For example we want all users who are students but we don't want to display students data i.e

    User.joins(:students).where(:students => { active: true } ).all
    
    @users.each do |user|
         user.name
    end

    In the above example we only want data from users table and we are not concerned about students or any other table thus we make use of joins.

    Thus depending upon your choice both includes or joins can hinder or improve your server performance based on number of queries being fired to database server or amount of data being kept in server memory , thus use it as per the situation of your project. You can even compare the performane of the two by checking server logs.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: