使用duplicateweedout

mysql> explain extended select * from cities where id in (select phonecode from  countries  group by name);
+----+-------------+-----------+--------+---------------+---------+---------+--------------------------+------+----------+-----------------+
| id | select_type | table     | type   | possible_keys | key     | key_len | ref                      | rows | filtered | Extra           |
+----+-------------+-----------+--------+---------------+---------+---------+--------------------------+------+----------+-----------------+
|  1 | SIMPLE      | countries | ALL    | NULL          | NULL    | NULL    | NULL                     |  246 |   100.00 | Start temporary |
|  1 | SIMPLE      | cities    | eq_ref | PRIMARY       | PRIMARY | 4       | test.countries.phonecode |    1 |   100.00 | End temporary   |
+----+-------------+-----------+--------+---------------+---------+---------+--------------------------+------+----------+-----------------+
2 rows in set, 1 warning (5.65 sec)
QUERY: explain extended select * from cities where id in (select phonecode from  countries  group by name)
                            TRACE: {
  "steps": [
    {
      "join_preparation": {
        "select#": 1,
        "steps": [
          {
            "join_preparation": {
              "select#": 2,
              "steps": [
                {
                  "transformations_to_subquery": [
                    "removed_grouping"
                  ]
                },
                {
                  "expanded_query": "/* select#2 */ select `countries`.`phonecode` from `countries`"
                },
                {
                  "transformation": {
                    "select#": 2,
                    "from": "IN (SELECT)",
                    "to": "semijoin",
                    "chosen": true
                  }
                }
              ]
            }
          },
          {
            "expanded_query": "/* select#1 */ select `cities`.`id` AS `id`,`cities`.`name` AS `name`,`cities`.`state_id` AS `state_id` from `cities` where `cities`.`id` in (/* select#2 */ select `countries`.`phonecode` from `countries`)"
          }
        ]
      }
    },
    {
      "join_optimization": {
        "select#": 1,
        "steps": [
          {
            "transformation": {
              "select#": 2,
              "from": "IN (SELECT)",
              "to": "semijoin",
              "chosen": true,
              "evaluating_constant_semijoin_conditions": [
              ]
            }
          },
          {
            "transformations_to_nested_joins": {
              "transformations": [
                "semijoin"
              ],
              "expanded_query": "/* select#1 */ select `cities`.`id` AS `id`,`cities`.`name` AS `name`,`cities`.`state_id` AS `state_id` from `cities` semi join (`countries`) where (1 and (`cities`.`id` = `countries`.`phonecode`))"
            }
          },
          {
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "(1 and (`cities`.`id` = `countries`.`phonecode`))",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "(1 and multiple equal(`cities`.`id`, `countries`.`phonecode`))"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "(1 and multiple equal(`cities`.`id`, `countries`.`phonecode`))"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "multiple equal(`cities`.`id`, `countries`.`phonecode`)"
                }
              ]
            }
          },
          {
            "table_dependencies": [
              {
                "table": "`cities`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              },
              {
                "table": "`countries`",
                "row_may_be_null": false,
                "map_bit": 1,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "ref_optimizer_key_uses": [
              {
                "table": "`cities`",
                "field": "id",
                "equals": "`countries`.`phonecode`",
                "null_rejecting": false
              }
            ]
          },
          {
            "pulled_out_semijoin_tables": [
            ]
          },
          {
            "rows_estimation": [
              {
                "table": "`cities`",
                "table_scan": {
                  "rows": 47478,
                  "cost": 161
                }
              },
              {
                "table": "`countries`",
                "table_scan": {
                  "rows": 246,
                  "cost": 1
                }
              }
            ]
          },
          {
            "execution_plan_for_potential_materialization": {
              "steps": [
                {
                  "considered_execution_plans": [
                    {
                      "plan_prefix": [
                      ],
                      "table": "`countries`",
                      "best_access_path": {
                        "considered_access_paths": [
                          {
                            "access_type": "scan",
                            "rows": 246,
                            "cost": 50.2,
                            "chosen": true
                          }
                        ]
                      },
                      "cost_for_plan": 50.2,
                      "rows_for_plan": 246,
                      "chosen": true
                    }
                  ]
                }
              ]
            }
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "`countries`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "scan",
                      "rows": 246,
                      "cost": 50.2,
                      "chosen": true
                    }
                  ]
                },
                "cost_for_plan": 50.2,
                "rows_for_plan": 246,
                "semijoin_strategy_choice": [
                  {
                    "strategy": "MaterializeScan",
                    "choice": "deferred"
                  }
                ],
                "rest_of_plan": [
                  {
                    "plan_prefix": [
                      "`countries`"
                    ],
                    "table": "`cities`",
                    "best_access_path": {
                      "considered_access_paths": [
                        {
                          "access_type": "ref",
                          "index": "PRIMARY",
                          "rows": 1,
                          "cost": 246.2,
                          "chosen": true
                        },
                        {
                          "access_type": "scan",
                          "using_join_cache": true,
                          "rows": 35609,
                          "cost": 1.75e6,
                          "chosen": false
                        }
                      ]
                    },
                    "cost_for_plan": 345.4,
                    "rows_for_plan": 246,
                    "semijoin_strategy_choice": [
                      {
                        "strategy": "MaterializeScan",
                        "recalculate_access_paths_and_cost": {
                          "tables": [
                            {
                              "table": "`cities`",
                              "best_access_path": {
                                "considered_access_paths": [
                                  {
                                    "access_type": "ref",
                                    "index": "PRIMARY",
                                    "rows": 1,
                                    "cost": 246.2,
                                    "chosen": true
                                  },
                                  {
                                    "access_type": "scan",
                                    "using_join_cache": true,
                                    "rows": 35609,
                                    "cost": 1.75e6,
                                    "chosen": false
                                  }
                                ]
                              }
                            }
                          ]
                        },
                        "cost": 445.8,
                        "rows": 1,
                        "duplicate_tables_left": true,
                        "chosen": true
                      },
                      {
                        "strategy": "DuplicatesWeedout",
                        "cost": 396.8,
                        "rows": 1,
                        "duplicate_tables_left": false,
                        "chosen": true
                      }
                    ],
                    "chosen": true
                  }
                ]
              },
              {
                "plan_prefix": [
                ],
                "table": "`cities`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "ref",
                      "index": "PRIMARY",
                      "usable": false,
                      "chosen": false
                    },
                    {
                      "access_type": "scan",
                      "rows": 47478,
                      "cost": 9656.6,
                      "chosen": true
                    }
                  ]
                },
                "cost_for_plan": 9656.6,
                "rows_for_plan": 47478,
                "semijoin_strategy_choice": [
                ],
                "pruned_by_cost": true
              },
              {
                "final_semijoin_strategy": "DuplicateWeedout"
              }
            ]
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "(`cities`.`id` = `countries`.`phonecode`)",
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "`countries`",
                  "attached": null
                },
                {
                  "table": "`cities`",
                  "attached": null
                }
              ]
            }
          },
          {
            "refine_plan": [
              {
                "creating_tmp_table": {
                  "tmp_table_info": {
                    "row_length": 6,
                    "key_length": 4,
                    "unique_constraint": false,
                    "location": "memory (heap)",
                    "row_limit_estimate": 2796202
                  }
                }
              },
              {
                "table": "`countries`",
                "access_type": "table_scan"
              },
              {
                "table": "`cities`"
              }
            ]
          }
        ]
      }
    },
    {
      "join_explain": {
        "select#": 1,
        "steps": [
        ]
      }
    }
  ]
}

 

转载于:https://www.cnblogs.com/youge-OneSQL/p/9544238.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值