LINQ - Using where or join - Performance difference?

前端 未结 2 1621
自闭症患者
自闭症患者 2021-01-13 05:12

Based on this question: What is difference between Where and Join in linq?

My question is following:

Is there a performance difference in the following two s

2条回答
  •  温柔的废话
    2021-01-13 05:57

    My question is now, is the first one slower than the second one? Does it build a cartesic product and filters it afterwards with the where clauses ?

    If the collections are in memory, then yes. There is no query optimizer for LinqToObjects - it simply does what the programmer asks in the order that is asked.

    If the collections are in a database (which is suspected due to the myDB variable), then no. The query is translated into sql and sent off to the database where there is a query optimizer. This optimizer will generate an execution plan. Since both queries are asking for the same logical result, it is reasonable to expect the same efficient plan will be generated for both. The only ways to be certain are to

    • inspect the execution plans
    • or measure the IO (SET STATISTICS IO ON).

    Is there a performance difference

    If you find yourself in a scenario where you have to ask, you should cultivate tools with which to measure and discover the truth for yourself. Measure - not ask.

提交回复
热议问题