简体   繁体   English

Linq to SQL to Linq编译性能

[英]Linq to SQL to Linq compiled performance

string id = (from c in context.Users
             where c.u_id == Id
             select c.u_id).SingleOrDefault();

1)How i can improve the performance for the above using Linq complied query for the above. 1)如何使用上面的Linq符合条件的查询来提高上面的性能。 We are limited to use .NET 3.5 only. 我们仅限使用.NET 3.5。

2)What will be performance gain using a complied linq query for the above code in terms of percentage? 2)对上面的代码使用符合条件的linq查询可以提高性能百分比?

You would create the compiled query via: 您可以通过以下方式创建编译后的查询:

Func<YourContextType, int, string> query = CompiledQuery.Compile(
       (YourContextType context, int id) => 
           context.Users.Where(u => u.u_id == id).Select(u => u.u_id)
                  .SingleOrDefault()
       );

You would then use this as: 然后,您可以将其用作:

string resultId = query(context, Id);

As for the performance gain, this may be significant, but it may also be minimal. 至于性能提升,这可能很重要,但也可能很小。 It really depends on how quick the query is being performed, and how often you can reuse the compiled query. 这实际上取决于执行查询的速度以及您可以多久重复使用一次已编译的查询。 In many cases, using the cmopiled query is actually slower, as the overhead of compilation doesn't make up for the speed gains. 在许多情况下,使用cmopiled查询实际上会比较慢,因为编译的开销无法弥补速度的提高。 You would need to measure to determine if this is worth the effort. 您需要进行度量以确定这是否值得付出努力。

Note that, if you know you only have one unique ID, you can also potentially speed up your original query merely by using FirstOrDefault() instead of SingleOrDefault() . 需要注意的是,如果你知道你只有一个唯一的ID,你也可以只可能通过加快您的原始查询FirstOrDefault()代替SingleOrDefault()

Declare your compiled query like this: 像这样声明您的编译查询:

static readonly Func<ENTITIES, YOUR_ID_TYPE, RETURN_VALUE_TYPE> compiledQuery  =
CompiledQuery.Compile<ENTITIES, YOUR_ID_TYPE, RETURN_VALUE_TYPE>(
(ctx, Id) => (from c in ctx.Users
where c.u_id == Id
select c.u_id).SingleOrDefault();

Then in your code call your compiled query: 然后在代码中调用已编译的查询:

RETURN_VALUE_TYPE results = compiledQuery.Invoke(context, Id);

And regarding the performance improvement that can depend on several things, however keep in mind that in the scope of LINQ query execution, query compilation is an expensive part of the process. 关于可能取决于几件事的性能改进,但是请记住,在LINQ查询执行范围内,查询编译是该过程中昂贵的一部分。 Any time you're adding LINQ query logic to your LINQ to SQL- or Entity Framework-based applications, you should consider precompiling the queries and reusing them. 每当将LINQ查询逻辑添加到基于SQL或Entity Framework的LINQ应用程序中时,都应考虑预编译查询并重新使用它们。

A cheeky option would be: don't involve LINQ for such a trivial but performance-critical operation. 一个厚脸皮的选择是:不要让LINQ参与这种琐碎但对性能至关重要的操作。 For example, with dapper-dot-net: 例如,使用dapper-dot-net:

string id = connection.Query<string>(
    @"select u_id from Users where u_id = @id",
    new { id = Id }).SingleOrDefault();

which completely avoids all the LINQ abstractions, leaning directly on the database (fully parameterless etc). 这完全避免了所有LINQ抽象,而直接依赖于数据库(完全无参数等)。

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

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