• <small id='Epg8w'></small><noframes id='Epg8w'>

    <i id='Epg8w'><tr id='Epg8w'><dt id='Epg8w'><q id='Epg8w'><span id='Epg8w'><b id='Epg8w'><form id='Epg8w'><ins id='Epg8w'></ins><ul id='Epg8w'></ul><sub id='Epg8w'></sub></form><legend id='Epg8w'></legend><bdo id='Epg8w'><pre id='Epg8w'><center id='Epg8w'></center></pre></bdo></b><th id='Epg8w'></th></span></q></dt></tr></i><div id='Epg8w'><tfoot id='Epg8w'></tfoot><dl id='Epg8w'><fieldset id='Epg8w'></fieldset></dl></div>

      <tfoot id='Epg8w'></tfoot>
    1. <legend id='Epg8w'><style id='Epg8w'><dir id='Epg8w'><q id='Epg8w'></q></dir></style></legend>

        • <bdo id='Epg8w'></bdo><ul id='Epg8w'></ul>

        使用 Dapper 删除超过 2100 行(按 ID)的正确方法

        时间:2024-04-15

            • <bdo id='Opa37'></bdo><ul id='Opa37'></ul>

              <small id='Opa37'></small><noframes id='Opa37'>

              <i id='Opa37'><tr id='Opa37'><dt id='Opa37'><q id='Opa37'><span id='Opa37'><b id='Opa37'><form id='Opa37'><ins id='Opa37'></ins><ul id='Opa37'></ul><sub id='Opa37'></sub></form><legend id='Opa37'></legend><bdo id='Opa37'><pre id='Opa37'><center id='Opa37'></center></pre></bdo></b><th id='Opa37'></th></span></q></dt></tr></i><div id='Opa37'><tfoot id='Opa37'></tfoot><dl id='Opa37'><fieldset id='Opa37'></fieldset></dl></div>
                <tbody id='Opa37'></tbody>

              1. <tfoot id='Opa37'></tfoot>

                1. <legend id='Opa37'><style id='Opa37'><dir id='Opa37'><q id='Opa37'></q></dir></style></legend>
                  本文介绍了使用 Dapper 删除超过 2100 行(按 ID)的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Dapper 支持我的服务器数据访问应用程序.

                  I am trying to use Dapper support my data access for my server app.

                  我的服务器应用程序有另一个应用程序,它以每分钟 400 条的速度将记录放入我的数据库中.

                  My server app has another application that drops records into my database at a rate of 400 per minute.

                  我的应用将它们分批拉出来,处理它们,然后从数据库中删除它们.

                  My app pulls them out in batches, processes them, and then deletes them from the database.

                  由于在我处理过程中数据会继续流入数据库,因此我没有什么好方法可以说delete from myTable where allProcessed = true.

                  Since data continues to flow into the database while I am processing, I don't have a good way to say delete from myTable where allProcessed = true.

                  但是,我知道要删除的行的 PK 值.所以我想做一个 delete from myTable where Id in @listToDelete

                  However, I do know the PK value of the rows to delete. So I want to do a delete from myTable where Id in @listToDelete

                  问题是,如果我的服务器宕机甚至 6 分钟,那么我有超过 2100 行要删除.

                  Problem is that if my server goes down for even 6 mintues, then I have over 2100 rows to delete.

                  由于 Dapper 接受了我的 @listToDelete 并将每一个都变成了一个参数,所以我对 delete 的调用失败了.(导致我的数据清除进一步落后.)

                  Since Dapper takes my @listToDelete and turns each one into a parameter, my call to delete fails. (Causing my data purging to get even further behind.)

                  在 Dapper 中处理此问题的最佳方法是什么?

                  What is the best way to deal with this in Dapper?

                  注意:我已经查看了表值参数,但从我所见,它们不是很性能.我的这一架构是我系统的瓶颈,我需要非常非常快速.

                  NOTES: I have looked at Tabled Valued Parameters but from what I can see, they are not very performant. This piece of my architecture is the bottle neck of my system and I need to be very very fast.

                  推荐答案

                  一种选择是在服务器上创建一个临时表,然后使用批量加载工具一次性将所有 ID 上传到该表中.然后使用连接、EXISTS 或 IN 子句仅删除您上传到临时表中的记录.

                  One option is to create a temp table on the server and then use the bulk load facility to upload all the IDs into that table at once. Then use a join, EXISTS or IN clause to delete only the records that you uploaded into your temp table.

                  批量加载是 SQL Server 中优化良好的路径,速度应该非常快.

                  Bulk loads are a well-optimized path in SQL Server and it should be very fast.

                  例如:

                  1. 执行语句CREATE TABLE #RowsToDelete(ID INT PRIMARY KEY)
                  2. 使用批量加载将键插入#RowsToDelete
                  3. 执行 DELETE FROM myTable where Id IN (SELECT ID FROM #RowsToDelete)
                  4. 执行DROP TABLE #RowsToDelte(如果关闭会话,表也会自动删除)
                  1. Execute the statement CREATE TABLE #RowsToDelete(ID INT PRIMARY KEY)
                  2. Use a bulk load to insert keys into #RowsToDelete
                  3. Execute DELETE FROM myTable where Id IN (SELECT ID FROM #RowsToDelete)
                  4. Execute DROP TABLE #RowsToDelte (the table will also be automatically dropped if you close the session)

                  (假设 Dapper)代码示例:

                  (Assuming Dapper) code example:

                  conn.Open();
                  
                  var columnName = "ID";
                  
                  conn.Execute(string.Format("CREATE TABLE #{0}s({0} INT PRIMARY KEY)", columnName));
                  
                  using (var bulkCopy = new SqlBulkCopy(conn))
                  {
                      bulkCopy.BatchSize = ids.Count;
                      bulkCopy.DestinationTableName = string.Format("#{0}s", columnName);
                  
                      var table = new DataTable();                    
                      table.Columns.Add(columnName, typeof (int));
                      bulkCopy.ColumnMappings.Add(columnName, columnName);
                  
                      foreach (var id in ids)
                      {
                          table.Rows.Add(id);
                      }
                  
                      bulkCopy.WriteToServer(table);
                  }
                  
                  //or do other things with your table instead of deleting here
                  conn.Execute(string.Format(@"DELETE FROM myTable where Id IN 
                                                     (SELECT {0} FROM #{0}s", columnName));
                  
                  conn.Execute(string.Format("DROP TABLE #{0}s", columnName));
                  

                  这篇关于使用 Dapper 删除超过 2100 行(按 ID)的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将参数传递给使用 sqlcmd 调用的 SQL Server 脚本? 下一篇:SQL Server - 参数名称中的无效字符

                  相关文章

                2. <legend id='UswOM'><style id='UswOM'><dir id='UswOM'><q id='UswOM'></q></dir></style></legend>
                    <bdo id='UswOM'></bdo><ul id='UswOM'></ul>
                  <tfoot id='UswOM'></tfoot>

                3. <i id='UswOM'><tr id='UswOM'><dt id='UswOM'><q id='UswOM'><span id='UswOM'><b id='UswOM'><form id='UswOM'><ins id='UswOM'></ins><ul id='UswOM'></ul><sub id='UswOM'></sub></form><legend id='UswOM'></legend><bdo id='UswOM'><pre id='UswOM'><center id='UswOM'></center></pre></bdo></b><th id='UswOM'></th></span></q></dt></tr></i><div id='UswOM'><tfoot id='UswOM'></tfoot><dl id='UswOM'><fieldset id='UswOM'></fieldset></dl></div>

                    <small id='UswOM'></small><noframes id='UswOM'>