Why does Entity Framework generate slow overengineered SQL?

前端 未结 2 1839
无人及你
无人及你 2021-01-14 06:04

I have this code:

DbSet table = ...// stored reference

var items = from n in table where
            n.Name.ToUpper().Contains(searchString         


        
2条回答
  •  借酒劲吻你
    2021-01-14 06:40

    It generates the resulting SQL by transforming an expression tree. It appears overengineered (for example, using a subquery) as a side-effect of the way it's done that transformation. The details of the transformation are proprietary and complex, and the results are not supposed to be human-readable.

    The question is not entirely clear - and you are trying to solve a problem which I believe may not be a problem. Try comparing the generated query and your own - I would guess the query optimiser will make short work of such an easy optimisation.

    My guess (and that's probably the best kind of answer you can get here unless a LINQ to Entities MS dev comes along) is that they're doing exactly that: generating the most effective query, but leaving the head-hurtingly-difficult job of optimising the query to the bit they've already put hundreds or thousands of man-days into: the query optimiser in SQL Server.

提交回复
热议问题