Entity Framework Core 5 — Split Queries

  • Microsoft.EntityFrameworkCore.Design 5.0.3
  • Microsoft.EntityFrameworkCore.SqlServer 5.0.3
  • Microsoft.EntityFrameworkCore.Tools 5.0.3
Course: Information Systems
Student: John
Student: Mark
Course: Computer Science
Student: Luke
Id  Name                  Id  CourseId   Name
1 Information Systems 1 1 John
1 Information Systems 2 1 Mark
2 Computer Science 3 2 Luke
  • In the first query, it will be returned only the information related to the Courses
  • In the second query, it will be returned only the information related to the Students
// First query:
Id Name
1 Information Systems
2 Computer Science
// Second query:
Id CourseId Name Id
1 1 John 1
2 1 Mark 1
3 2 Luke 2
  • Split query avoids performance issues associated with JOINs
  • A problem that the Split query solves, is the problem known as “cartesian explosion”. An example of this problem is when a blog has multiple related posts, and the rows for these posts will duplicate the blog’s information. The more one-to-many relationships are loaded, the amount of duplicated data may grow and adversely affect the performance of your application.
  • There are no guarantees for data consistency for multiple queries, unlike when there is only a single query. When using multiple queries, if the database is updated concurrently when executing the queries, the resulting data may not be consistent.
  • Each query currently implies an additional roundtrip to the database, and multiple network roundtrips can degrade performance.
  • Some databases allow only a single query to be active at any given point, so all results from earlier queries must be buffered in your application’s memory before executing later queries, which leads to increased memory requirements.
https://github.com/henriquesd/EntityFrameworkCore5Examples

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store