当前位置:网站首页>Inner join execution plan changed

Inner join execution plan changed

2022-06-11 22:02:00 YoohuDeLi

One 、 background

Same connection query inner join, Because of a certain condition in There are several more values in the , The execution plan has changed .
Implementation plan 1 :
 Insert picture description here
Implementation plan II :
 Insert picture description here

Two 、 The difference between external connection and internal connection

The cost calculation formula of connection query is as follows: :
Total cost of connection query = The cost of a single access to a driver table + Drive table fan out number x The cost of a single access to the driven table .

  • For left ( Outside ) Connect and right ( Outside ) For connection queries , Their driver table is fixed , So to get the best query scheme, you only need : Select the lowest cost access method for the driven table and the driven table respectively .
  • For internal connections , The positions of the driven table and the driven table are interchangeable , So we need to consider two aspects :
    (1) The final query cost of different tables as driving tables may be different , That is, we need to consider the optimal table join order .
    (2) Then select the lowest cost access method for the driven table and the driven table respectively .

3、 ... and 、 adopt optimizer_trace View the decision process

1、optimizer_trace View and open
see :show variables like ‘optimizer_trace’;
Turn on :set optimizer_trace=“enabled=on”;

2、optimizer_trace Use
(1) open optimizer trace function ( It's off by default ):
SET optimizer_trace=“enabled=on”;
(2) Enter the query statement :
SELECT …;
(3) from OPTIMIZER_TRACE View the optimization process of the previous query in the table :
SELECT * FROM information_schema.OPTIMIZER_TRACE;

3、 case analysis
(1) Execute the optimization process of plan 1

{
  "steps": [
    {
      "join_preparation": { # prepare Stage 
        "select#": 1,
        "steps": [
          {
            "IN_uses_bisection": true
          },
          {
            "expanded_query": "/* select#1 */ select distinct `q`.`question_id` AS `question_id` from (`tb_question` `q` join `tb_question_label` `label` on((`label`.`question_id` = `q`.`question_id`))) where ((`label`.`subject_id` = 11) and (`label`.`is_deleted` = 1) and (`label`.`label_id` in (197750,197637,197947)) and (`q`.`subject_id` = 11) and (`q`.`parent_id` = 0) and (`q`.`is_deleted` = 0) and (`q`.`quality` = 4)) limit 3000"
          },
          {
            "transformations_to_nested_joins": {
              "transformations": [
                "JOIN_condition_to_WHERE",
                "parenthesis_removal"
              ],
              "expanded_query": "/* select#1 */ select distinct `q`.`question_id` AS `question_id` from `tb_question` `q` join `tb_question_label` `label` where ((`label`.`subject_id` = 11) and (`label`.`is_deleted` = 1) and (`label`.`label_id` in (197750,197637,197947)) and (`q`.`subject_id` = 11) and (`q`.`parent_id` = 0) and (`q`.`is_deleted` = 0) and (`q`.`quality` = 4) and (`label`.`question_id` = `q`.`question_id`)) limit 3000"
            }
          }
        ]
      }
    },
    {
      "join_optimization": { # optimize Stage 
        "select#": 1,
        "steps": [
          {
            "condition_processing": { #  Process search criteria 
              "condition": "WHERE",
              #  Original search criteria 
              "original_condition": "((`label`.`subject_id` = 11) and (`label`.`is_deleted` = 1) and (`label`.`label_id` in (197750,197637,197947)) and (`q`.`subject_id` = 11) and (`q`.`parent_id` = 0) and (`q`.`is_deleted` = 0) and (`q`.`quality` = 4) and (`label`.`question_id` = `q`.`question_id`))",
              "steps": [
                {
                  #  Equivalent transfer transformation 
                  "transformation": "equality_propagation",
                  "resulting_condition": "((`label`.`label_id` in (197750,197637,197947)) and multiple equal(11, `label`.`subject_id`) and multiple equal(1, `label`.`is_deleted`) and multiple equal(11, `q`.`subject_id`) and multiple equal(0, `q`.`parent_id`) and multiple equal(0, `q`.`is_deleted`) and multiple equal(4, `q`.`quality`) and multiple equal(`label`.`question_id`, `q`.`question_id`))"
                },
                {
                  #  Constant transfer conversion 
                  "transformation": "constant_propagation",
                  "resulting_condition": "((`label`.`label_id` in (197750,197637,197947)) and multiple equal(11, `label`.`subject_id`) and multiple equal(1, `label`.`is_deleted`) and multiple equal(11, `q`.`subject_id`) and multiple equal(0, `q`.`parent_id`) and multiple equal(0, `q`.`is_deleted`) and multiple equal(4, `q`.`quality`) and multiple equal(`label`.`question_id`, `q`.`question_id`))"
                },
                {
                  #  Remove useless conditions 
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "((`label`.`label_id` in (197750,197637,197947)) and multiple equal(11, `label`.`subject_id`) and multiple equal(1, `label`.`is_deleted`) and multiple equal(11, `q`.`subject_id`) and multiple equal(0, `q`.`parent_id`) and multiple equal(0, `q`.`is_deleted`) and multiple equal(4, `q`.`quality`) and multiple equal(`label`.`question_id`, `q`.`question_id`))"
                }
              ]
            }
          },
          {
            #  Replace virtual generated columns 
            "substitute_generated_columns": {
            }
          },
          {
            #  Table dependency information 
            "table_dependencies": [
              {
                "table": "`tb_question` `q`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              },
              {
                "table": "`tb_question_label` `label`",
                "row_may_be_null": false,
                "map_bit": 1,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "ref_optimizer_key_uses": [
              {
                "table": "`tb_question` `q`",
                "field": "question_id",
                "equals": "`label`.`question_id`",
                "null_rejecting": false
              },
              {
                "table": "`tb_question` `q`",
                "field": "parent_id",
                "equals": "0",
                "null_rejecting": false
              },
              {
                "table": "`tb_question_label` `label`",
                "field": "question_id",
                "equals": "`q`.`question_id`",
                "null_rejecting": false
              }
            ]
          },
          {
            #  Estimate the access cost of different single table access methods 
            "rows_estimation": [
              {
                "table": "`tb_question` `q`",
                "const_keys_added": {
                  "keys": [
                    "uk_question"
                  ],
                  "cause": "distinct"
                },
                "range_analysis": {
                  "table_scan": { #  Number of rows and cost of full table scanning 
                    "rows": 566210,
                    "cost": 118915
                  },
                  #  Analyze possible indexes 
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY", #  Primary key not available 
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "uk_question", # uk_question May be used 
                      "usable": true,
                      "key_parts": [
                        "question_id"
                      ]
                    },
                    {
                      "index": "idx_parent", # idx_parent May be used 
                      "usable": true,
                      "key_parts": [
                        "parent_id",
                        "id"
                      ]
                    },
                    {
                      "index": "idx_adapt_from", # idx_adapt_from Cannot be used 
                      "usable": false,
                      "cause": "not_applicable"
                    }
                  ],
                  "setup_range_conditions": [
                  ],
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_single_table"
                  },
                  #  Analyze the cost of various possible indexes 
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        #  Use idx_parent Cost analysis of 
                        "index": "idx_parent",
                        "ranges": [
                          "0 <= parent_id <= 0"
                        ],
                        "index_dives_for_eq_ranges": true, #  Whether to use index dive
                        "rowid_ordered": true, #  Whether the records obtained by this index are sorted by primary key 
                        "using_mrr": false, #  Whether to use mrr
                        "index_only": false, #  Whether it is index override access 
                        "rows": 283105, #  Number of records obtained using this index 
                        "cost": 339727, #  The cost of using this index 
                        "chosen": false, #  Whether to select the index 
                        "cause": "cost"
                      }
                    ],
                    #  Analyze the cost of using index consolidation 
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    }
                  }
                }
              },
              {
                "table": "`tb_question_label` `label`",
                "range_analysis": {
                  "table_scan": {
                    "rows": 685400,
                    "cost": 140576
                  },
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "idx_label",
                      "usable": true,
                      "key_parts": [
                        "label_id",
                        "id"
                      ]
                    },
                    {
                      "index": "idx_question",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "idx_sub_question",
                      "usable": false,
                      "cause": "not_applicable"
                    }
                  ],
                  "setup_range_conditions": [
                  ],
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_single_table"
                  },
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "idx_label",
                        "ranges": [
                          "197637 <= label_id <= 197637",
                          "197750 <= label_id <= 197750",
                          "197947 <= label_id <= 197947"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": false,
                        "rows": 3,
                        "cost": 6.61,
                        "chosen": true
                      }
                    ],
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    }
                  },
                  #  For the above single table query tb_question_label The best access method 
                  "chosen_range_access_summary": {
                    "range_access_plan": {
                      "type": "range_scan",
                      "index": "idx_label",
                      "rows": 3,
                      "ranges": [
                        "197637 <= label_id <= 197637",
                        "197750 <= label_id <= 197750",
                        "197947 <= label_id <= 197947"
                      ]
                    },
                    "rows_for_plan": 3,
                    "cost_for_plan": 6.61,
                    "chosen": true
                  }
                }
              }
            ]
          },
          {
            #  Analyze various possible implementation plans ( There may be many different schemes for multi table query )
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "`tb_question_label` `label`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "ref",
                      "index": "idx_question",
                      "usable": false,
                      "chosen": false
                    },
                    {
                      "rows_to_scan": 3,
                      "access_type": "range",
                      "range_details": {
                        "used_index": "idx_label"
                      },
                      "resulting_rows": 0.05,
                      "cost": 7.21,
                      "chosen": true
                    }
                  ]
                },
                "condition_filtering_pct": 100,
                "rows_for_plan": 0.05,
                "cost_for_plan": 7.21,
                "rest_of_plan": [
                  {
                    "plan_prefix": [
                      "`tb_question_label` `label`"
                    ],
                    "table": "`tb_question` `q`",
                    "best_access_path": {
                      "considered_access_paths": [
                        {
                          "access_type": "eq_ref",
                          "index": "uk_question",
                          "rows": 1,
                          "cost": 0.06,
                          "chosen": true
                        },
                        {
                          "access_type": "ref",
                          "index": "idx_parent",
                          "chosen": false,
                          "cause": "heuristic_eqref_already_found"
                        },
                        {
                          "access_type": "scan",
                          "cost": 118913,
                          "rows": 566210,
                          "chosen": false,
                          "cause": "cost"
                        }
                      ]
                    },
                    "condition_filtering_pct": 100,
                    "rows_for_plan": 0.05,
                    "cost_for_plan": 7.27,
                    "chosen": true
                  }
                ]
              },
              {
                "plan_prefix": [
                ],
                "table": "`tb_question` `q`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "ref",
                      "index": "uk_question",
                      "usable": false,
                      "chosen": false
                    },
                    {
                      "access_type": "ref",
                      "index": "idx_parent",
                      "rows": 283105,
                      "cost": 73634,
                      "chosen": true
                    },
                    {
                      "rows_to_scan": 566210,
                      "access_type": "scan",
                      "resulting_rows": 283.11,
                      "cost": 118913,
                      "chosen": false
                    }
                  ]
                },
                "condition_filtering_pct": 0.1,
                "rows_for_plan": 283.11,
                "cost_for_plan": 73634,
                "pruned_by_cost": true
              }
            ]
          },
          {
            #  Try adding some other query criteria to the query 
            "attaching_conditions_to_tables": {
              "original_condition": "((`q`.`question_id` = `label`.`question_id`) and (`q`.`quality` = 4) and (`q`.`is_deleted` = 0) and (`q`.`parent_id` = 0) and (`q`.`subject_id` = 11) and (`label`.`is_deleted` = 1) and (`label`.`subject_id` = 11) and (`label`.`label_id` in (197750,197637,197947)))",
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "`tb_question_label` `label`",
                  "attached": "((`label`.`is_deleted` = 1) and (`label`.`subject_id` = 11) and (`label`.`label_id` in (197750,197637,197947)))"
                },
                {
                  "table": "`tb_question` `q`",
                  "attached": "((`q`.`quality` = 4) and (`q`.`is_deleted` = 0) and (`q`.`parent_id` = 0) and (`q`.`subject_id` = 11))"
                }
              ]
            }
          },
          {
            #  Slightly improve the implementation plan 
            "refine_plan": [
              {
                "table": "`tb_question_label` `label`",
                "pushed_index_condition": "(`label`.`label_id` in (197750,197637,197947))",
                "table_condition_attached": "((`label`.`is_deleted` = 1) and (`label`.`subject_id` = 11))"
              },
              {
                "table": "`tb_question` `q`"
              }
            ]
          }
        ]
      }
    },
    {
      "join_execution": {
        "select#": 1,
        "steps": [
          {
            "creating_tmp_table": {
              "tmp_table_info": {
                "table": "intermediate_tmp_table",
                "row_length": 9,
                "key_length": 8,
                "unique_constraint": false,
                "location": "memory (heap)",
                "row_limit_estimate": 3000
              }
            }
          }
        ]
      }
    }
  ]
}

(2) Execute the execution process of plan 2

{
  "steps": [
    {
      "join_preparation": { # prepare Stage 
        "select#": 1,
        "steps": [
          {
            "IN_uses_bisection": true
          },
          {
            "expanded_query": "/* select#1 */ select distinct `q`.`question_id` AS `question_id` from (`tb_question` `q` join `tb_question_label` `label` on((`label`.`question_id` = `q`.`question_id`))) where ((`label`.`subject_id` = 11) and (`label`.`is_deleted` = 0) and (`label`.`label_id` in (197049,187007,187006)) and (`q`.`subject_id` = 11) and (`q`.`parent_id` = 0) and (`q`.`is_deleted` = 0) and (`q`.`quality` = 4)) limit 3000"
          },
          {
            "transformations_to_nested_joins": {
              "transformations": [
                "JOIN_condition_to_WHERE",
                "parenthesis_removal"
              ],
              "expanded_query": "/* select#1 */ select distinct `q`.`question_id` AS `question_id` from `tb_question` `q` join `tb_question_label` `label` where ((`label`.`subject_id` = 11) and (`label`.`is_deleted` = 0) and (`label`.`label_id` in (197049,187007,187006)) and (`q`.`subject_id` = 11) and (`q`.`parent_id` = 0) and (`q`.`is_deleted` = 0) and (`q`.`quality` = 4) and (`label`.`question_id` = `q`.`question_id`)) limit 3000"
            }
          }
        ]
      }
    },
    {
      "join_optimization": { # optimize Stage 
        "select#": 1,
        "steps": [
          {
            "condition_processing": { #  Process search criteria 
              "condition": "WHERE",
              #  Original search criteria 
              "original_condition": "((`label`.`subject_id` = 11) and (`label`.`is_deleted` = 0) and (`label`.`label_id` in (197049,187007,187006)) and (`q`.`subject_id` = 11) and (`q`.`parent_id` = 0) and (`q`.`is_deleted` = 0) and (`q`.`quality` = 4) and (`label`.`question_id` = `q`.`question_id`))",
              "steps": [
                {
                  #  Equivalent transfer transformation 
                  "transformation": "equality_propagation",
                  "resulting_condition": "((`label`.`label_id` in (197049,187007,187006)) and multiple equal(11, `label`.`subject_id`) and multiple equal(0, `label`.`is_deleted`) and multiple equal(11, `q`.`subject_id`) and multiple equal(0, `q`.`parent_id`) and multiple equal(0, `q`.`is_deleted`) and multiple equal(4, `q`.`quality`) and multiple equal(`label`.`question_id`, `q`.`question_id`))"
                },
                {
                  #  Constant transfer conversion 
                  "transformation": "constant_propagation",
                  "resulting_condition": "((`label`.`label_id` in (197049,187007,187006)) and multiple equal(11, `label`.`subject_id`) and multiple equal(0, `label`.`is_deleted`) and multiple equal(11, `q`.`subject_id`) and multiple equal(0, `q`.`parent_id`) and multiple equal(0, `q`.`is_deleted`) and multiple equal(4, `q`.`quality`) and multiple equal(`label`.`question_id`, `q`.`question_id`))"
                },
                {
                  #  Remove useless conditions 
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "((`label`.`label_id` in (197049,187007,187006)) and multiple equal(11, `label`.`subject_id`) and multiple equal(0, `label`.`is_deleted`) and multiple equal(11, `q`.`subject_id`) and multiple equal(0, `q`.`parent_id`) and multiple equal(0, `q`.`is_deleted`) and multiple equal(4, `q`.`quality`) and multiple equal(`label`.`question_id`, `q`.`question_id`))"
                }
              ]
            }
          },
          {
            "substitute_generated_columns": {
            }
          },
          {
          	#  Replace virtual generated columns 
            "table_dependencies": [
              {
                "table": "`tb_question` `q`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              },
              {
                "table": "`tb_question_label` `label`",
                "row_may_be_null": false,
                "map_bit": 1,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "ref_optimizer_key_uses": [
              {
                "table": "`tb_question` `q`",
                "field": "question_id",
                "equals": "`label`.`question_id`",
                "null_rejecting": false
              },
              {
                "table": "`tb_question` `q`",
                "field": "parent_id",
                "equals": "0",
                "null_rejecting": false
              },
              {
                "table": "`tb_question_label` `label`",
                "field": "question_id",
                "equals": "`q`.`question_id`",
                "null_rejecting": false
              }
            ]
          },
          {
          	#  Estimate the access cost of different single table access methods 
            "rows_estimation": [
              {
                "table": "`tb_question` `q`",
                "const_keys_added": {
                  "keys": [
                    "uk_question"
                  ],
                  "cause": "distinct"
                },
                "range_analysis": {
                  "table_scan": { #  Number of rows and cost of full table scanning 
                    "rows": 566210,
                    "cost": 118915
                  },
                  #  Analyze possible indexes 
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY", #  Primary key not available 
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "uk_question", # uk_question May be used 
                      "usable": true,
                      "key_parts": [
                        "question_id"
                      ]
                    },
                    {
                      "index": "idx_parent", # idx_parent May be used 
                      "usable": true,
                      "key_parts": [
                        "parent_id",
                        "id"
                      ]
                    },
                    {
                      "index": "idx_adapt_from", # idx_adapt_from Cannot be used 
                      "usable": false,
                      "cause": "not_applicable"
                    }
                  ],
                  "setup_range_conditions": [
                  ],
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_single_table"
                  },
                  #  Analyze the cost of various possible indexes 
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                      	#  Use idx_parent Cost analysis of 
                        "index": "idx_parent",
                        "ranges": [
                          "0 <= parent_id <= 0"
                        ],
                        "index_dives_for_eq_ranges": true, #  Whether to use index dive
                        "rowid_ordered": true, #  Whether the records obtained by this index are sorted by primary key 
                        "using_mrr": false, #  Whether to use mrr
                        "index_only": false, #  Whether it is index override access 
                        "rows": 283105, #  Number of records obtained using this index 
                        "cost": 339727, #  The cost of using this index 
                        "chosen": false, #  Whether to select the index 
                        "cause": "cost"
                      }
                    ],
                    #  Analyze the cost of using index consolidation 
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    }
                  }
                }
              },
              {
                "table": "`tb_question_label` `label`",
                "range_analysis": {
                  "table_scan": {
                    "rows": 685400,
                    "cost": 140576
                  },
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "idx_label",
                      "usable": true,
                      "key_parts": [
                        "label_id",
                        "id"
                      ]
                    },
                    {
                      "index": "idx_question",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "idx_sub_question",
                      "usable": false,
                      "cause": "not_applicable"
                    }
                  ],
                  "setup_range_conditions": [
                  ],
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_single_table"
                  },
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "idx_label",
                        "ranges": [
                          "187006 <= label_id <= 187006",
                          "187007 <= label_id <= 187007",
                          "197049 <= label_id <= 197049"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": false,
                        "using_mrr": false,
                        "index_only": false,
                        "rows": 53010,
                        "cost": 63615,
                        "chosen": true
                      }
                    ],
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    }
                  },
                  #  For the above single table query tb_question_label The best access method 
                  "chosen_range_access_summary": {
                    "range_access_plan": {
                      "type": "range_scan",
                      "index": "idx_label",
                      "rows": 53010,
                      "ranges": [
                        "187006 <= label_id <= 187006",
                        "187007 <= label_id <= 187007",
                        "197049 <= label_id <= 197049"
                      ]
                    },
                    "rows_for_plan": 53010,
                    "cost_for_plan": 63615,
                    "chosen": true
                  }
                }
              }
            ]
          },
          {
          	#  Analyze various possible implementation plans ( There may be many different schemes for multi table query )
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "`tb_question_label` `label`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "ref",
                      "index": "idx_question",
                      "usable": false,
                      "chosen": false
                    },
                    {
                      "rows_to_scan": 53010,
                      "access_type": "range",
                      "range_details": {
                        "used_index": "idx_label"
                      },
                      "resulting_rows": 530.1,
                      "cost": 74217,
                      "chosen": true
                    }
                  ]
                },
                "condition_filtering_pct": 100,
                "rows_for_plan": 530.1,
                "cost_for_plan": 74217,
                "rest_of_plan": [
                  {
                    "plan_prefix": [
                      "`tb_question_label` `label`"
                    ],
                    "table": "`tb_question` `q`",
                    "best_access_path": {
                      "considered_access_paths": [
                        {
                          "access_type": "eq_ref",
                          "index": "uk_question",
                          "rows": 1,
                          "cost": 636.12,
                          "chosen": true
                        },
                        {
                          "access_type": "ref",
                          "index": "idx_parent",
                          "chosen": false,
                          "cause": "heuristic_eqref_already_found"
                        },
                        {
                          "access_type": "scan",
                          "cost": 118913,
                          "rows": 566210,
                          "chosen": false,
                          "cause": "cost"
                        }
                      ]
                    },
                    "condition_filtering_pct": 5,
                    "rows_for_plan": 26.505,
                    "cost_for_plan": 74853,
                    "chosen": true
                  }
                ]
              },
              {
                "plan_prefix": [
                ],
                "table": "`tb_question` `q`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "ref",
                      "index": "uk_question",
                      "usable": false,
                      "chosen": false
                    },
                    {
                      "access_type": "ref",
                      "index": "idx_parent",
                      "rows": 283105,
                      "cost": 73634,
                      "chosen": true
                    },
                    {
                      "rows_to_scan": 566210,
                      "access_type": "scan",
                      "resulting_rows": 283.11,
                      "cost": 118913,
                      "chosen": false
                    }
                  ]
                },
                "condition_filtering_pct": 0.1,
                "rows_for_plan": 283.11,
                "cost_for_plan": 73634,
                "rest_of_plan": [
                  {
                    "plan_prefix": [
                      "`tb_question` `q`"
                    ],
                    "table": "`tb_question_label` `label`",
                    "best_access_path": {
                      "considered_access_paths": [
                        {
                          "access_type": "ref",
                          "index": "idx_question",
                          "rows": 1.4988,
                          "cost": 509.18,
                          "chosen": true
                        },
                        {
                          "access_type": "range",
                          "range_details": {
                            "used_index": "idx_label"
                          },
                          "cost": 74217,
                          "rows": 53010,
                          "chosen": false,
                          "cause": "cost"
                        }
                      ]
                    },
                    "condition_filtering_pct": 3.336,
                    "rows_for_plan": 14.155,
                    "cost_for_plan": 74143,
                    "chosen": true
                  }
                ]
              }
            ]
          },
          {
          	#  Try adding some other query criteria to the query 
            "attaching_conditions_to_tables": {
              "original_condition": "((`label`.`question_id` = `q`.`question_id`) and (`q`.`quality` = 4) and (`q`.`is_deleted` = 0) and (`q`.`parent_id` = 0) and (`q`.`subject_id` = 11) and (`label`.`is_deleted` = 0) and (`label`.`subject_id` = 11) and (`label`.`label_id` in (197049,187007,187006)))",
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "`tb_question` `q`",
                  "attached": "((`q`.`quality` = 4) and (`q`.`is_deleted` = 0) and (`q`.`subject_id` = 11))"
                },
                {
                  "table": "`tb_question_label` `label`",
                  "attached": "((`label`.`is_deleted` = 0) and (`label`.`subject_id` = 11) and (`label`.`label_id` in (197049,187007,187006)))"
                }
              ]
            }
          },
          {
          	#  Slightly improve the implementation plan 
            "refine_plan": [
              {
                "table": "`tb_question` `q`"
              },
              {
                "table": "`tb_question_label` `label`"
              }
            ]
          }
        ]
      }
    },
    {
      "join_explain
(trace Too long , The following text bytes are ignored )

(3) summary
adopt (1)、(2) Of optimizer_trace analysis , You can see , Change of execution plan , It is based on the cost of connection query .

原网站

版权声明
本文为[YoohuDeLi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112143164023.html