繁体   English   中英

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。

读取速度5mb每3秒正常吗? 如何提高阅读速度?

细节。

我的应用程序在启动时启动 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 方面预热的一部分。 尝试将其删除,看看在多次运行测试后是否可以重现结果。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM