gpt4 book ai didi

vba - 从 Excel VBA 到 PostgreSQL 数据库的连接缓慢

转载 作者:行者123 更新时间:2023-11-29 12:57:43 25 4
gpt4 key购买 nike

我在 PostgreSQL 数据库中有一个 View 。在 pgAdmin 中执行 View 非常快(10,000 条记录)。但是执行“Select * From 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,而“Data = RS.GetRows”需要 00:04:12。在 pgAdmin 中,显示所有 10,000 条记录只需不到一秒钟。

最佳答案

我发现可以使用 OLE DB。而且速度很快!
下载 PgOleDb:https://www.connectionstrings.com/pgoledb/info-and-download
将两个 DLL 复制到 C:\Windows\SysWOW64。
运行“Regsvr32 PGOLEDB.DLL”。
连接字符串:https://www.connectionstrings.com/pgoledb

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

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

关于vba - 从 Excel VBA 到 PostgreSQL 数据库的连接缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com