select-n-plus-1

N+1 in rails relations with active records?

耗尽温柔 提交于 2020-01-03 05:41:07
问题 I have four models Group Reports Comments user Group => has_many => Reports Report => has_many => Comments Comment => Belongs_to => User When i want to show a group I do something like <%= @group.name %> <%= @group.reports.includes(:comments).each do |report| %> <%= report.name %> <% report.comments.each do |comment| %> <%= comment.name %> <%= comment.user.name %> <% end %> <% end %> What is the best way to solve N+1 Query problems in this case ?? 回答1: Perhaps @group.reports.includes(

Django N+1 query solution

旧城冷巷雨未停 提交于 2020-01-01 08:02:19
问题 I visited http://guides.rubyonrails.org/active_record_querying.html after talking with a peer regarding N+1 and the serious performance implications of bad DB queries. ActiveRecord (Rails): clients = Client.includes(:address).limit(10) Where client's have addresses, and I intend to access them while looping through the clients, Rails provides includes to let it know to go ahead and add them to the query, which eliminates 9 queries right off the bat. Django: https://github.com/lilspikey/django

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

╄→尐↘猪︶ㄣ 提交于 2019-12-19 10:18:41
问题 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

Hibernate Subselect vs Batch Fetching

半城伤御伤魂 提交于 2019-12-18 12:24:34
问题 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

n+1 query not working

北城余情 提交于 2019-12-11 09:46:02
问题 In the below code I expect the n+1 query problem to occur, but it's not happening. User.java : import java.util.*; public class User { private long userId; private String firstName; private Set phones; public User() { System.out.println("0-arg constructor :User"); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public long getUserId() { return userId; } public void setUserId(long userId) { this.userId = userId; }

NHibernate N+1 fetch problem

自闭症网瘾萝莉.ら 提交于 2019-12-11 06:25:50
问题 I have a entity and fluent mapping that look like this. public class Client : EntityWithTypedId<long> { [Length(Max=50)] public virtual string GivenName { get; set; } public virtual IList<Address> Addresses { get; set; } } public class ClientMap : ClassMap<Client> { public ClientMap() { Schema("dbo"); Table("Client"); Id(x => x.Id, "ClientId").GeneratedBy.Identity(); Map(x => x.GivenName, "GivenName"); HasManyToMany(x => x.Addresses) .FetchType.Join() .Cascade.AllDeleteOrphan() .Table(

JPA @OneToOne select lists with N+1 queries

时间秒杀一切 提交于 2019-12-10 17:33:37
问题 I'm actually trying to use JPA @OneToOne annotation to link a Child entity to its Parent . It's working well, except the fact that when getting a list of Child s, the JPA engine (Hibernate in this case) make 1+n queries. Here is the log of the Hibernate queries : select child0_.id as id1_0_, child0_.parent as parent3_0_, child0_.value as value2_0_ from child child0_ select parent0_.id as id1_1_0_, parent0_.something as somethin2_1_0_ from parent parent0_ where parent0_.id=? select parent0_.id

EF Core nested Linq select results in N + 1 SQL queries

∥☆過路亽.° 提交于 2019-12-08 14:42:18
问题 I have a data model where a 'Top' object has between 0 and N 'Sub' objects. In SQL this is achieved with a foreign key dbo.Sub.TopId . var query = context.Top //.Include(t => t.Sub) Doesn't seem to do anything .Select(t => new { prop1 = t.C1, prop2 = t.Sub.Select(s => new { prop21 = s.C3 //C3 is a column in the table 'Sub' }) //.ToArray() results in N + 1 queries }); var res = query.ToArray(); In Entity Framework 6 (with lazy-loading off) this Linq query would be converted to a single SQL

Django N+1 query solution

匆匆过客 提交于 2019-12-04 00:18:49
I visited http://guides.rubyonrails.org/active_record_querying.html after talking with a peer regarding N+1 and the serious performance implications of bad DB queries. ActiveRecord (Rails): clients = Client.includes(:address).limit(10) Where client's have addresses, and I intend to access them while looping through the clients, Rails provides includes to let it know to go ahead and add them to the query, which eliminates 9 queries right off the bat. Django: https://github.com/lilspikey/django-batch-select provides batch query support. Do you know of other libraries or tricks to achieve what