EF Core Performance

Last week , I worked on optimizing the runtime of loading inventory items . The problem is a “Cartesian Explosion” that is related to performing joins . When we perform a join , one’s table repeat the X number of time the matched records of another joined table . There are a few ways to avoid the cartesian explosion especially when loading related entities in a huge database :-

  1. AsSplitQuery() introduced by EF core 5.0 . It only works when using Include(). Instead of joining the tables , split queries generate additional SQL query instead of single queries .
  2. IncludeOptimized() introduced by EF core pro . It works similarly to the first method as it drastically decreases the amount of data transferred to avoid generating excessive data that is needed .
  3. Explicitly loading the data needed in the single query .

In conclusion , unfortunately there isn’t one solid solution for loading all entities that fit every scenario . IncludeOptimized() does not support AsNoTracking() and cannot be mixed with Include() . In hindsight , explicit loading should be the way to fix the cartesian explosion problem as there are limitations to using built in functions .

Leave a Reply