在 PostgreSQL 中读取 bytea 数据很慢

Read bytea data is slow in PostgreSQL

我将数据存储在 Windows 上的 PostgreSQL 9.5 数据库的 bytea 列中。

数据传输速度低于我的预期:大约每秒 1.5mb。

下面的代码

        using (var conn = ConnectionProvider.GetOpened())
        using (var comm = new NpgsqlCommand("SELECT mycolumn FROM mytable", conn))
        using (var dr = comm.ExecuteReader())
        {
            var clock = Stopwatch.StartNew();
            while (dr.Read())
            {
                var bytes = (byte[])dr[0];
                Debug.WriteLine($"bytes={bytes.Length}, time={clock.Elapsed}");
                clock.Restart();
            }
        }

产生以下输出

bytes=3895534, time=00:00:02.4397086
bytes=4085257, time=00:00:02.7220734
bytes=4333460, time=00:00:02.4462513
bytes=4656500, time=00:00:02.7401579
bytes=5191876, time=00:00:02.7959250
bytes=5159785, time=00:00:02.7693224
bytes=5184718, time=00:00:03.0613514
bytes=720401, time=00:00:00.0227767
bytes=5182772, time=00:00:02.7704914
bytes=538456, time=00:00:00.2996142
bytes=246085, time=00:00:00.0003131
Total: 00:00:22.5199268

奇怪的是读取最后的246kb不到一毫秒,读取中间的720kb只用了22ms。

3秒5mb的阅读速度正常吗?我怎样才能提高阅读速度?

详情。

我的应用程序在启动时启动 PostgreSQL 服务器并在退出时将其关闭。

我用下面的代码启动服务器

public static void StartServer(string dataDirectory, int port)
{      
    Invoke("pg_ctl", $"start -w  -D \"{dataDirectory}\" -m fast -o \"-B 512MB -p {port} -c temp_buffers=32MB -c work_mem=32MB\"");
}

此外,我将存储类型更改为我的专栏:

ALTER TABLE mytable ALTER COLUMN mycolumn SET STORAGE EXTERNAL;

我在 Windows 10

上使用 npgsql 3.0.4.0 和 PostgreSQL 9.5

运行 此处使用 Npgsql 3.0.8(应该相同)、PostgreSQL 9.5、windows 10 和 Npgsql 我根本没有得到您的结果:

bytes=3895534, time=00:00:00.0022591
bytes=4085257, time=00:00:00.0208912
bytes=4333460, time=00:00:00.0228702
bytes=4656500, time=00:00:00.0237144
bytes=5191876, time=00:00:00.0317834
bytes=5159785, time=00:00:00.0268229
bytes=5184718, time=00:00:00.0159028
bytes=720401, time=00:00:00.0130150
bytes=5182772, time=00:00:00.0153306
bytes=538456, time=00:00:00.0021693
bytes=246085, time=00:00:00.0005174

首先,您 运行 反对什么服务器,是在本地主机上还是在某个远程机器上?

想到的第二件事是作为测试的一部分停止和启动服务器。您看到的缓慢性能可能是 PostgreSQL 方面热身的一部分。尝试将其删除,并在 运行 多次测试后查看结果是否可以重现。

否则这看起来像是一些环境问题(客户端计算机、服务器计算机或网络)。我会尝试在不同的机器上或在不同的设置中重现该问题,然后从那里开始。