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
- Removing
order by
(about two times faster) - sorting in ascending order (clustered indices are in ascending and can't be otherwise)
- adding
option(loop join)
(extremely fast) - using left outer join
- 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.