使用union

QUERY: explain extended select id from states where country_id < 100 union (select id from countries where id > 10)
                            TRACE: {
  "steps": [
    {
      "join_preparation": {
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `states`.`id` AS `id` from `states` where (`states`.`country_id` < 100)"
          }
        ]
      }
    },
    {
      "join_preparation": {
        "select#": 2,
        "steps": [
          {
            "expanded_query": "/* select#2 */ select `countries`.`id` AS `id` from `countries` where (`countries`.`id` > 10)"
          }
        ]
      }
    },
    {
      "creating_tmp_table": {
        "tmp_table_info": {
          "table": "intermediate_tmp_table",
          "row_length": 5,
          "key_length": 4,
          "unique_constraint": false,
          "location": "memory (heap)",
          "row_limit_estimate": 3355443
        }
      }
    },
    {
      "join_optimization": {
        "select#": 1,
        "steps": [
          {
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "(`states`.`country_id` < 100)",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "(`states`.`country_id` < 100)"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "(`states`.`country_id` < 100)"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "(`states`.`country_id` < 100)"
                }
              ]
            }
          },
          {
            "table_dependencies": [
              {
                "table": "`states`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "ref_optimizer_key_uses": [
            ]
          },
          {
            "rows_estimation": [
              {
                "table": "`states`",
                "table_scan": {
                  "rows": 4120,
                  "cost": 12
                }
              }
            ]
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "`states`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "scan",
                      "rows": 4120,
                      "cost": 836,
                      "chosen": true
                    }
                  ]
                },
                "cost_for_plan": 836,
                "rows_for_plan": 4120,
                "chosen": true
              }
            ]
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "(`states`.`country_id` < 100)",
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "`states`",
                  "attached": "(`states`.`country_id` < 100)"
                }
              ]
            }
          },
          {
            "refine_plan": [
              {
                "table": "`states`",
                "access_type": "table_scan"
              }
            ]
          }
        ]
      }
    },
    {
      "join_optimization": {
        "select#": 2,
        "steps": [
          {
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "(`countries`.`id` > 10)",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "(`countries`.`id` > 10)"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "(`countries`.`id` > 10)"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "(`countries`.`id` > 10)"
                }
              ]
            }
          },
          {
            "table_dependencies": [
              {
                "table": "`countries`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "ref_optimizer_key_uses": [
            ]
          },
          {
            "rows_estimation": [
              {
                "table": "`countries`",
                "range_analysis": {
                  "table_scan": {
                    "rows": 246,
                    "cost": 52.3
                  },
                  "potential_range_indices": [
                    {
                      "index": "PRIMARY",
                      "usable": true,
                      "key_parts": [
                        "id"
                      ]
                    }
                  ],
                  "best_covering_index_scan": {
                    "index": "PRIMARY",
                    "cost": 50.439,
                    "chosen": true
                  },
                  "setup_range_conditions": [
                  ],
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_group_by_or_distinct"
                  },
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "PRIMARY",
                        "ranges": [
                          "10 < id"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": true,
                        "using_mrr": false,
                        "index_only": true,
                        "rows": 236,
                        "cost": 48.419,
                        "chosen": true
                      }
                    ],
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    }
                  },
                  "chosen_range_access_summary": {
                    "range_access_plan": {
                      "type": "range_scan",
                      "index": "PRIMARY",
                      "rows": 236,
                      "ranges": [
                        "10 < id"
                      ]
                    },
                    "rows_for_plan": 236,
                    "cost_for_plan": 48.419,
                    "chosen": true
                  }
                }
              }
            ]
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "`countries`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "range",
                      "rows": 236,
                      "cost": 95.619,
                      "chosen": true
                    }
                  ]
                },
                "cost_for_plan": 95.619,
                "rows_for_plan": 236,
                "chosen": true
              }
            ]
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "(`countries`.`id` > 10)",
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "`countries`",
                  "attached": "(`countries`.`id` > 10)"
                }
              ]
            }
          },
          {
            "refine_plan": [
              {
                "table": "`countries`",
                "access_type": "range"
              }
            ]
          }
        ]
      }
    },
    {
      "join_explain": {
        "select#": 1,
        "steps": [
        ]
      }
    },
    {
      "join_explain": {
        "select#": 2,
        "steps": [
        ]
      }
    },
    {
      "join_preparation": {
        "select#": "fake",
        "steps": [
          {
            "expanded_query": "/* select#fake */ select `id` AS `id` from dual"
          }
        ]
      }
    },
    {
      "join_optimization": {
        "select#": "fake",
        "steps": [
          {
            "table_dependencies": [
              {
                "table": "``.``",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "rows_estimation": [
              {
                "table": "``.``",
                "table_scan": {
                  "rows": 0,
                  "cost": 10
                }
              }
            ]
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "``.``",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "scan",
                      "rows": 0,
                      "cost": 10,
                      "chosen": true
                    }
                  ]
                },
                "cost_for_plan": 10,
                "rows_for_plan": 0,
                "chosen": true
              }
            ]
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": null,
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "``.``",
                  "attached": null
                }
              ]
            }
          },
          {
            "refine_plan": [
              {
                "table": "``.``",
                "access_type": "table_scan"
              }
            ]
          }
        ]
      }
    },
    {
      "join_explain": {
        "select#": "fake",
        "steps": [
        ]
      }
    }
  ]
}

 

posted @ 2018-08-27 19:28  友哥  阅读(207)  评论(0编辑  收藏  举报