【问题标题】:Slow connection from Excel VBA to PostgreSQL database从 Excel VBA 到 PostgreSQL 数据库的连接缓慢
【发布时间】:2016-08-10 14:48:59
【问题描述】:

我在 PostgreSQL 数据库中有一个视图。在 pgAdmin 中执行视图非常快(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 条记录只需不到一秒钟的时间。

【问题讨论】:

  • 您在使用adOpenForwardOnlyadOpenStatic 时是否看到任何改进?
  • 另外,当您使用 DSN 时,它必须在建立连接之前进入注册表,而且速度较慢。 Check this out for some tips that may help
  • 也尝试异步处理。见:support.microsoft.com/en-ca/kb/190988
  • 也尝试设置 RS.CusorLocation = adUseClient,看看是否有帮助。
  • adOpenStatic/adOpenForwardOnly 和 adUseServer/adUseClient 的所有四种组合都提供相同的执行时间。

标签: vba postgresql odbc ado


【解决方案1】:

我发现使用 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;”不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 2020-07-11
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多