使用覆盖索引优化查询



我有以下带有子查询和自联接的查询:

SELECT bucket.patient_sid AS sid
FROM 
(SELECT clinical_data.patient_sid, 
        clinical_data.lft, 
        clinical_data.rgt
FROM clinical_data INNER JOIN 
(SELECT clinical_data.patient_sid, 
        clinical_data.lft, 
        clinical_data.rgt, 
        clinical_data.attribute_id 
FROM clinical_data 
WHERE clinical_data.attribute_id = '33' AND clinical_data.string_value = '2160-0') AS attribute 
ON clinical_data.patient_sid = attribute.patient_sid 
    AND clinical_data.lft >= attribute.lft 
    AND clinical_data.rgt <= attribute.rgt 
WHERE clinical_data.attribute_id = '36') AS bucket;

我在此定义了以下索引:

KEY `idx_bucket` (`attribute_id`,`string_value`)
KEY `idx_self_join` (`patient_sid`,`attribute_id`,`lft`,`rgt`)

当我查看使用EXPLAIN的查询时,使用覆盖索引idx_bucket的子查询肯定得到了优化,但自联接和where子句没有得到优化。此外,为什么它报告used_key_parts仅使用patient_sidattribute_id,而lftrgt显示attachment_condition(这意味着什么?)。lft和"rgt"都只是被定义为没有特殊属性的整数,那么为什么它们不在我的覆盖索引中使用呢?

更奇怪的是,当我定义时

KEY `idx_self_join` (`patient_sid`,`lft`,`rgt`,`attribute_id`) 

used_key_parts.中仅注册了patient_sid。此外,filtered11.00%下降到1.60%

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "645186.71"
    },
    "nested_loop": [
      {
        "table": {
          "table_name": "clinical_data",
          "access_type": "ref",
          "possible_keys": [
            "fk_attribute_idx",
            "idx_value_string",
            "idx_value_double",
            "idx_bucket",
            "idx_self_join_idx"
          ],
          "key": "idx_bucket",
          "used_key_parts": [
            "attribute_id",
            "string_value"
          ],
          "key_length": "308",
          "ref": [
            "const",
            "const"
          ],
          "rows_examined_per_scan": 126402,
          "rows_produced_per_join": 126402,
          "filtered": "100.00",
          "cost_info": {
            "read_cost": "126402.00",
            "eval_cost": "25280.40",
            "prefix_cost": "151682.40",
            "data_read_per_join": "46M"
          },
          "used_columns": [
            "patient_sid",
            "string_value",
            "attribute_id",
            "lft",
            "rgt"
          ],
          "attached_condition": "(`ns_large2`.`clinical_data`.`patient_sid` is not null)"
        }
      },
      {
        "table": {
          "table_name": "clinical_data",
          "access_type": "ref",
          "possible_keys": [
            "fk_attribute_idx",
            "idx_value_string",
            "idx_value_double",
            "idx_bucket",
            "idx_self_join_idx"
          ],
          "key": "idx_self_join_idx",
          "used_key_parts": [
            "attribute_id",
            "patient_sid"
          ],
          "key_length": "10",
          "ref": [
            "const",
            "ns_large2.clinical_data.patient_sid"
          ],
          "rows_examined_per_scan": 14,
          "rows_produced_per_join": 201169,
          "filtered": "11.11",
          "using_index": true,
          "cost_info": {
            "read_cost": "131327.39",
            "eval_cost": "40233.83",
            "prefix_cost": "645186.71",
            "data_read_per_join": "73M"
          },
          "used_columns": [
            "patient_sid",
            "attribute_id",
            "lft",
            "rgt"
          ],
          "attached_condition": "((`ns_large2`.`clinical_data`.`lft` >= `ns_large2`.`clinical_data`.`lft`) and (`ns_large2`.`clinical_data`.`rgt` <= `ns_large2`.`clinical_data`.`rgt`))"
        }
      }
    ]
  }
}

以下是您的基本JOIN:

SELECT
FROM clinical_data cd1
JOIN clinical_data cd2
    ON cd1.patient_sid = cd2.patient_sid
    AND cd2.attribute_id = '33'
WHERE cd1.attribute_id = '36'

以下是我最终想到的:

SELECT
    cd1.patient_sid as sid
FROM clinical_data cd1
JOIN clinical_data cd2
    ON cd1.patient_sid = cd2.patient_sid
    AND cd1.lft >= cd2.lft 
    AND cd1.rgt <= cd2.rgt 
WHERE cd1.attribute_id = '36'
    AND cd2.attribute_id = '33'
    AND cd2.string_value = '2160-0'

"Used_columns"表示它"覆盖"。最后的"已使用的关键部分"并不是全部用作"关键",因为它们是在"范围"中需要的,而不是"="。

摆脱外部查询:

        SELECT  clinical_data.patient_sid, clinical_data.lft, clinical_data.rgt
            FROM  clinical_data
            INNER JOIN  
              ( SELECT  clinical_data.patient_sid, clinical_data.lft, clinical_data.rgt,
                        clinical_data.attribute_id
                    FROM  clinical_data
                    WHERE  clinical_data.attribute_id = '33'
                      AND  clinical_data.string_value = '2160-0'
              ) AS attribute  ON clinical_data.patient_sid = attribute.patient_sid
              AND  clinical_data.lft >= attribute.lft
              AND  clinical_data.rgt <= attribute.rgt
            WHERE  clinical_data.attribute_id = '36'

抱歉,lft-rgt模式不是很有效。

相关内容

  • 没有找到相关文章

最新更新