0

I have a TPT inheritance in my EF model. Where there is a "Master" abstract type from which several types inherit, including "Order". There are 1700000 orders, but master has many more rows corresponding to other types.

We had a strange case in which selecting 50 orders was slower than selecting the same 50 orders, but with some other related entities included. It tracked back to database where the very simple query

select top 50 * from SAM.Master m 
join SAL.[Order] o on o.OrderMasterID = m.MasterID
order by MasterID desc

takes more than a second. (Yes, in our case, one second is actually too much). But this can be made faster either by

  1. Removing order by (about two times faster)
  2. sorting in ascending order (clustered indices are in ascending and can't be otherwise)
  3. adding option(loop join) (extremely fast)
  4. using left outer join
  5. Adding Where FormTypeID = 1 (the discriminator column in Master table which is 1 for all orders) (two times faster)

actually the only solution that yielded the same result are 3 and 5, but 3 is impossible using Entity Framework (we can't add hints to queries) and 5 is not fast enough

Any suggestions are greatly appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alireza
  • 5,421
  • 5
  • 34
  • 67

2 Answers2

3

You can use plan guides to obtain the behavior you desire. See Using Query Hints in Plan Guides for an example. The example requires the actual statement text (the T-SQL generated by EF) but you can circumvent the need to obtain the EF generated statement by using sp_create_plan_guide_from_handle.

On your next project avoid using Class Table Inheritance with a base Master object that every entity derives from...

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Thanks, I'll read and try to use your suggestion tomorrow. Btw, we're not inheriting everything from Master, only the masters of the forms we encountered in a specific process. This also happens to details, and we have a set of very interesting tools that work on Master and Detail regardless of their concrete type. If for the sake of avoiding possible problems in performance we want to abandon all benefits of inheritance, I might rather do something very radical, like using an object database, or at least changing to nhibernate (where I can extend the codes to do what I want). – Alireza Apr 09 '12 at 12:13
1

If you want to force a query hint. I think your best shout is to ether create a store procedure and use the query hint (see more here). Or maybe you can do something like this:

var items = dc.ExecuteQuery<ToSomeObject>("YourQueryWithHints").ToList();
Community
  • 1
  • 1
Arion
  • 31,011
  • 10
  • 70
  • 88
  • thanks, but unfortunately this is part of a large system with lots of features that make use of LINQ to entities and using stored procedures will block most features. – Alireza Apr 08 '12 at 11:53
  • 1
    Then you might consider using `ExecuteQuery` as some thing in between. Because you can not just query hints in Linq. – Arion Apr 08 '12 at 12:05