0

我在我的机器上以(本地)身份运行本地 SQL Sever 实例。我使用的是普通的台式办公电脑(i5-2400 @ 3.1 ghz 2.85 gb ram)。sqlserver.exe 在我的任务管理器中使用了 1.5GB 的内存。我已经开始使用我机器上的 sql server 实例从数据集中上传表格,然后在 sql server 中查询数据以转储到 excel 中。这样做我已经在我的 sql server 表中建立了 8 个表。表 opcosourcingweekly 有大约 400k 行和 26 列......

我认为我的问题可能与 SQL Server 中的索引或终止进程的设置有关。我看不出我的代码在本地 SQL Server 实例上执行简单的删除语句时会如何挂起。

由于某种原因,当我在下面使用 ExecuteNonQuery 时,我的代码挂起......我已将其注释掉并尝试运行程序并在我的本地 Sql Server 实例中进行更新。 要删除的简单查询在 SQL Management Studeo Express 中运行需要 2-3 分钟...知道为什么我的 sql server 实例的性能如此糟糕吗?

之前,当我在 button_click 参数中有这些东西时,它并没有花这么长时间......我在我的程序中添加了 datamonthlydump 选项并需要创建私人潜艇......

知道如何使 SQL Server 在本地计算机上运行时更快吗?我已经编写了一些程序,这是我的系统第一次挂起。 当我在 Visual Basic 中编程时,有时我会在中途停止程序并保持连接打开 我不知道这是否在我的 sql 服务器上打开了连接,即使在重新启动或如何清除我的 sql 服务器/重新启动它之后也是如此想法如何提高下面代码的性能?谢谢!

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSQLImport.Click

    If btnMthly.Checked = True Then
        datadumpmthly()
    ElseIf btnWkly.Checked = True Then
        datadumpwkly()
    End If

End Sub

    Private Sub datadumpwkly()
    Dim connectionstring1 As String = "Data Source=(local); Initial Catalog=master;Pooling=False;Integrated Security=True; "
    Dim connection As SqlConnection = New SqlConnection(connectionstring1)
    connection.Open()
    Dim queryString As String = "delete from dbo.OpCoSourcingweekly"
    Dim dbCommand As New SqlCommand(queryString, connection)
    'dbCommand.CommandText = queryString
    'dbCommand.Connection = connection
    'dbCommand.CommandTimeout = 100000
    'dbCommand.ExecuteNonQuery()

    Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(connectionstring1)
        bulkcopy.DestinationTableName = "OpCoSourcingweekly"
        bulkcopy.BulkCopyTimeout = 10000000

        Try
            bulkcopy.WriteToServer(dataset1.Tables(0))
        Catch ex As Exception
            Dim error1 As String
            error1 = ex.Message
            MsgBox("ERROR: " & error1)
        End Try
        bulkcopy.Close()

    End Using
    'dbCommand.CommandTimeout = 100000
    'queryString = "delete from dbo.OpCoSourcingweekly where [Fiscal Year Week] = '201233'"

    'Dim dbCommand1 As New SqlCommand(queryString, connection)
    'dbCommand1.CommandText = queryString
    'dbCommand1.Connection = connection
    'dbCommand.CommandTimeout = 100000
    'dbCommand.ExecuteNonQuery()

    'queryString = "delete from dbo.OpCoSourcingweekly where [Fiscal Year Week] = '201234'"
    'Dim dbCommand2 As New SqlCommand(queryString, connection)
    'dbCommand2.CommandText = queryString
    'dbCommand2.Connection = connection
    'dbCommand.CommandTimeout = 100000
    'dbCommand.ExecuteNonQuery()
    MsgBox("Done with import to SQL")
    connection.Close()
End Sub

通过以下操作解决感谢您的评论

Private Sub datadumpwkly()
    Dim connectionstring1 As String = "Data Source=(local); Initial Catalog=master; Pooling=False;Integrated Security=True; "
    Using connection As SqlConnection = New SqlConnection(connectionstring1)
        connection.Open()
        Dim queryString As String = "truncate table dbo.OpCoSourcingweekly"
        Using dbCommand As New SqlCommand(queryString, connection)
            dbCommand.CommandText = queryString
            dbCommand.Connection = connection
            dbCommand.CommandTimeout = 100000
            dbCommand.ExecuteNonQuery()

            Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(connectionstring1)
                bulkcopy.DestinationTableName = "OpCoSourcingweekly"
                bulkcopy.BulkCopyTimeout = 10000000

                Try
                    bulkcopy.WriteToServer(dataset1.Tables(0))
                Catch ex As Exception
                    Dim error1 As String
                    error1 = ex.Message
                    MsgBox("ERROR: " & error1)
                End Try
                bulkcopy.Close()

            End Using
            dbCommand.CommandTimeout = 100000
        End Using
        queryString = "delete from dbo.OpCoSourcingweekly where [Fiscal Year Week] = '201233'"

        Using dbCommand1 As New SqlCommand(queryString, connection)
            dbCommand1.CommandText = queryString
            dbCommand1.Connection = connection
            dbCommand1.CommandTimeout = 100000
            dbCommand1.ExecuteNonQuery()
        End Using

        queryString = "delete from dbo.OpCoSourcingweekly where [Fiscal Year Week] = '201234'"
        Using dbCommand2 As New SqlCommand(queryString, connection)
            dbCommand2.CommandText = queryString
            dbCommand2.Connection = connection
            dbCommand2.CommandTimeout = 100000
            dbCommand2.ExecuteNonQuery()
        End Using
        MsgBox("Done with import to SQL")
        connection.Close()
    End Using
End Sub
4

2 回答 2

1

加快删除整个表的一种方法是使用TRUNCATE TABLE命令。

使用它有限制(与外键有关),但如果你能够使用它,它会非常快。

于 2012-07-19T15:59:03.210 回答
0

5年后......我遇到了同样的问题并通过将表引擎从MyISAN更改为InnoDB解决了这个问题

于 2017-08-10T19:39:58.957 回答