sql - PostgreSQL 解释 : why does child have larger cost than parent?

标签 sql postgresql query-optimization

为什么一些较低的规划节点的成本高于最顶层的节点?在 this article , 我找到了这个例子

EXPLAIN SELECT *
FROM tenk1 t1, onek t2
WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2;

                                        QUERY PLAN
------------------------------------------------------------------------------------------
 Merge Join  (cost=198.11..268.19 rows=10 width=488)
   Merge Cond: (t1.unique2 = t2.unique2)
   ->  Index Scan using tenk1_unique2 on tenk1 t1  (cost=0.29..656.28 rows=101 width=244)
         Filter: (unique1 < 100)
   ->  Sort  (cost=197.83..200.33 rows=1000 width=244)
         Sort Key: t2.unique2
         ->  Seq Scan on onek t2  (cost=0.00..148.00 rows=1000 width=244)

子节点索引扫描比merge join节点的开销更大。文章中还说“重要的是要了解上层节点的成本包括其所有子节点的成本”。

那么为什么 child 的成本比它的 parent 大呢?

最佳答案

如您所见,费用确实包括所有 child 费用 :) 子索引的行可能比您加入查询的结果多。因此,如果 tenk1_unique2 是 unique2 和其他一些值的复合唯一键,并且是此连接的最便宜索引,它可以包含 101 行和一个 unique2 值......这样你就有 101 与 1 进行比较并得到 1 行结果。 ..

更新1 parent 的成本是 child 成本的总和。但是加入的 child 的行数更大。但是如果你加入 1 对 101,你会得到 1。所以 parent 可以有 1 行,而 child 有 100 行......

Update2 我想是句子

the cost of an upper-level node includes the cost of all its child nodes

仅对

有效

Estimated start-up cost

更新3

我的同事说估计总成本 (656.28) 在应用过滤器之前出现...

关于sql - PostgreSQL 解释 : why does child have larger cost than parent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29244695/

相关文章:

postgresql - 如何退出 PostgreSQL 命令行实用程序 : psql

query-optimization - 如何优化我的递归 SPARQL 查询?

sql - 如何以通用方式在 Postgres 中添加空行

sql - '.'附近的语法不正确。在更新变量表时

sql - 重新编号文本列中的引用变量

c# - 获取第一个表插入的值到第二个表

java第二个sql语句不工作

java - 似乎无法为本地heroku应用程序设置本地java envar DATABASE url

php - 优化 MySQL 查询以获得正确的页面模板

MySQL 查询从 1000 万行表中获取每个条目的最新记录