【问题标题】:Can two GIN indexes on the same table cause performance degradation?同一张表上的两个 GIN 索引会导致性能下降吗?
【发布时间】:2017-11-26 15:25:41
【问题描述】:

我有以下查询(postgresql):

select prt.a,
(SELECT count(ind.b) FROM ind WHERE ind.indexed_field_1 @@ to_tsquery(prt.a)
AND lower(ind.t) not in ('a', 'b', 'c')) cnt
from p as prt
order by cnt desc
limit 200;

ind.indexed_field_1 使用 GIN 索引进行索引。到目前为止一切顺利 - 它大约运行了约 1 秒(ind 有约 600K 行)。

一旦我将indexed_field_2 添加到ind 表中,并使用 GIN 索引对其进行索引 - 原始查询突然运行约 5 秒,indexed_field_2 上的类似查询也是如此:

select prt.a,
(SELECT count(ind.b) FROM ind WHERE ind.indexed_field_2 @@ to_tsquery(prt.a)
AND lower(ind.t) not in ('a', 'b', 'c')) cnt
from p as prt
order by cnt desc
limit 200;

有什么线索会导致这种情况吗?

执行计划看起来一样。

编辑

第二次查询的执行计划:

[
  {
    "Execution Time": 4863.615,
    "Planning Time": 0.3,
    "Plan": {
      "Startup Cost": 383480149.03,
      "Shared Hit Blocks": 1296773,
      "Shared Written Blocks": 0,
      "Shared Read Blocks": 267565,
      "Plans": [
        {
          "Temp Written Blocks": 0,
          "Sort Key": [
            "((SubPlan 1)) DESC"
          ],
          "Node Type": "Sort",
          "Sort Space Used": 46,
          "Shared Hit Blocks": 1296773,
          "Plans": [
            {
              "Node Type": "Seq Scan",
              "Shared Hit Blocks": 1296773,
              "Plans": [
                {
                  "Partial Mode": "Simple",
                  "Node Type": "Aggregate",
                  "Strategy": "Plain",
                  "Shared Hit Blocks": 1296773,
                  "Subplan Name": "SubPlan 1",
                  "Plans": [
                    {
                      "Exact Heap Blocks": 1383693,
                      "Node Type": "Bitmap Heap Scan",
                      "Shared Hit Blocks": 1296773,
                      "Plans": [
                        {
                          "Startup Cost": 0,
                          "Plan Width": 0,
                          "Temp Written Blocks": 0,
                          "Shared Written Blocks": 0,
                          "Shared Read Blocks": 5872,
                          "Local Written Blocks": 0,
                          "Node Type": "Bitmap Index Scan",
                          "Index Cond": "(indexed_field_2  @@ to_tsquery(prt.a)",
                          "Plan Rows": 3156,
                          "Temp Read Blocks": 0,
                          "Shared Dirtied Blocks": 0,
                          "Local Hit Blocks": 0,
                          "Parallel Aware": false,
                          "Actual Rows": 51,
                          "Local Dirtied Blocks": 0,
                          "Parent Relationship": "Outer",
                          "Total Cost": 43.68,
                          "Shared Hit Blocks": 173739,
                          "Actual Loops": 44742,
                          "Local Read Blocks": 0,
                          "Index Name": "part_ids2_idx"
                        }
                      ],
                      "Shared Read Blocks": 266531,
                      "Relation Name": "ind",
                      "Local Hit Blocks": 0,
                      "Local Dirtied Blocks": 0,
                      "Temp Written Blocks": 0,
                      "Plan Width": 16,
                      "Actual Loops": 44742,
                      "Rows Removed by Index Recheck": 0,
                      "Lossy Heap Blocks": 0,
                      "Filter": "(lower(ind.t) <> ALL ('{a,b,c}'::text[]))",
                      "Alias": "ind",
                      "Recheck Cond": "(indexed_field_2  @@ to_tsquery(prt.a)",
                      "Temp Read Blocks": 0,
                      "Local Read Blocks": 0,
                      "Startup Cost": 44.46,
                      "Shared Dirtied Blocks": 0,
                      "Shared Written Blocks": 0,
                      "Local Written Blocks": 0,
                      "Plan Rows": 3109,
                      "Parallel Aware": false,
                      "Actual Rows": 51,
                      "Parent Relationship": "Outer",
                      "Total Cost": 8563.06,
                      "Rows Removed by Filter": 0
                    }
                  ],
                  "Shared Read Blocks": 266531,
                  "Temp Written Blocks": 0,
                  "Local Dirtied Blocks": 0,
                  "Local Hit Blocks": 0,
                  "Plan Width": 8,
                  "Actual Loops": 44742,
                  "Temp Read Blocks": 0,
                  "Local Read Blocks": 0,
                  "Startup Cost": 8570.83,
                  "Shared Dirtied Blocks": 0,
                  "Shared Written Blocks": 0,
                  "Local Written Blocks": 0,
                  "Plan Rows": 1,
                  "Parallel Aware": false,
                  "Actual Rows": 1,
                  "Parent Relationship": "SubPlan",
                  "Total Cost": 8570.84
                }
              ],
              "Shared Read Blocks": 267565,
              "Relation Name": "parties",
              "Temp Written Blocks": 0,
              "Local Dirtied Blocks": 0,
              "Local Hit Blocks": 0,
              "Plan Width": 24,
              "Actual Loops": 1,
              "Alias": "parties",
              "Temp Read Blocks": 0,
              "Local Read Blocks": 0,
              "Startup Cost": 0,
              "Shared Dirtied Blocks": 0,
              "Shared Written Blocks": 0,
              "Local Written Blocks": 0,
              "Plan Rows": 44742,
              "Parallel Aware": false,
              "Actual Rows": 44742,
              "Parent Relationship": "Outer",
              "Total Cost": 383478215.31
            }
          ],
          "Shared Read Blocks": 267565,
          "Local Hit Blocks": 0,
          "Local Dirtied Blocks": 0,
          "Sort Method": "top-N heapsort",
          "Plan Width": 24,
          "Actual Loops": 1,
          "Temp Read Blocks": 0,
          "Local Read Blocks": 0,
          "Shared Written Blocks": 0,
          "Startup Cost": 383480149.03,
          "Shared Dirtied Blocks": 0,
          "Sort Space Type": "Memory",
          "Local Written Blocks": 0,
          "Plan Rows": 44742,
          "Parallel Aware": false,
          "Actual Rows": 200,
          "Parent Relationship": "Outer",
          "Total Cost": 383480260.89
        }
      ],
      "Local Written Blocks": 0,
      "Node Type": "Limit",
      "Plan Rows": 200,
      "Temp Read Blocks": 0,
      "Shared Dirtied Blocks": 0,
      "Local Hit Blocks": 0,
      "Parallel Aware": false,
      "Actual Rows": 200,
      "Local Dirtied Blocks": 0,
      "Temp Written Blocks": 0,
      "Plan Width": 24,
      "Actual Loops": 1,
      "Local Read Blocks": 0,
      "Total Cost": 383480149.53
    },
    "Triggers": []
  }
]

【问题讨论】:

    标签: postgresql indexing full-text-search


    【解决方案1】:

    新索引可以改变优化器的估计和选择的执行计划,新的计划可能(偶然地)比旧的更差。

    如果没有EXPLAIN (ANALYZE, BUFFERS) 两个查询的输出,您只能这么说。

    【讨论】:

    • 谢谢,我会编辑问题并添加计划+执行时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2014-02-19
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多