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)?

58 questions
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,384
  • 1
  • 19
  • 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
  • 2,013
  • 4
  • 28
  • 34
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,952
  • 6
  • 33
  • 50
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
  • 55,146
  • 59
  • 179
  • 257
8
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…
8
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
  • 7,389
  • 14
  • 65
  • 101
6
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
  • 12,584
  • 5
  • 40
  • 64
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
  • 150,284
  • 78
  • 298
  • 434
4
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
  • 124,992
  • 159
  • 614
  • 958
4
votes
4 answers

how to automatically identify n+1 queries in a django app?

Is there any tool, plugin or technique that I can use to help identify n+1 queries on a Django application? There is a gem called bullet for Rails that identifies n+1 queries and logs or pops up warnings in a number of ways but I haven't been able…
ashanbrown
  • 717
  • 8
  • 18
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
  • 6,093
  • 24
  • 34
3
votes
2 answers

"Traditional" one-to-many Query with RavenDB

I know the include-feature of RavenDB. It allows me to fetch a referenced document right away in one roundtrip to the database. But my problem is: The document i fetch in the first place is not including a reference to the "other" documents. But the…
Sven Eppler
  • 1,646
  • 1
  • 15
  • 26
3
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,729
  • 6
  • 37
  • 77
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…
1
2 3 4