Why does Entity Framework generate slow overengineered SQL?

前端 未结 2 1841
无人及你
无人及你 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:44

    It does an extra Select but Selects have no cost associated. You can see the estimated query plan and it would show 0% cost in that. It does that because EF is compatible with various RDBMS systems like Oracle, SQL server and to ensure maximum compatibility it might be doing this.

    However I do agree that Entity Framework generates UGLY sql. The example that you gave was a very simple Linq query and you'll see more of that ugliness when your queries start becoming complex.

    1) While this may or may not answer your answer, I would say use a micro ORM like PetaPoco:

    https://github.com/toptensoftware/PetaPoco

    or Dapper.Net

    https://github.com/SamSaffron/dapper-dot-net

    I have been using it in one of my project and I am completely satisfied with the raw speed that you get with plain Ado.Net.

    2) My 2nd suggestion would be always use stored procedure for atleast Select statements. For inserts, updates and deletes you should probably use EF and take advantage of change tracking mechamism and that would save your time from writing tedious queries but atleast for Select statements you should try to use plain SQL and that gives you more freedom over what SQL is generated.

提交回复
热议问题