select-n-plus-1

Using Entity Framework navigation properties without creating lots of queries (avoiding N+1)

巧了我就是萌 提交于 2019-12-01 10:30:25
I've been using Entity Framework Profiler to test my data access in an MVC project and have come accross several pages where I'm making far more db queries than I need to because of N+1 problems. Here is a simple example to show my problem: var club = this.ActiveClub; // ActiveClub uses code similar to context.Clubs.First() var members = club.Members.ToList(); return View("MembersWithAddress", members); The view loops through Members and then follows a navigion property on each member to also show their address. Each of the address requests results in an extra db query. One way to solve this

Hibernate Subselect vs Batch Fetching

房东的猫 提交于 2019-11-30 06:51:02
Hibernate provides (at least) two options for getting around the N+1 query problem. The one is setting the FetchMode to Subselect, which generates a select with a IN-clause and a subselect within this IN-clause. The other is to specify a BatchSize, which generates a select with a IN-clause containing the parents' IDs. Both work but I find that the Subselect option often runs into performance problems due to the query for the parents being complex. On the other hand, with a large BatchSize (say 1000), the number of queries and complexity of those queries are very small. My question is thus:

Preventing N+1 queries in Rails

☆樱花仙子☆ 提交于 2019-11-28 12:50:01
I've seen a few examples of passing an :include hash value when calling one of ActiveRecord's find methods in Rails. However, I haven't seen any examples of whether this is possible via relationship methods. For example, let's say I have the following: def User < ActiveRecord::Base has_many :user_favorites has_many :favorites, :through => :user_favorites end def Favorite < ActiveRecord::Base has_many :user_favorites has_many :users, :through => :user_favorites end def UserFavorite < ActiveRecord::Base belongs_to :user belongs_to :favorite end All the examples I see show code like this: User

Preventing N+1 queries in Rails

蹲街弑〆低调 提交于 2019-11-27 07:12:50
问题 I've seen a few examples of passing an :include hash value when calling one of ActiveRecord's find methods in Rails. However, I haven't seen any examples of whether this is possible via relationship methods. For example, let's say I have the following: def User < ActiveRecord::Base has_many :user_favorites has_many :favorites, :through => :user_favorites end def Favorite < ActiveRecord::Base has_many :user_favorites has_many :users, :through => :user_favorites end def UserFavorite <

What is the “N+1 selects problem” in ORM (Object-Relational Mapping)?

跟風遠走 提交于 2019-11-25 21:34:49
问题 The \"N+1 selects problem\" is generally stated as a problem in Object-Relational mapping (ORM) discussions, and I understand that it has something to do with having to make a lot of database queries for something that seems simple in the object world. Does anybody have a more detailed explanation of the problem? 回答1: Let's say you have a collection of Car objects (database rows), and each Car has a collection of Wheel objects (also rows). In other words, Car → Wheel is a 1-to-many