问题描述
我在PostgreSQL数据库中有一个视图.在pgAdmin中执行视图的速度非常快(10,000条记录).但是执行"Select * From myView;".来自VBA的速度非常慢.我使用ADO和ODBC连接到数据库.谁能给我提示如何加快速度?
I have a view in a PostgreSQL database. Executing the view in pgAdmin is very fast (10,000 records). But executing "Select * From myView;" from VBA is VERY slow. I connect to the database using ADO and ODBC. Can anyone give me a hint on how to speed up things?
一个小例子:
Sub TEST()
Dim CN As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim Data As Variant
Dim SQL As String
Dim ConStr As String
ConStr = "Server=11.22.33.44;" & _
"DSN=PostgreSQL35W 32bit;" & _
"UID=xxx;" & _
"PWD=xxx;" & _
"Database=xxx;" _ &
"Port=5432;" & _
"CommandTimeout=12"
SQL = "Select * From myView;"
Debug.Print Now()
CN.ConnectionString = ConStr
CN.Open
Debug.Print Now()
RS.ActiveConnection = CN
RS.Source = SQL
RS.CursorType = adOpenStatic
RS.LockType = adLockReadOnly
Debug.Print Now()
RS.Open
Debug.Print Now()
Data = RS.GetRows
Debug.Print Now()
RS.Close
CN.Close
Set RS = Nothing
Set CN = Nothing
End Sub
这给出了输出:
10/08/2016 16:14:26
10/08/2016 16:14:26
10/08/2016 16:14:26
10/08/2016 16:14:38
10/08/2016 16:18:50
"RS.Open"的时间为00:00:12,而"Data = RS.GetRows"的时间为00:04:12.在pgAdmin中,只需不到一秒钟即可显示所有10,000条记录.
That is "RS.Open" takes 00:00:12, and "Data = RS.GetRows" 00:04:12.In pgAdmin it takes less than a second to show all 10,000 records.
推荐答案
我发现要使用OLE DB.而且快!
已下载的PgOleDb: https://www.connectionstrings.com/pgoledb/info-and-download
将两个DLL复制到C:\ Windows \ SysWOW64.
跑了"Regsvr32 PGOLEDB.DLL".
连接字符串: https://www.connectionstrings.com/pgoledb
I found out to use OLE DB. And it is FAST!
Downloaded PgOleDb: https://www.connectionstrings.com/pgoledb/info-and-download
Copied the two DLLs to C:\Windows\SysWOW64.
Ran "Regsvr32 PGOLEDB.DLL".
Connection string: https://www.connectionstrings.com/pgoledb
Provider=PostgreSQL OLE DB Provider;Data Source=myServerAddress;
location=myDataBase;User ID=myUsername;password=myPassword;
命令超时= 1000;"不起作用.
The command "timeout=1000;" does not function.
这篇关于从Excel VBA到PostgreSQL数据库的连接缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!