从Excel VBA到PostgreSQL数据库的连接速度缓慢

时间:2016-08-10 14:48:59

标签: vba postgresql odbc ado

我在PostgreSQL数据库中有一个视图。在pgAdmin中执行视图非常快(10,000条记录)。但执行"选择*来自myView;"来自VBA非常慢。我使用ADO和ODBC连接到数据库。任何人都可以给我一个关于如何加快速度的提示吗?

一个小例子:

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,"数据= RS.GetRows" 0时04分12秒。 在pgAdmin中,显示所有10,000条记录只需不到一秒钟。

1 个答案:

答案 0 :(得分:2)

我发现使用OLE DB。它很快!
已下载PgOleDb:ternary operator
将两个DLL复制到C:\ Windows \ SysWOW64 跑“Regsvr32 PGOLEDB.DLL”。
连接字符串:https://www.connectionstrings.com/pgoledb/info-and-download

Provider=PostgreSQL OLE DB Provider;Data Source=myServerAddress;
location=myDataBase;User ID=myUsername;password=myPassword; 

命令“timeout = 1000;”不起作用。

相关问题