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

标签 vba postgresql odbc ado

我在 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/

相关文章:

Excel自定义RibbonUI;为菜单添加部分标题

postgresql - 结合其他列对 JSONB 键建立索引

sql - Postgresql触发器函数语法错误

php - exec ('script.ps1' ) 返回错误,cmd> script.ps1 运行成功

c# - 为什么在检查空值后从 OdbcDataReader 读取值时会出现 InvalidCastException?

VBA:复制和粘贴数据行的最快方法

vba - 循环为一行中的每个新数据创建一个新工作表 - MS Excel

sql - 检查整个表中的单个值

mysql - 通过 ODBC 访问内存中的 sqlite 数据库

vba - 将数据复制并粘贴到所选行