使用覆盖索引优化查询 [英] Optimization of query using covering indices

查看:133
本文介绍了使用覆盖索引优化查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following query with a subquery and self join:

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;

我为此定义了以下索引:

I have the following indices defined on this:

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

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

When I look at the query using EXPLAIN, the subquery using the covering index idx_bucket is definitely optimized, but the self join and where clause are not. Furthermore, why does it report that only patient_sid and attribute_id are used for used_key_parts while an attachment_condition is shown for lft, rgt (what does this mean?). Both lft and 'rgt` are just defined as integers with no special properties, so why aren't they being used in my covering index?

当我定义时更奇怪

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

只有patient_sid注册在used_key_parts.中,而且filtered11.00%跌至1.60%

only patient_sid is registered in used_key_parts. Furthermore filtered drops to 1.60% from 11.00%!

{
  "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:

Here's your basic 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'

这篇关于使用覆盖索引优化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆