最近,我将应用程序的数据库从MS Access迁移到了SQL Server。我真的很震惊-尽管有一个很好的服务器-大多数来自前端访问的INSERT INTO查询都非常慢。以前,在检索数据时,我将其放入临时访问表中,然后在检索完成后,我这样做了:
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")发布于 2017-09-01 02:35:09
找到了这个问题的极快解决方案。基本上,从Access表中选择所有内容然后将其插入到SQL Server中是不好的,传输表的速度也很慢,另外,一行接一行地插入也不是很快。它的解决方案是批量更新。
基本上我把我的字符串存储在
Dim sBulkString as String
for i = 1 to n...
sBulkString = sBulkString & "INSERT INTO...; "
next i
conn.Execute sBulkString, , adCmdTexthttps://stackoverflow.com/questions/45986758
复制相似问题