简体   繁体   English

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

[英]Slow connection from Excel VBA to PostgreSQL database

I have a view in a PostgreSQL database.我在 PostgreSQL 数据库中有一个视图。 Executing the view in pgAdmin is very fast (10,000 records).在 pgAdmin 中执行视图非常快(10,000 条记录)。 But executing "Select * From myView;"但是执行“Select * From myView;” from VBA is VERY slow.从 VBA 非常慢。 I connect to the database using ADO and ODBC.我使用 ADO 和 ODBC 连接到数据库。 Can anyone give me a hint on how to speed up things?谁能给我一个关于如何加快速度的提示?

A small example:一个小例子:

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

This gives output:这给出了输出:

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

That is "RS.Open" takes 00:00:12, and "Data = RS.GetRows" 00:04:12.即“RS.Open”需要 00:00:12,“Data = RS.GetRows”需要 00:04:12。 In pgAdmin it takes less than a second to show all 10,000 records.在 pgAdmin 中,显示所有 10,000 条记录只需不到一秒钟。

I found out to use OLE DB.我发现要使用 OLE DB。 And it is FAST!而且速度很快!
Downloaded PgOleDb: https://www.connectionstrings.com/pgoledb/info-and-download下载 PgOleDb: https ://www.connectionstrings.com/pgoledb/info-and-download
Copied the two DLLs to C:\\Windows\\SysWOW64.将两个 DLL 复制到 C:\\Windows\\SysWOW64。
Ran "Regsvr32 PGOLEDB.DLL".运行“Regsvr32 PGOLEDB.DLL”。
Connection string: https://www.connectionstrings.com/pgoledb连接字符串: https : //www.connectionstrings.com/pgoledb

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

The command "timeout=1000;"命令“超时=1000;” does not function.不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM