所以我有一个有1000行的表,我只是说
select * from hugedata limit 1
查询计划只是说table scan
然后limit operator
附加的是查询计划
[
{
"Plan": {
"Node Type": "Limit",
"Parallel Aware": false,
"Async Capable": false,
"Startup Cost": 0.00,
"Total Cost": 0.02,
"Plan Rows": 1,
"Plan Width": 41,
"Actual Startup Time": 0.007,
"Actual Total Time": 0.007,
"Actual Rows": 1,
"Actual Loops": 1,
"Output": ["pk", "description", "flags"],
"Shared Hit Blocks": 1,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"WAL Records": 0,
"WAL FPI": 0,
"WAL Bytes": 0,
"Plans": [
{
"Node Type": "Seq Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "hugedata",
"Schema": "public",
"Alias": "hugedata",
"Startup Cost": 0.00,
"Total Cost": 15406.01,
"Plan Rows": 1000001,
"Plan Width": 41,
"Actual Startup Time": 0.006,
"Actual Total Time": 0.006,
"Actual Rows": 1,
"Actual Loops": 1,
"Output": ["pk", "description", "flags"],
"Shared Hit Blocks": 1,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0,
"WAL Records": 0,
"WAL FPI": 0,
"WAL Bytes": 0
}
]
},
"Settings": {
},
"Planning": {
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
},
"Planning Time": 0.035,
"Triggers": [
],
"Execution Time": 0.069
}
]
问题是限制子句在postgres中是如何应用的?
或
从我的搜索中,它告诉我限制运算符的工作原理与第一点建议的一样,如果这是真的,为什么实现效率如此低下,或者我错过了什么?
Limit 文档说它在生成查询计划时考虑了限制,但它是否在 N+1 行产生产量或考虑意味着未指定其他内容...https://www.postgresql.org/docs/current /queries-limit.html
PostgreSQL 执行器按需自上而下工作。
为了计算第一个结果行,它从
Limit
节点获取第一行。依次从 Seq Scan
中获取第一行。
当 PostgreSQL 从
Limit
节点获取第二个结果行时,通知执行完成。
总之,从顺序扫描中仅获取一行。