Questions tagged [select-n-plus-1]

Use this tag for the ORM problem known as select N+1 queries.

Use this tag should be used for questions related to the ORM problem known as N+1 queries.

See this question for reference: What is the "N+1 selects problem" in ORM (Object-Relational Mapping)?

56 questions
1795
votes
18 answers

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

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…
Lars A. Brekken
  • 20,385
  • 3
  • 23
  • 27
19
votes
2 answers

How can you avoid NHibernate N+1 with composite key

EDIT I remade an entire project for this one problem. And thus, I remade the question. I want to be able to efficiently avoid N+1 and Cartesian joins joining together a 4 level deep entity with a composite key on the third level. I am looking for…
BradLaney
  • 2,245
  • 1
  • 18
  • 28
19
votes
2 answers

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

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…
GWigWam
  • 1,600
  • 3
  • 27
  • 29
15
votes
2 answers

Hibernate Subselect vs Batch Fetching

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,…
Zecrates
  • 2,782
  • 6
  • 30
  • 47
13
votes
3 answers

Django N+1 query solution

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…
orokusaki
  • 48,267
  • 47
  • 159
  • 244
7
votes
3 answers

JPA @OneToOne select lists with N+1 queries

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 Childs, the JPA engine (Hibernate in this case) make 1+n queries. Here is the log of the…
7
votes
4 answers

Hibernate creating N+1 queries for @ManyToOne JPA annotated property

I have these classes: @Entity public class Invoice implements Serializable { @Id @Basic(optional = false) private Integer number; private BigDecimal value; //Getters and setters } @Entity public class InvoiceItem implements…
Mateus Viccari
  • 6,382
  • 13
  • 59
  • 95
6
votes
1 answer

Select n+1 problem

Foo has Title. Bar references Foo. I have a collection with Bars. I need a collection with Foo.Title. If i have 10 bars in collection, i'll call db 10 times. bars.Select(x=>x.Foo.Title) At the moment this (using NHibernate Linq and i don't want…
5
votes
3 answers

Which is the fastest performing way to avoid n+1 issues and why?

I'm looking to add some utility methods to help avoid a lot of n+1 issues in a legacy application. The common pattern is this: select a.* /* over 10 columns */ from [table-A] a where /* something */ Retrieved into a collection of ClassA record…
Keith
  • 133,927
  • 68
  • 273
  • 391
5
votes
4 answers

Why is the n+1 selects pattern slow?

I'm rather inexperienced with databases and have just read about the "n+1 selects issue". My follow-up question: Assuming the database resides on the same machine as my program, is cached in RAM and properly indexed, why is the n+1 query pattern…
Perseids
  • 10,688
  • 5
  • 32
  • 62
4
votes
3 answers

How to add information from the DB to an existing array of objects?

I have an array in the following format; // echo '
' . var_export($this->viewableFields, true) . '
'; array ( 0 => (object) array( 'formId' => '4', 'userId' => '7' ), 1 => (object) array( 'formId' => '4', …
jonboy
  • 2,659
  • 6
  • 28
  • 67
4
votes
1 answer

Why would identical NHibernate queries run an order of magnitude slower in a WinForm project than an MSTest unit test?

I have an app that is run as a service in production, but we do some manual testing with a simple GUI - practically nothing going on in it, it's just a wrapper with a textbox for input. I recently changed my database schema and updated my mappings…
Paul Phillips
  • 5,605
  • 20
  • 34
3
votes
1 answer

Is it possible to count the number of SQL queries with Flask / SQLAlchemy / Pytest / SQLite?

I have a Flask project which uses SQLAlchemy and a database. For testing, I replace the database by an SQLite database. Now I would like to run some of the views and test for the number of queries executed. Essentially, I want to avoid to…
Martin Thoma
  • 91,837
  • 114
  • 489
  • 768
3
votes
1 answer

Using Entity Framework and where queries without creating lots of queries (avoiding N+1)

I've been using a design pattern that after using the Entity Framework Profiler seems like it might be quite foolish. I've been extended my entity classes to have properties that are filtered views of a collection associated with that entity. Like…
3
votes
2 answers

Preventing N+1 queries in Rails

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…
1
2 3 4