Search code examples
sqllinqentity-framework-4table-per-type

Entity Framework Generated SQL - Table per Type - Performant?


I am changing my old ADO.Net ways for the Entity Framework. My Products class is made up from about 20 tables (3 of which I have combined so far into the single class), there are 5 Product type tables also, so I have made the Product class Abstract and each type has its own class using the lovely inheritance that the Entity Framework provides using the Table per Type methodology as below. So effectively 3 tables on a product class and 5 product type tables, each with its own class derived from Product.

<there was a screenshot here, I don't have the rep to post it though!>

I have been keeping an eye on what this EDM is doing under the covers with the SQL Server Profiler as I've been working. When I run a simple Linq to Entities Query as below:

var model = from p in Product.Products
                    where p.archive == false && ((Prod_ID == 0) || (p.ID == Prod_ID))
                    select p;

Which brings back all types of products that are not archived, and may potentially have an ID search in there as well, the sql profiler shows me an 800 line piece of SQL!!

Is this normal? And is this performant? Or have I sent the Entity Framework crazy?

<I tried to post the SQL too, but there was too much of it for the post>

So please, just a sanity check and any advice!

Regards,

Mark


Solution

  • The Entity Framework can kick out some very large queries, but generally the SQL Server query optimiser does its thing and the query executes with the server barely noticing.

    Beyond being generally sensible, sensible I personally tend not to worry about performance and optimisation until a performance analysis tells me I've got a bottleneck; if you're already using SQL profiler, does the query take a relatively long time to execute? Have you had a look at the execution plan and does it look ok?