Postgres Npgsql 连接速度比 SQL Server 慢

发布于 2024-09-16 09:47:51 字数 1040 浏览 8 评论 0原文

为什么通过互联网或 VPN 连接到 postgres 比 sql server 慢? 我进行了测试,

    DateTime now0 =  System.DateTime.Now;
    string sqlconnectsrting = "server=xxx.xxx.xxx.xxx;database=pubs;uid=sa;pwd=";
    for (int i = 0; i < 1000; i++)
    {
        SqlConnection connect = new SqlConnection(sqlconnectsrting);
        connect.Open();
        connect.Close();
    }
    System.DateTime now1 = System.DateTime.Now;
    Console.WriteLine(String.Format("SQL Connect time : {0}", now1 - now0));

    now0 = System.DateTime.Now;
    string npgconnectsrting = "Server=xxx.xxx.xxx.xxx;Port=5433;User Id=postgres;Password=postgres;Database=hr_data;";
    for (int i = 0; i < 1000; i++)
    {
        NpgsqlConnection connect = new NpgsqlConnection(npgconnectsrting);
        connect.Open();
        connect.Close();
    }
    now1 = System.DateTime.Now;
    Console.WriteLine(String.Format("Postgres Connect time : {0}", now1 - now0));

当连接是本地主机时,它们是相似的,但是当连接是通过互联网时。 SQL 连接需要 1-5 秒,但 postgres 需要 3-7 分钟。 有办法解决这个问题吗?

端黄英

why connect to postgres over internet or VPN slower than sql server ?
I have test

    DateTime now0 =  System.DateTime.Now;
    string sqlconnectsrting = "server=xxx.xxx.xxx.xxx;database=pubs;uid=sa;pwd=";
    for (int i = 0; i < 1000; i++)
    {
        SqlConnection connect = new SqlConnection(sqlconnectsrting);
        connect.Open();
        connect.Close();
    }
    System.DateTime now1 = System.DateTime.Now;
    Console.WriteLine(String.Format("SQL Connect time : {0}", now1 - now0));

    now0 = System.DateTime.Now;
    string npgconnectsrting = "Server=xxx.xxx.xxx.xxx;Port=5433;User Id=postgres;Password=postgres;Database=hr_data;";
    for (int i = 0; i < 1000; i++)
    {
        NpgsqlConnection connect = new NpgsqlConnection(npgconnectsrting);
        connect.Open();
        connect.Close();
    }
    now1 = System.DateTime.Now;
    Console.WriteLine(String.Format("Postgres Connect time : {0}", now1 - now0));

When connect is localhost they are similar, but when connect is over internet. SQL connect take 1-5s but postgres take 3-7 minutes.
Is there anyway to fix this?

Tuan Hoang Anh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追我者格杀勿论 2024-09-23 09:47:51

SSL 的设置是什么?

SHOW ssl;

即使您实际上不使用 SSL,创建 SSL 连接也会变慢。如果您不需要额外的安全性,请将其关闭。

3到7分钟大约是180到420秒,如果创建1000个连接,每个连接“只”需要0.18到0.42秒。 SQL Server 不会在 1 到 5 秒内通过网络建立 1000 个连接,网络会太慢。听起来像是一个连接池,让您相信您已经创建了一个新连接。

What's the setting for SSL?

SHOW ssl;

Creating an SSL-connection will be slower, even when you actualy don't use SSL. Turn it off if you don't need the extra security.

3 to 7 minutes is about 180 to 420 seconds, if you create 1000 connections, it will "only" take 0.18 to 0.42 seconds for each connection. SQL Server is not going to make a 1000 connections within 1 to 5 seconds over a network, the network will be too slow. Sounds like a connection pool that let you believe you have created a new connection.

给不了的爱 2024-09-23 09:47:51

3-7 分钟对于连接来说绝对是太长了(我不确定这个时间是针对单个连接还是针对您正在尝试的 1000 个连接)

我很确定网络存在问题。

“通过互联网”到底是什么意思?
这些服务器真的位于公共互联网中吗?
SQL Server 和 Postgres 是否安装在同一台计算机上?

如果没有,这些机器位于哪里?如果两个 DBMS 安装在不同的计算机上,我会研究 Postgres 服务器的网络配置。

除了一般的“互联网”问题(例如路由缓慢)之外,它也可能是(物理)网络接口的问题。我们曾经遇到过这样的情况,因为网卡有故障,每个数据包都会重新发送 3-4 次,这会大大减慢速度。

3-7 minutes is definitely to long for a connect (I'm not sure if that time is for a single connect or for the 1000 connects you are trying)

I'm pretty sure there is a problem with the network.

What exactly do you mean with "over the internet"?
Are those servers really located in the public internet?
Are SQL Server and Postgres installed on the same machine?

If not, where are those machines located? If both DBMS are installed on different machines, I'd look into the network configuration for the Postgres server.

Apart from a general "internet" problem (e.g. slow routing) it could also be a problem with the (physical) network interface. We once had that with a faulty network card, that was resending every packet 3-4 times, which slowed down things considerably.

羁绊已千年 2024-09-23 09:47:51

当您建立连接时,Npgsql 尝试创建 minpoolsize 连接。首次连接时,它会运行一些查询来检查类型和一些支持的功能。也许这会减慢你的测试速度。

奇怪的是,Npgsql 仅在第一次连接时执行此操作,因此,此后您应该可以顺利运行。您介意只测试 2 个循环,而不是 1000 个循环,看看效果如何吗?

When you make a connection, Npgsql tries to create minpoolsize connections. At first connecting, it runs some queries to check types and some supported features. Maybe this is slowing down your tests.

What is strange is that Npgsql only does that for the first connection, so, you should have an smooth run after that. Would you mind to test only 2 loops, instead of 1000 and see how it goes?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文