首页 > 解决方案 > 为什么postresql排序没有order by?

问题描述

请参阅下面的超级简单的 sql。

   select   doc_type, translation
   from     document, labels
   where    labels.id=document.doc_type

[
  {
    "Plan": {
      "Node Type": "Merge Join",
      "Parallel Aware": false,
      "Async Capable": false,
      "Join Type": "Inner",
      "Startup Cost": 338397.68,
      "Total Cost": 614895.84,
      "Plan Rows": 18432774,
      "Plan Width": 34,
      "Actual Startup Time": 0.011,
      "Actual Total Time": 0.012,
      "Actual Rows": 0,
      "Actual Loops": 1,
      "Inner Unique": false,
      "Merge Cond": "(labels.id = document.doc_type)",
      "Plans": [
        {
          "Node Type": "Sort",
          "Parent Relationship": "Outer",
          "Parallel Aware": false,
          "Async Capable": false,
          "Startup Cost": 82.93,
          "Total Cost": 86.2,
          "Plan Rows": 1310,
          "Plan Width": 34,
          "Actual Startup Time": 0.011,
          "Actual Total Time": 0.011,
          "Actual Rows": 0,
          "Actual Loops": 1,
          "Sort Key": [
            "labels.id"
          ],
          "Sort Method": "quicksort",
          "Sort Space Used": 25,
          "Sort Space Type": "Memory",
          "Plans": [
            {
              "Node Type": "Seq Scan",
              "Parent Relationship": "Outer",
              "Parallel Aware": false,
              "Async Capable": false,
              "Relation Name": "labels",
              "Alias": "labels",
              "Startup Cost": 0,
              "Total Cost": 15.1,
              "Plan Rows": 1310,
              "Plan Width": 34,
              "Actual Startup Time": 0.008,
              "Actual Total Time": 0.008,
              "Actual Rows": 0,
              "Actual Loops": 1
            }
          ]
        },
        {
          "Node Type": "Sort",
          "Parent Relationship": "Inner",
          "Parallel Aware": false,
          "Async Capable": false,
          "Startup Cost": 338314.76,
          "Total Cost": 345350.17,
          "Plan Rows": 2814164,
          "Plan Width": 2,
          "Actual Startup Time": 0,
          "Actual Total Time": 0,
          "Actual Rows": 0,
          "Actual Loops": 0,
          "Sort Key": [
            "document.doc_type"
          ],
          "Plans": [
            {
              "Node Type": "Seq Scan",
              "Parent Relationship": "Outer",
              "Parallel Aware": false,
              "Async Capable": false,
              "Relation Name": "document",
              "Alias": "document",
              "Startup Cost": 0,
              "Total Cost": 36857.64,
              "Plan Rows": 2814164,
              "Plan Width": 2,
              "Actual Startup Time": 0,
              "Actual Total Time": 0,
              "Actual Rows": 0,
              "Actual Loops": 0
            }
          ]
        }
      ]
    },
    "Planning Time": 0.114,
    "Triggers": [],
    "Execution Time": 0.033
  }
]



使用解释分析我可以看到 postgres 正在做一个排序,但我不要求它(没有 order by)。我如何防止 postgres 在不需要时进行排序?所以假设表格文档中有2条记录得到这个

1、“人”

2、“女人”

还可以

2、“女人”

1、“人”

在这个超级简单的 sql 上,这种排序不是问题,但首先我想了解为什么会发生这种情况,在巨大的表上,我想这对于你甚至不想要的东西来说可能是昂贵的

问候,约翰

标签: postgresqlsorting

解决方案


排序不仅仅用于完成 ORDER BY。排序也是一种使相等的值彼此相邻的方法。这对于加入、聚合或唯一化很有用。具体来说,您的计划是进行合并连接。合并连接要求每个输入按连接键排序。

为什么要进行合并联接而不是哈希联接?我不知道,但显然你的统计数据是疯狂的。这可能与此有关,无论如何,尝试对这种情况进行微观管理是不值得的。


推荐阅读