PostgreSQL 相当于 MySQL 内存表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11729690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:20:01  来源:igfitidea点击:

PostgreSQL equivalent of MySQL memory tables?

mysqlpostgresqltemp-tables

提问by Elliot B.

Does PostgreSQL have an equivalent of MySQL memory tables?

PostgreSQL 有相当于 MySQL 的内存表吗?

These MySQL memory tables can persist across sessions (i.e., different from temporary tables which drop at the end of the session). I haven't been able to find anything with PostgreSQL that can do the same.

这些 MySQL 内存表可以跨会话保持(即,不同于在会话结束时删除的临时表)。我还没有找到任何可以做同样事情的 PostgreSQL 东西。

回答by aleroot

No, at the moment they don't exist in PostgreSQL. If you truly need a memory table you can create a RAM disk, add a tablespacefor it, and create tables on it.

不,目前它们不存在于 PostgreSQL 中。如果您确实需要一个内存表,您可以创建一个 RAM 磁盘,为其添加一个表空间,然后在其上创建表。

If you only need the temporary table that is visible between different sessions, you can use an UNLOGGEDtable. These are not true memory tables but they'll behave surprisingly similarly when the table data is significantly smaller than the system RAM.

如果您只需要在不同会话之间可见的临时表,则可以使用UNLOGGEDtable。这些不是真正的内存表,但当表数据明显小于系统 RAM 时,它们的行为会惊人地相似。

Global temporary tables would be another option but are not supported in PostgreSQL as of 9.2 (see comments).

全局临时表将是另一种选择,但自 9.2 起在 PostgreSQL 中不受支持(请参阅注释)。

回答by Madusudanan

Answering a four year old question but since it comes on top of google search results even now.

回答一个四年前的问题,但因为它现在已经出现在谷歌搜索结果之上。

There is no built in way to cache a full table in memory, but there is an extension that can do this.

没有内置的方法可以在内存中缓存一个完整的表,但是有一个扩展可以做到这一点。

In Memory Column Storeis a library that acts as a drop in extension and also as a columnar storage and execution engine. You can refer herefor the documentation. There is a load function that you can use to load the entire table into memory.

In Memory Column Store是一个库,它既可以作为扩展的一部分,也可以作为列式存储和执行引擎。你可以参考这里的文档。您可以使用加载函数将整个表加载到内存中。

The advantage is the table is stored inside postgres shared_buffers, so when executing a query postgres immediately senses that the pages are in memory and fetches from there.

优点是该表存储在 postgres shared_buffers 中,因此在执行查询时,postgres 会立即感知页面在内存中并从那里获取。

The downside is that shared_buffers is not really designed to operate in such a way and instabilities might occur (usually it doesn't), but you can probably have this in a secondary cluster/machine with this configuration just to be safe.

缺点是 shared_buffers 并不是真正设计为以这种方式运行,并且可能会发生不稳定性(通常不会),但是为了安全起见,您可以在具有此配置的辅助集群/机器中使用它。

All other usual caveats about postgres and shared_buffers still apply.

所有其他关于 postgres 和 shared_buffers 的常见警告仍然适用。