如何找到程序的限制因素?

发布于 2024-11-13 07:04:48 字数 1197 浏览 5 评论 0原文

我创建了一个程序,可以解析文件中的数据并将其导入到关系 postgresql 数据库中。 该计划已经运行了两周,看起来还剩下几天。平均每秒约 150 个导入。 如何找到限制因素并使其进展更快? 我的程序的 CPU 不会超过 10%,内存不会超过 7%。 Postgresql 数据库 CPU 不会超过 10%,内存不会超过 25%。

我猜测限制因素是硬盘写入速度,但我如何验证,如果是这样;改善吗? (无需购买更快的硬盘)

这是“iostat -d -x”的输出:

Device:         rrqm/s   wrqm/s     r/s     w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.59     3.23    0.61    1.55    23.15    38.37    28.50     0.01    5.76   1.04   0.22
sdb               0.02   308.37   21.72  214.53   706.41  4183.68    20.70     0.56    2.38   2.24  52.89

正如您可能猜到的那样,数据库位于 sdb 上。

编辑:我正在解析的文件约为 7GB。 对于文件中的大多数(但不是全部)数据,我逐行查看,这里是一个示例“

  1. 返回 tableA 中partA的ID。
    • 如果 ID 不存在,则将 PartA 插入 tableA 返回 ID
  2. 返回tableB中partB的ID。
    • 如果ID不存在,则将partB插入tableB中,返回ID
  3. 返回partA和partB多对多关系的ID。
    • 如果关系 ID 不存在,则创建它。
    • 否则更新关系(使用日期 ID)
  4. 移至下一行。

为了节省许多查询,我将插入的 PartA 和 PartB 项的 ID 保存在内存中以减少查找。

这是我的 postgresql.conf 文件的副本: http://pastebin.com/y9Ld2EGz 我唯一更改的内容其中默认数据目录和内存限制。

I've created a program that parses data from a file and imports it into a relational postgresql database.
The program has been running for 2 weeks and looks like it has a few more days left. It is averaging ~150 imports a second. How can I find the limiting factor and make it go faster?
The CPU for my program does not go above 10%, The Memory does not go above 7%.
The Postgresql database CPU does not go above 10%, and 25% Memory.

I'm guessing that the limiting factor is the hard-disk write speed, but how can I verify, and if the case; improve it? (short of buying a faster hard drive)

This is the output of "iostat -d -x":

Device:         rrqm/s   wrqm/s     r/s     w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.59     3.23    0.61    1.55    23.15    38.37    28.50     0.01    5.76   1.04   0.22
sdb               0.02   308.37   21.72  214.53   706.41  4183.68    20.70     0.56    2.38   2.24  52.89

As you can likely guess, the database is on sdb.

EDIT: The file I am parsing is ~7GB.
For most (but not all) of the data in the file I go line by line, here is an example"

  1. Return the ID of partA in tableA.
    • If ID does not exist insert partA into tableA returning ID
  2. Return the ID of partB in tableB.
    • If ID does not exist insert partB into tableB returning ID
  3. Return the ID of the many-to-many relationship of partA and partB.
    • If the ID of the relationship does not exist create it.
    • else update the relationship (with a date id)
  4. move onto the next line.

To save many queries, I save the IDs of inserted PartA and PartB items in memory to reduce lookups.

Here is a copy of my postgresql.conf file: http://pastebin.com/y9Ld2EGz The only things I changed where the default data directory, and the memory limits.

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

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

发布评论

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

评论(1

您的好友蓝忘机已上羡 2024-11-20 07:04:48

您应该在很多很多天前就终止这个进程,并问不是如何找到应用程序的限制因素,而是:

有没有更快的方式导入数据
进入pgSQL?

看看这个问题然后是 pgSQL COPY 文档。使用适当的工具,您正在运行的导入过程可能会在数小时内完成,而不是数周。

顺便说一句,无论您使用什么 RDBMS,以编程方式插入数据的性能永远不如 RDBMS 供应商提供的用于处理此类批量操作的本机工具。例如:SQL Server 有 bcp、DTS/SSIS 和其他一些用于批量数据导入/导出的选项。 Oracle有自己的等。

You should have killed this process many, many days ago and asked not how to find the limiting factor of the app but rather:

Is there a faster way to import data
into pgSQL?

Take a look at this question and then the pgSQL COPY documentation. It's possible that the import process you're running could be achieved in hours rather than weeks with the proper tools.

By the way, regardless of what RDBMS you're using, programmatic insertions of data are never as performant as native tools provided by the RDBMS's vendor to handle bulk operations such as these. For example: SQL Server has bcp, DTS/SSIS and a few other options for bulk data import/export. Oracle has its own etc.

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