我是一家公司的 SQL Server DBA,该公司销售 ASP.NET MVC3 应用程序,该应用程序使用 LINQ 和 Entity Framework 4 进行所有数据库访问。当我在 SQL Server 的计划缓存中发现由 LINQ 生成的低效查询时,我希望能够在源代码中找到该 LINQ 语句,以便我可以对其进行优化。查找生成给定 SQL 语句的 LINQ 的最佳方法是什么?
例如,是否有任何方法可以在配置文件中添加条目或以某种方式修饰代码,以便类和方法名称或 LINQ 语句本身作为注释包含在生成的 SQL 中?
I am a SQL Server DBA for a company that sells an ASP.NET MVC3 application that uses LINQ and Entity Framework 4 for all database access. When I find an inefficient query in my SQL Server's plan cache that was generated by LINQ, I would like to be able to find that LINQ statement in the source code so that I can optimize it. What is the best way to find the LINQ that generated a given SQL statement?
For example, is there any way to put an entry in a config file or decorate the code somehow so that the class and method name or the LINQ statement itself are included as comments in the generated SQL?
发布评论
评论(5)
商业工具ORM Profiler、 Entity Framework Profiler 或 Hugati Query Profiler 都会为您提供以下方法的堆栈跟踪生成 SQL。这使得在代码中查找 LINQ 变得相当容易,尽管它不会直接显示。
这些工具的另一个优点是可以轻松地在应用程序执行的许多其他 SQL 语句中找到低效的查询。
The commercial tools ORM Profiler, Entity Framework Profiler or Hugati Query Profiler will both give you a stack trace for the methods which generated the SQL. That makes it fairly easy to find the LINQ in code, though it isn't displayed directly.
These tools also have the advantage that they make it easy to find inefficient queries amongst the many other SQL statements executed by the app.
虽然它不是免费工具,但这可能会提供您需要的信息:
http://efprof.com/
有这里描述的也是一个较便宜的工具,我没有使用过,但它看起来很有前途:
http://huagati.blogspot.com/2010/06/entity-framework-support-in-huagati.html
http://www.huagati.com/L2SProfiler/
Although it is not a free tool, this may provide the information you need:
http://efprof.com/
There is also a less expensive tool described here, which I have not used, but it looks very promising:
http://huagati.blogspot.com/2010/06/entity-framework-support-in-huagati.html
http://www.huagati.com/L2SProfiler/
我敢打赌 Entity Framework Profiler (http://efprof.com/) 会帮助你。工作流程与您要求的非常不同(顺便说一句,这会非常酷)。这是一个很好的工具,即使它不是您的最终解决方案,也值得一看。
祝你好运!
I bet Entity Framework Profiler (http://efprof.com/) would help you out. The workflow is very different from what you asked for (which would be pretty cool BTW). It is a good tool, and is worth a look even if it's not your final solution.
Good luck!
如果您有权访问 LINQ 代码所在的 ASP.NET 代码,您或多或少可以知道您正在查找哪个查询,将其复制到名为 LINQPad 的免费软件工具中并直接在那里运行以获取生成的 SQL 语句。 http://www.linqpad.net/
您需要首先在 .net 代码上获取 LINQ 查询,创建连接到数据源,将 Linq 代码粘贴到新查询中并运行它们。您将获得从 LINQ 代码生成的 SQL 查询。
例如:
SQL 结果选项卡:
这可能不完全是您正在寻找的内容,但值得了解此工具,因为它对于快速测试 LINQ 查询非常有帮助。在那里,您可以快速编辑和运行以改进代码,而无需重新编译整个内容。
If you have access to the ASP.NET code where the LINQ code is you can more or less know which query you are looking for, copy it into a freeware tool called LINQPad and run it directly there to get the generated SQL statements. http://www.linqpad.net/
You need first get the LINQ queries on your .net code, create a connection to your datasource, paste the Linq code in new queries and run them. You will get the SQL Query generated from the LINQ code.
For example:
SQL Results Tab:
This is probably not exactly what you are looking for, but it is worth knowing about this tool since it is very helpful to quickly test LINQ queries. There you can quickly edit and run to improve the code without recompiling the whole stuff.
我认为您不能轻松修改生成的 SQL,但您可以做的是在将查询发送到数据库之前获取生成的 SQL。
因此,您可以将每个查询记录在单独的文本文件中,并包含时间戳和源代码上下文信息。但这意味着修改源中将 LINQ 查询发送到数据库的每个位置。也许 DataContext 类中的某个地方有一个扩展点来简化这一点。
无论如何,这里是获取 LINQ 查询相应的 sql 查询的代码:
I don't think you can modify the generated SQL easily but what you can do is to get the generated SQL before sending the query to the database.
So you can log every query in a separate textfile with timestamp and source code context information. But that means to modify each place in your source where LINQ queries are sent to the database. Maybe there is an extension point somewhere in the DataContext class for simplifying this.
Anyway here is the code to get the corresponding sql query for a LINQ query: