简体   繁体   English

LINQ-2-SQL编译查询。 领域还是财产?

[英]LINQ-2-SQL Compiled Queries. Field vs Property?

I've recently turned a LINQ-2-SQL function into a Compiled query, and I noticed some really strange behavior with it. 我最近将LINQ-2-SQL函数转换为已编译的查询,并且注意到它的一些非常奇怪的行为。 When I store it as a static field, as per every example that I've seen here and on the rest of the web, load testing pegs the CPU at 100% for the entire duration of the test. 当我将其存储为静态字段时(根据我在此处和在Web其余部分上看到的每个示例),负载测试会将CPU在整个测试过程中固定为100%。 When I do it as a static property, on the other hand, the exact same load testing script fluctuates in the 40-100% range. 另一方面,当我将其作为静态属性执行时,完全相同的负载测试脚本会在40-100%的范围内波动。 Why is performance so much better as a property? 为什么性能要比性能好得多? Anyone have an idea? 有人有主意吗? As far as I can tell, the only difference between them is that the field takes more CPU than the property. 据我所知,它们之间的唯一区别是该字段占用的CPU比该属性占用的CPU多。

Here's the two versions: 这是两个版本:

    private static Func<WebDataContext, int, int, IEnumerable<Product>> GetProductsWithDecendentQuery
    {
        get
        {
            return CompiledQuery.Compile<WebDataContext, int, int, IEnumerable<Product>>((WebDataContext context, int left, int right) => 
                context.Taxonomies.Where(x => x.LeftNumber >= left && x.RightNumber <= right)
                .SelectMany(x => x.InventoryTaxonomies.Select(y => y.Product))
                .Distinct().Where(x => x.Status && (x.IsDropShip != true || x.Locations.Sum(y => y.Quantity) > 1))
            );
        }
    }

vs

    private static Func<WebDataContext, int, int, IEnumerable<Product>> GetProductsWithDecendentQuery =
        CompiledQuery.Compile<WebDataContext, int, int, IEnumerable<Product>>((WebDataContext context, int left, int right) => 
                context.Taxonomies.Where(x => x.LeftNumber >= left && x.RightNumber <= right)
                .SelectMany(x => x.InventoryTaxonomies.Select(y => y.Product))
                .Distinct().Where(x => x.Status && (x.IsDropShip != true || x.Locations.Sum(y => y.Quantity) > 1))
            );

I suspect that there is some other IO that is being performed that is not CPU dependent on the property version than on the field version. 我怀疑还有其他正在执行的IO,这些IO不取决于属性版本,而取决于字段版本。 Other than the CPU utilization, do you have a comparison of the timing differences between the options? 除了CPU利用率以外,您是否还可以比较两个选项之间的时序差异?

When LINQ to SQL was in Beta and CompiledQuery was first introduced at least, the static fields were recommended in part because the compiler could detect them and optimize parts of the query at compile time rather than waiting until runtime as would be required in the case of a property. 当LINQ to SQL在Beta中并且至少首次引入CompiledQuery时,建议使用静态字段,部分原因是编译器可以在编译时检测到它们并优化查询的某些部分,而不必等到运行时才需要财产。 For example, there were claims that the compiler could detect the context's metadata and hard code the property setters rather than relying on a reflection approach when hydrating the objects for a static field compiled query. 例如,有人声称编译器可以检测上下文的元数据并对属性设置器进行硬编码,而不是在为静态字段编译的查询补水对象时依赖于反射方法。 Rico Mariani discussed some of these optimizations at http://blogs.msdn.com/b/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx . Rico Mariani在http://blogs.msdn.com/b/ricom/archive/2007/06/22/dlinq-linq-to-sql-performance-part-1.aspx上讨论了其中的一些优化。 I can't say if/how these optimizations have changed in subsequent releases at this point to say for sure if that could cause the differences you are seeing. 我不能说这些优化是否/如何在随后的发行版中更改,以便确定是否会导致您所看到的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM