简体   繁体   English

SQL Server EF核心行删除性能

[英]SQL Server EF Core Row Deletion Performance

I've got a simple 3-column table to track pending calculations. 我有一个简单的3列表来跟踪未完成的计算。 The first column is an integer Foreign Key linking to a clients table. 第一列是链接到客户表的整数外键。 The second is a date and the third is a priority flag. 第二个是日期,第三个是优先级标志。 The primary key for the table is a composite of the client FK and the date. 该表的主键是客户端FK和日期的组合。

This table is used for a calculation queue, so it sees a lot of churn. 该表用于计算队列,因此会产生很多流失。 What I noticed, however, is that working with this table itself is sometimes slower than the actual calculations that it's a queue for. 但是,我注意到的是,使用此表本身有时要比排队的实际计算要慢。 Specifically, when it has about 1000 rows, and the calculation is short-circuited, removing just one row from the queue and calling SaveChanges takes on average three seconds. 具体来说,当它有大约1000行并且计算被短路时,从队列中仅删除一行并调用SaveChanges平均需要三秒钟。

With this table constantly being added to and removed from, those three seconds add up quick. 随着该表的不断添加和删除,这三秒钟很快就累了。 And it has to be done serially to ensure the queue is tackled in the correct order. 而且必须串行执行以确保按正确的顺序处理队列。 It just seems like an inordinately long amount of time to remove a single row on a three-column table. 删除三列表上的单个行似乎耗时极长。

I'm guessing the composite key is to blame...? 我猜这应该归咎于复合钥匙...? But I'm not sure, and I'm new to MS SQL and EF Core. 但是我不确定,我还是MS SQL和EF Core的新手。 Can anyone point me in the right direction to profiling this to identify the bottleneck? 谁能指出我正确的方向来进行分析以找出瓶颈?

I would start by profiling the app through Visual Studio ( Debug > Performance Profiler ) to see where the problem actually is: 我将首先通过Visual Studio( Debug > Performance Profiler )对应用程序进行性能分析,以查看问题的实际出处:

  1. If you find out that the call to SQL is actually slow, I would continue with checking what commands are actually sent to the SQL server and which of them takes so long. 如果您发现对SQL的调用实际上很慢,那么我将继续检查哪些命令实际发送到了SQL Server,其中哪些命令花费了很长时间。 Use either ExpressProfiler , SQL Server Profiler extension for Azure Data Studio , or SQL Server Management Studio. 使用ExpressProfiler ,用于Azure Data Studio的 SQL Server Profiler扩展或SQL Server Management Studio。 This can also lead to poor SQL performance because of neglected maintenance such as fragmented indexes, etc. 由于忽略了维护(如碎片索引等),这也可能导致SQL性能下降。
  2. You might also find out that the problem is actually somewhere within EF logic, for example, change tracker trying to figure out what entities to update. 您可能还会发现问题实际上出在EF逻辑中,例如,变更跟踪器试图找出要更新的实体。 In that case, you will need to find a way to avoid the particular issue (eg, do not save after each delete, use SQL command to delete rows manually, use Dapper instead of EF, etc.). 在这种情况下,您将需要找到一种避免特定问题的方法(例如,在每次删除后不保存,使用SQL命令手动删除行,使用Dapper而不是EF等)。

I hope these ideas help you to fix the problem. 我希望这些想法可以帮助您解决问题。 Good luck ;-) 祝好运 ;-)

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

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