SQL解释计划:什么是Materialize?

发布于 2024-09-05 05:40:48 字数 140 浏览 6 评论 0原文

我要求 PostgreSQL 解释我的查询。部分解释是:

table_name --> Materialize

Materialize 有什么作用?我正在连接两个表,而不是视图或类似的东西。

I asked PostgreSQL to explain my query. Part of the explanation was:

table_name --> Materialize

What does materialize do? I'm joining two tables, not views or anything like that.

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

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

发布评论

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

评论(5

樱桃奶球 2024-09-12 05:40:48

物化节点意味着树中它下面的任何内容的输出(可以是扫描,或全套连接或类似的东西)在执行上层节点之前被物化到内存中。这通常是在外部节点因某种原因需要可以重新扫描的源时完成的。

因此,在您的情况下,规划器正在确定对您的一个表的扫描结果将适合内存,并且它直到可以选择需要重新扫描的上连接操作,同时仍然更便宜。

A materialize node means the output of whatever is below it in the tree (which can be a scan, or a full set of joins or something like that) is materalized into memory before the upper node is executed. This is usually done when the outer node needs a source that it can re-scan for some reason or other.

So in your case, the planner is determining that the result of a scan on one of your tables will fit in memory, and it till make it possible to choose an upper join operation that requires rescans while still being cheaper.

抱猫软卧 2024-09-12 05:40:48

这意味着它不能使用任何索引(或类似的方法)来提高连接效率,因此最后的手段是将其中一个表的结果具体化,以便在连接另一个表时使用较小的集合来处理。

It means that it can't use any index (or similar method) to make the join efficient, so as a last resort is materializes the result from one of the tables to have a smaller set to work with when joining against the other table.

時窥 2024-09-12 05:40:48

在合并连接和嵌套循环连接中,数据库将“重新扫描”内循环。
基本上就像:

for each row in outer table:
    for each row in inner table:
        # do something

规划器将具体化内部循环表,这意味着将整个表加载到内存缓冲区中以避免昂贵的磁盘 IO 成本。

有用的链接

In merge join and nested loop join, the database will "rescan" the inner loop.
Basically like:

for each row in outer table:
    for each row in inner table:
        # do something

The planner will materializes the inner loop table which means load the whole table in an in-memory buffer to avoid the expensive disk IO cost.

A useful link.

唯憾梦倾城 2024-09-12 05:40:48

从更实证的角度来看,您可以执行 EXPLAIN ANALYZE 并保留这些结果,然后更改标志:

set enable_material=off;

再次运行相同的 EXPLAIN ANALYZE 来比较结果一行一行,您将确切地看到发生了什么变化,查询时间是好还是坏,等等。修改一长串查询配置标志并观察效果。

https://www.postgresql.org/docs/current/runtime-配置查询.html

From a more empirical point of view, you can do your EXPLAIN ANALYZE and keep those results, then change the flag:

set enable_material=off;

Go and run the same EXPLAIN ANALYZE again to compare the results line by line and you will see exactly what changes, whether the query time is better or worse, etc. Tinker around with the long list of query config flags and observe the effects.

https://www.postgresql.org/docs/current/runtime-config-query.html

寄意 2024-09-12 05:40:48

可以说Materialize命令将创建一个表的视图(就像内存中的虚拟表或表的快照)

我们 用于通过在信息更丰富的上下文中呈现数据来增强数据的可见性,并通过向不需要查看数据/或我们不希望向其显示数据的用户隐藏关键或敏感数据来控制对数据的访问。

使用 Materialize/Snap-Shot 的主要好处是减少事后查询该表的成本。
在执行计划中同样指出,如果我们使用具体化,与不使用它时相比,我们可以获得多少好处!

解释检查执行计划

->  Materialize  (cost=0.29..8.51 rows=10 width=244)
         ->  Index Scan using tenk2_unique2 on tenk2 t2  (cost=0.29..8.46 rows=10 width=244)
               Index Cond: (unique2 < 10)

我忽略了这个- -> 因为据我所知没有这样的运算符,加上 -- 之后会注释该表达式,我假设您只是指这个 ->

我希望这有帮助..

We can say the Materialize command will create a View of a table(just like a virtual table OR a SnapShot of table in Memory)

It is used to enhance visibility of the data by presenting it in a more informational context and to control access to the data by hiding critical or sensitive data from users who don't need to see it/or to whom we don't what to show it.

The main benefit of using Materialize/Snap-Shot is to reduce the cost of query on that table afterwards.
In execution plan the same states that how much benefit we may fetch if we use materialize compare to when it is not used!

Explain to check execution plan

->  Materialize  (cost=0.29..8.51 rows=10 width=244)
         ->  Index Scan using tenk2_unique2 on tenk2 t2  (cost=0.29..8.46 rows=10 width=244)
               Index Cond: (unique2 < 10)

I have ignored this --> as there is not such operator in my knowledge, plus the -- will comment the expression afterwards, I have assumed you meant this -> only.

I hope this helps..

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