我最近将应用程序的数据库从 MS Access 迁移到 SQL Server。我真的很震惊 - 即使有一个很好的服务器 - 来自前端 Access 的大多数 INSERT INTO
查询都非常慢。以前在检索数据时我将其放入临时 Access 表然后在检索完成后我做了:
INSERT INTO permanentTable
SELECT *
FROM tmpTable ;
并且将所有记录移动到 permanentTable
只用了不到 1 分钟。
现在使用 SQL Server 时略有不同 - 插入 80 行大约需要 5-6 分钟。我试图通过删除索引来优化查询(它是一个有 40 列的表)——它一点也不快。运行查询时在服务器本身上 - 它真的很快。有什么问题?尝试一个接一个地插入 - 插入一行大约需要 5 秒。
我在 MS Access 中使用带有 ADODB.conn 字符串和 ADODB.execute 方法的 ODBC 连接。
我使用的代码类似于:
Sub AdoOdbcExample()
Dim con As Object
Set con = CreateObject("ADODB.Connection") con.Open _ "Driver={SQL Server Native Client 11.0};" & _ "Server=.\SQLEXPRESS;" & _ "Database=myDb;" & _ "Trusted_Connection=yes;"
con.Execute "UPDATE Clients SET FirstName='Gord' WHERE ID=5;"
con.Close Set con = Nothing
End Sub
此外,这是我的连接字符串的样子:
Dim conn As ADODB.Connection
Dim sConnString As String
' Create the connection string.
sConnString = "Driver={ODBC Driver 13 for SQL Server};server=nameserver;database=dbname;trusted_connection=Yes;"
' Create the Connection
Set conn = New ADODB.Connection
' Open the connection and execute.
conn.Open sConnString
conn.Execute (query)
conn.Close
我正在运行的查询是:
CurrentDb.Execute ("INSERT INTO [ODBC;Driver={ODBC Driver 13 for SQL Server};server=serverName;database=dbName;trusted_connection=Yes;].permanentTable SELECT * FROM tmpTable")
最佳答案
为这个问题找到了一个非常快速的解决方案。基本上从Access表中SELECT所有内容然后插入SQL Server是不好的,传输表也很慢,而且一行一行地插入也不是很快。解决方案是批量更新。
基本上我将我的字符串存储在
Dim sBulkString as String
for i = 1 to n...
sBulkString = sBulkString & "INSERT INTO...; "
next i
conn.Execute sBulkString, , adCmdText
关于sql - 从 Access 迁移到 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45986758/