LINQ-to-SQL CompiledQuery.Compile() 与更新、删除、插入? [英] LINQ-to-SQL CompiledQuery.Compile() with Update, Delete, Insert?

查看:18
本文介绍了LINQ-to-SQL CompiledQuery.Compile() 与更新、删除、插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

所以我已经将 LINQ-to-SQL 中的所有选择查询转换为使用 CompiledQueries 来加快速度.到目前为止,对于 select 语句效果很好,但我一直无法弄清楚如何预编译插入、更新或删除语句.

So I've got all my select queries in LINQ-to-SQL converted to using CompiledQueries to speed things up. Works great so far for select statements, but I haven't been able to figure out how to pre-compile insert, update, or delete statements.

当然,当您在 LINQ-to-SQL 中插入、删除或更新时,您必须使用对象模型.但很明显,它在生成查询的过程中的某个地方,预编译并存储在静态成员中会很好.

Granted, when you're inserting, deleting or updating in LINQ-to-SQL, you have to use the object model. But obviously somewhere along the way it generates a query, which would be nice to pre-compile and store in a static member.

这可能吗?LINQ 在未预编译时的更新、删除和插入性能如何?我可以看到它比选择要快得多,因为它们在下面所做的要简单得多,而且动态"更少......

Is this possible? What is LINQ performance like for updates, deletes and inserts when its not pre-compiled? I could see it being a lot faster than the selects, because what they do underneath is a lot simpler and less "dynamic"...

推荐答案

有很大的不同.Linq-To-SQL 选择查询可以是大型复杂表达式树.它的这些可能需要一些时间编译".在这种情况下,合并到一些可以针对 SQL Server 运行的 T-SQL.所以缓存一个操作的结果以便它可以被重用是有意义的.

There is a big difference. Linq-To-SQL select queries can be large complex expressions trees. Its these that may take sometime 'compiling'. In this case coalescing to some T-SQL that can be run against a SQL Server. So it makes sense to cache the result of an operation so that it can be re-used.

然而,其他删除、更新和插入是简单的操作,不需要将表达式树转换为 T-SQL(LINQ 本身就是关于查询的).不幸的是,我们被训练将执行这些其他操作的 SQL 代码视为查询",我们实际上并没有要求任何信息.

However other Delete, Update, and Insert are simple operations that do not require an expression tree to be converted to T-SQL (LINQ itself is all about querying). Its just unfortunate that we've been trained to think of SQL code that performs these other operations as 'queries', we're not actuall asking for any info.

这些操作仅由 DataContext 定义,而不由 LINQ 定义,因此已经编译了执行这些功能的代码.

These operations are only defined by the DataContext not by LINQ hence the code to perform these functions is already compiled.

这篇关于LINQ-to-SQL CompiledQuery.Compile() 与更新、删除、插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆