性能调优 PostgreSQL

发布于 2024-07-18 06:14:10 字数 115 浏览 9 评论 0原文

请记住,我是 sql/数据库领域的菜鸟。

我每秒插入/更新数千个对象。 这些对象会以多秒的间隔被主动查询。

我应该做哪些基本事情来调整我的(postgres)数据库的性能?

Keep in mind that I am a rookie in the world of sql/databases.

I am inserting/updating thousands of objects every second. Those objects are actively being queried for at multiple second intervals.

What are some basic things I should do to performance tune my (postgres) database?

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

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

发布评论

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

评论(6

生生不灭 2024-07-25 06:14:10

这是一个广泛的主题,因此这里有很多内容供您阅读。

  • EXPLAIN 和 EXPLAIN ANALYZE 对于理解正在发生的事情非常有用你的数据库引擎
  • 确保相关的列被索引
  • 确保不相关的列没有被索引(如果必须更新太多索引,插入/更新性能可能会下降)
  • 确保你的 postgres.conf 是正确调整
  • 了解 work_mem 是什么,以及它如何影响您的查询(对于较大的查询最有用)
  • 确保您的数据库正确规范化
  • VACUUM 用于清除旧数据
  • ANALYZE 用于更新统计信息(统计量的统计目标)
  • 持久连接(您可以使用 pgpool 或 pgbouncer 等连接管理器)
  • 了解查询的构造方式(连接、子选择、游标)
  • 数据缓存(即 memcached)是一个选项

当您用尽这些选项时:添加更多内存、更快的磁盘子系统等。硬件很重要,尤其是在较大的数据集上。

当然,请阅读 postgres/databases 上的所有其他线程。 :)

It's a broad topic, so here's lots of stuff for you to read up on.

  • EXPLAIN and EXPLAIN ANALYZE is extremely useful for understanding what's going on in your db-engine
  • Make sure relevant columns are indexed
  • Make sure irrelevant columns are not indexed (insert/update-performance can go down the drain if too many indexes must be updated)
  • Make sure your postgres.conf is tuned properly
  • Know what work_mem is, and how it affects your queries (mostly useful for larger queries)
  • Make sure your database is properly normalized
  • VACUUM for clearing out old data
  • ANALYZE for updating statistics (statistics target for amount of statistics)
  • Persistent connections (you could use a connection manager like pgpool or pgbouncer)
  • Understand how queries are constructed (joins, sub-selects, cursors)
  • Caching of data (i.e. memcached) is an option

And when you've exhausted those options: add more memory, faster disk-subsystem etc. Hardware matters, especially on larger datasets.

And of course, read all the other threads on postgres/databases. :)

拥抱影子 2024-07-25 06:14:10

首先也是最重要的,阅读官方手册的性能提示

对所有查询运行 EXPLAIN 并了解其输出将让您了解您的查询是否尽可能快,以及是否应该添加索引。

完成此操作后,我建议您阅读服务器配置< /a> 手册的一部分。 有许多选项可以进行微调以进一步提高性能。 但请务必了解您设置的选项,因为如果设置不正确,它们也很容易影响性能。

请记住,每次您更改查询或选项时,都要测试基准,以便了解每次更改的效果。

First and foremost, read the official manual's Performance Tips.

Running EXPLAIN on all your queries and understanding its output will let you know if your queries are as fast as they could be, and if you should be adding indexes.

Once you've done that, I'd suggest reading over the Server Configuration part of the manual. There are many options which can be fine-tuned to further enhance performance. Make sure to understand the options you're setting though, since they could just as easily hinder performance if they're set incorrectly.

Remember that every time you change a query or an option, test and benchmark so that you know the effects of each change.

乱了心跳 2024-07-25 06:14:10

实际上,有一些简单的规则可以让您在大多数情况下获得足够的性能:

  1. 索引是第一部分。 主键会自动建立索引。 我建议在所有外键上放置索引。 进一步在所有经常查询的列上建立索引,如果一个表上有大量查询,并且查询了多个列,则将这些列一起建立索引。

  2. postgresql 安装中的内存设置。 将以下参数设置得更高:

.

shared_buffers, work_mem, maintenance_work_mem, temp_buffers

如果是专用数据库机器,您可以轻松地将其中的前 3 个设置为 ram 的一半(在具有共享缓冲区的 linux 下要小心,也许您必须调整 shmmax 参数),在任何其他情况下,这取决于有多少 ram你想给 postgresql.

http://www.postgresql.org/docs/8.3/交互式/runtime-config-resource.html

Actually there are some simple rules which will get you in most cases enough performance:

  1. Indices are the first part. Primary keys are automatically indexed. I recommend to put indices on all foreign keys. Further put indices on all columns which are frequently queried, if there are heavily used queries on a table where more than one column is queried, put an index on those columns together.

  2. Memory settings in your postgresql installation. Set following parameters higher:

.

shared_buffers, work_mem, maintenance_work_mem, temp_buffers

If it is a dedicated database machine you can easily set the first 3 of these to half the ram (just be carefull under linux with shared buffers, maybe you have to adjust the shmmax parameter), in any other cases it depends on how much ram you would like to give to postgresql.

http://www.postgresql.org/docs/8.3/interactive/runtime-config-resource.html

﹉夏雨初晴づ 2024-07-25 06:14:10

The absolute minimum I'll recommend is the EXPLAIN ANALYZE command. It will show a breakdown of subqueries, joins, et al., all the time showing the actual amount of time consumed in the operation. It will also alert you to sequential scans and other nasty trouble.

It is the best way to start.

因为看清所以看轻 2024-07-25 06:14:10

如果你信任你的文件系统,请将 fsync = off 放入你的 posgresql.conf 中,否则每个 postgresql 操作将立即写入磁盘(使用 fsync 系统调用)。
十多年来,我们在许多生产服务器上都关闭了这个选项,并且从未发生过数据损坏。

Put fsync = off in your posgresql.conf, if you trust your filesystem, otherwise each postgresql operation will be imediately written to the disk (with fsync system call).
We have this option turned off on many production servers since quite 10 years, and we never had data corruptions.

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