`
clark1231
  • 浏览: 254630 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

joins与include的区别

阅读更多

  :joins和:include的用法比较类似,但是二者又有一定的区别,这两个都能提高效率,让我们谈一下,他们的优点和区别吧 
      1>: 优点: 举个例子 
           accounts表和blogs表的关系是一对多 
      1)blogs = Blog.find(:all)  # 查询一次blogs表 
           blogs.each {|blog| blog.account.email}   # 循环多少次,就查询多少次accounts表 
      2)blogs = Blog.find(:all, :include => :account)  #查询了blogs表和accounts表各一次 
           blogs.each {|blog| blog.account.email}   # 不再查询,因为已经加载了accounts表 
      总结: :include 减少了查询表的次数,提高了访问数据库的效率,:joins和:include都可以减少查询表的次数 
      注意: 前面的:include预加载,也可以继续优化,也就是使用效率更高的:joins,这种方式叫做‘携带加载’,举例:blogs = Blog.find(:all, :joins => :account, :select => "blogs.*, accounts.email as accounts_email" ) 

     2>:  :joins和:include的区别 
     这两个都是联表查询,可以提高效率,但是:include 是预加载,也就是将关联的信息会预加载到内存,那么当我们用到预加载的内容时,使用这个会很好,如果不用到预加载的内容时,则不要用:include,而要用:joins,例如我们把关联表的某些数据用到条件里时,而不用到页面显示,这时就用:joins,具体的例子: blogs = Blog.find(:all, :include => :account, :conditions => {:accounts => {:id => 1}} ) 

分享到:
评论

相关推荐

    esp8266 mesh程序,亲测可用

    2. Every node sends one packet to server per 7 seconds after it joins mesh network. 3. Every node gets mac address of all devices working in mesh per 14 seconds after it joins mesh Root device uses ...

    HP_Vertica_7.1.x_SQL_Reference_Manual

    Joins are a fundamental aspect of SQL, allowing the combination of records from two or more tables based on a related column between them. HP Vertica supports various types of joins, including inner ...

    Rails3的ActiveRecord 查询API.doc

    在 Rails 2.x 中,许多使用哈希参数的查询方法如 `:conditions`, `:include`, `:joins` 等在 Rails 3.1 中被标记为过时,虽然在3.1版本中仍然可用,但计划在 Rails 3.2 中完全移除。这些变化主要是为了引入一个新的...

    salesforce dev 501 Notes

    - **Joins**: Retrieving data from multiple related objects using JOIN clauses. - **Filtering**: Using WHERE clauses to filter records based on specific criteria. - **Sorting and Limiting**: ORDER BY ...

    EFQuerySamples

    4. **IncludeAndThenInclude**:演示如何使用`Include`和`ThenInclude`方法来加载关联的数据,避免N+1查询问题。 5. **LambdaExpressions**:使用Lambda表达式进行查询,这是LINQ的主要组成部分,可以更直观地表达...

    Ruby on Rails: the scope method

    - **关联查询**:在具有关联关系的模型中,可以通过定义`scope`来查询与当前模型相关的记录。 - **控制器中的使用**:可以在控制器中使用`scope`来进一步过滤或处理查询结果,例如通过`has_scope` gem来实现基于HTTP...

    SAS.9.2.SQL.Procedure.Users.Guide

    - Direct support for common database operations like joins and aggregations. - Ability to interact with external databases directly. - **Use Cases**: - When working with large datasets or when ...

    Apress - Oracle SQL Recipes A Problem Solution Approach (November 2009) (ATTiCA).pdf

    4. **Complex Data Manipulation**: The book provides recipes for handling complex data manipulation tasks, such as transforming data structures, pivoting data, and performing advanced joins and ...

    geoprocessing scripts with python.pdf

    - **Vector Data**: Vector data might need operations such as buffering, clipping, or spatial joins. - **Temporal Data**: Temporal data, which includes timestamps or dates, might require filtering by ...

    sequelize-node-ORM

    Sequelize提供了丰富的查询API,可以进行简单的CRUD操作,也可以执行复杂的联接(Joins)、聚合(Aggregations)和子查询(Subqueries)。例如,获取所有用户和他们相关的帖子: ```javascript User.findAll({ ...

    Sequelize01

    Sequelize提供了一个强大的查询构建器,允许你构建复杂的查询,包括JOINs、子查询、聚合函数等。例如,查找所有有角色的用户: ```javascript User.findAll({ include: [Role] }); ``` 七、优化与性能 优化主要...

    VisualFoxPro6.0中文版命令手册.pdf

    Some common clauses used in VFP commands include: - **Range:** Specifies the records on which the command will operate. This can be one of the following: - **ALL:** Indicates all records. - **NEXT ...

    SSD7 选择题。Multiple-Choice

    View Assessment Result: Multiple-Choice Quiz 2 Your performance was as follows: 1.... The degree of a table is the number of _____ in the table....--------------------------------------------------...

Global site tag (gtag.js) - Google Analytics