Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I understand the responses telling me to batch my data, not load the whole entity, etc; and in general I agree with that approach. However, in this scenario, we are selecting a single entity - because it comes from a normalised database, the involves joining across multiple tables. </p> <p>In this exact example, the horrible query generated by EF still executes in under 0.2 seconds. If I cared to hand write the query, it would probably take 1/10 of that again. As it is, it's definitely fast enough. The size of the whole entity on the wire is &lt;6Kb; which, again, I consider small enough.</p> <p>So for me to be batching this single entity across multiple requests, simply doesn't work in this example. Clearly if I was using straight ADO.NET and web services/WCF, I wouldn't be having this issue. Anyway, on to the answer:</p> <p>I already stated the compiling the EF query didn't help. However, it seems that this may be because RIA Services was applying the 'Where EntityID = ID' condition on top of my original query, and blowing my compiled query away.</p> <p>If I simply do this in my domain service:</p> <pre><code>(from e in ctx.Entities .Include("SubEntity") .Include("SubEntity.SubEntity") // and so on, X20... where e.EntityID == id select e).FirstOrDefault(); </code></pre> <p>This line of code takes nearly the whole time to execute, yet the SQL is very fast. It's just slow to get to the SQL Server. So this implies to me that EF is taking a long time to generate the query.</p> <p>To fix the issue, I made a precompiled query that selects my entity directly by ID, and materialises it rather than return IQueryable:</p> <pre><code> private static Func&lt;DataContext, Guid, Entity&gt; getEntityByEntityID = CompiledQuery.Compile&lt;DataContext, Guid, Entity&gt;( (ctx, id) =&gt; (from e in ctx.Entities .Include("SubEntity") .Include("SubEntity.SubEntity") // and so on, X20... where e.EntityID == id select e).FirstOrDefault()); </code></pre> <p>Then I expose a new operation on my domain service to use the compiled query:</p> <pre><code>public Entity GetEntityByEntityID(Guid entityID) { return getEntityByEntityID(this.ObjectContext, entityID); } </code></pre> <p>Result: it is now a little bit slower the first time this is called. For subsequent calls, the operation is now averaging around .5 seconds for the whole service call.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload