【问题标题】:What is the fastest way to update SQL rows with values, based upon a value in the row?根据行中的值,用值更新 SQL 行的最快方法是什么?
【发布时间】:2018-08-26 22:07:11
【问题描述】:

我在SQL Server 中有以下table,称为tblProducts

+-----------+--------------+-------------+
| pkProduct | fkProductID  |  intIssue   |
+-----------+--------------+-------------+
|     1     |     10       |      1      |
|     2     |     10       |      2      |
|     3     |     10       |      4      |
|     4     |     11       |      1      |
|     5     |     11       |      2      |
|     6     |     11       |      3      |
|     7     |     11       |      5      |
|     8     |     12       |      1      |
|     9     |     13       |      1      |
|     10    |     13       |      4      |
|     11    |     14       |      1      |
|     12    |     14       |      3      |
|     13    |     14       |      6      |
|     14    |     15       |      1      |
|     15    |     16       |      1      |
+-----------+--------------+-------------+

随着时间的推移,无论出于何种原因,行已被删除,问题编号现在存在差距。我希望问题编号按顺序运行,以便 table 看起来像这样:

+-----------+--------------+-------------+
| pkProduct | fkProductID  |  intIssue   |
+-----------+--------------+-------------+
|     1     |     10       |      1      |
|     2     |     10       |      2      |
|     3     |     10       |      3      |
|     4     |     11       |      1      |
|     5     |     11       |      2      |
|     6     |     11       |      3      |
|     7     |     11       |      4      |
|     8     |     12       |      1      |
|     9     |     13       |      1      |
|     10    |     13       |      2      |
|     11    |     14       |      1      |
|     12    |     14       |      2      |
|     13    |     14       |      3      |
|     14    |     15       |      1      |
|     15    |     16       |      1      |
+-----------+--------------+-------------+

目前我已将table 添加到我的DataSet 并在vb.net 代码中使用它来更新行:

Dim currentProductsTable As tblProductsDataTable

Using taProduct As New tblProductsTableAdapter
    currentProductsTable = taProduct.GetData
End Using

Dim issueCounter As Integer = 1
Dim previousRow As tblProductsRow = Nothing

Using con As SqlConnection = New SqlConnection(My.Settings.MyConnectionString)
    Using cmd As New SqlCommand("UPDATE dbo.tblProducts SET [intIssue] = @issueCounter WHERE pkProduct = @ProductID", con)
        cmd.Connection = con 
        con.Open()
        For Each row As tblProductsRow In currentProductsTable.Rows
            If Not previousRow Is Nothing Then
                If row.fkProductID = previousRow.fkProductID Then
                    cmd.Parameters.Clear()
                    cmd.Parameters.Add("@issueCounter", SqlDbType.Int).Value = issueCounter
                    cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = row.pkProduct
                    previousRow = row
                Else
                    issueCounter = 1
                    cmd.Parameters.Clear()
                    cmd.Parameters.Add("@issueCounter", SqlDbType.Int).Value = issueCounter
                    cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = row.pkProduct
                    previousRow = row
                End If
            Else
                issueCounter = 1
                cmd.Parameters.Clear()
                cmd.Parameters.Add("@issueCounter", SqlDbType.Int).Value = issueCounter
                cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = row.pkProduct
                previousRow = row
            End If
            cmd.ExecuteNonQuery()
            issueCounter += 1
        Next
    End Using
End Using

我已通过fkProductID ASCintIssue ASC 订购了.GetData, 但是这需要很长时间,因为table 中有大约 30000 个rows

我想知道是否有更快的方法来进行这种更新?

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。
  • 已标记,真是个沃利!对不起。

标签: sql sql-server vb.net foreach sql-update


【解决方案1】:

我猜你正在使用 SQL Server。如果是这样,您可以在一个查询中完成所有操作:

with toupdate as (
      select p.*,
             row_number() over (partition by fkProductID order by pkProduct) as new_intIssue
      from dbo.tblProducts p
     )
update toupdate
    set intIssue = new_intIssue
    where intIssue <> new_intIssue;

【讨论】:

  • 谢谢,它似乎在快速扫描行时工作得很好。我真的需要学习更多的 SQL。查询不到一秒就完成了!与在循环中进行的 10 分钟以上相比。你回答的速度也很惊人,但我想如果你知道那就很容易了!不过我还是很惊讶。
  • @Pete 。 . .我在了解人们如何使用数据库以及需要做的事情方面有一些经验。
猜你喜欢
  • 2018-04-03
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多