在 SQL Profiler 中查找特定 LINQ 查询的巧妙技巧

发布于 2024-07-09 12:47:56 字数 700 浏览 4 评论 0原文

由于有时会创建疯狂的 SQL,因此分析 LINQ 查询及其执行计划尤为重要。

我经常发现我需要跟踪特定的查询,但很难在查询分析器中找到。 我经常在有大量正在运行的事务的数据库(有时是生产服务器)上执行此操作 - 因此仅打开 Profiler 是没有好处的。

我还发现尝试使用 DataContext 进行跟踪是不够的,因为它没有提供我实际上可以自己执行的 SQL。

到目前为止,我最好的策略是在查询中添加一个“随机”数字,并在跟踪中过滤它。

LINQ:

where o.CompletedOrderID != "59872547981"

探查器过滤器:

'TextData' like '%59872547981'

这可以很好地工作,但有一些注意事项:

  • 我必须小心记住删除条件,或者选择不会对查询计划影响太大的东西。 是的,我知道把它留在里面是自找麻烦。
  • 据我所知,即使使用这种方法,我也需要为每个需要跟踪的 LINQ 查询启动一个新的跟踪。 如果我转到“文件>” 现有跟踪的“属性”我无法更改过滤条件。

您无法在应用程序中运行查询并看到它在分析器中弹出而无需任何额外的努力。 只是希望其他人能想出比这更好的方法,或者至少建议搜索一个比列查询更“危险”的标记。

Profiling LINQ queries and their execution plans is especially important due to the crazy SQL that can sometimes be created.

I often find that I need to track a specific query and have a hard time finding in query analyzer. I often do this on a database which has a lot of running transactions (sometimes production server) - so just opening Profiler is no good.

I've also found tryin to use the DataContext to trace inadequate, since it doesnt give me SQL I can actually execute myself.

My best strategy so far is to add in a 'random' number to my query, and filter for it in the trace.

LINQ:

where o.CompletedOrderID != "59872547981"

Profiler filter:

'TextData' like '%59872547981'

This works fine with a couple caveats :

  • I have to be careful to remember to remove the criteria, or pick something that wont affect the query plan too much. Yes I know leaving it in is asking for trouble.
  • As far as I can tell though, even with this approach I need to start a new trace for every LINQ query I need to track. If I go to 'File > Properties' for an existing trace I cannot change the filter criteria.

You cant beat running a query in your app and seeing it pop up in the Profiler without any extra effort. Was just hoping someone else had come up with a better way than this, or at least suggest a less 'dangerous' token to search for than a query on a column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难遇 2024-07-16 12:47:56

弄乱 where 子句可能不是最好的做法,因为它可能并且将会影响查询的执行计划。

相反,通过投影到匿名类中做一些有趣的事情 - 使用唯一的静态列名称或不会影响执行计划的名称。 (这样您就可以在生产代码中将其完整保留,以防以后需要对生产代码进行任何分析......)

from someobject in dc.SomeTable
where someobject.xyz = 123
select new { MyObject = someobject, QueryTraceID1234132412='boo' }

Messing with the where clause is maybe not the best thing to do since it can and will affect the execution plans for your queries.

Do something funky with projection into anonymous classes instead - use a unique static column name or something that will not affect the execution plan. (That way you can leave it intact in production code in case you later need to do any profiling of production code...)

from someobject in dc.SomeTable
where someobject.xyz = 123
select new { MyObject = someobject, QueryTraceID1234132412='boo' }
╰◇生如夏花灿烂 2024-07-16 12:47:56

您可以使用 Linq to SQL 调试可视化工具 - http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx 并在监视窗口中查看它。

或者您可以使用 DataContext.GetCommand(); 在 SQL 执行之前查看它。

您还可以查看 DataContext.GetChangeSet() 来查看将要插入/更新或删除的内容。

You can use the Linq to SQL Debug Visualiser - http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx and see it in your watch window.

Or you can use DataContext.GetCommand(); to see the SQL before it executes.

You can also look at the DataContext.GetChangeSet() to view what's going to be inserted/ updated or deleted.

紫南 2024-07-16 12:47:56

EFCore 有一个功能 TagWith() 正是用于此目的。

 var nearestFriends =
      (from f in context.Friends.TagWith("This is my spatial query!")
      orderby f.Location.Distance(myLocation) descending
      select f).Take(5).ToList();

https://learn.microsoft.com/en-us/ef/ core/querying/tags

不幸的是,您不能使用 查询存储来查找它们:-)

这是因为查询之前的注释是删除

太可惜了! 希望我不用再等12年。

EFCore has a feature TagWith() exactly for this purpose.

 var nearestFriends =
      (from f in context.Friends.TagWith("This is my spatial query!")
      orderby f.Location.Distance(myLocation) descending
      select f).Take(5).ToList();

https://learn.microsoft.com/en-us/ef/core/querying/tags

Unfortunately you can't use Query Store to find them :-)

This is because comments before the query are stripped out.

Such a shame! Hope I don't have to wait another 12 years.

喜你已久 2024-07-16 12:47:56

您可以让数据上下文注销原始 SQL,然后您可以在探查器中搜索该 SQL 以检查性能。

using System.Diagnostics.Debugger;

yourDataContext.Log = new DebuggerWriter();

现在,所有 SQL 查询都将显示在调试器输出窗口中。

You can have your datacontext log out the raw SQL, which you could then search for in the profiler to examine performance.

using System.Diagnostics.Debugger;

yourDataContext.Log = new DebuggerWriter();

All of your SQL queries will be displayed in the debugger output window now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文