mysql中cost对比测试

1.数据库表
mysql中cost对比测试_第1张图片
复制表
mysql中cost对比测试_第2张图片
2.执行select c_id from customer where c_discount = 0.35;后,explain对比
mysql中cost对比测试_第3张图片
3.optimizer_trace比较
原表

{
  "steps": [
    {
      "join_preparation": {
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `customer`.`c_id` AS `c_id` from `customer` where (`customer`.`c_discount` = 0.35)"
          }
        ]
      }
    },
    {
      "join_optimization": {
        "select#": 1,
        "steps": [
          {
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "(`customer`.`c_discount` = 0.35)",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "multiple equal(0.35, `customer`.`c_discount`)"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "multiple equal(0.35, `customer`.`c_discount`)"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "multiple equal(0.35, `customer`.`c_discount`)"
                }
              ]
            }
          },
          {
            "substitute_generated_columns": {
            }
          },
          {
            "table_dependencies": [
              {
                "table": "`customer`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "ref_optimizer_key_uses": [
            ]
          },
          {
            "rows_estimation": [
              {
                "table": "`customer`",
                "table_scan": {
                  "rows": 289371,
                  "cost": 11504
                }
              }
            ]
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "`customer`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "rows_to_scan": 289371,
                      "filtering_effect": [
                      ],
                      "final_filtering_effect": 0.1,
                      "access_type": "scan",
                      "resulting_rows": 28937,
                      "cost": 40441,
                      "chosen": true
                    }
                  ]
                },
                "condition_filtering_pct": 100,
                "rows_for_plan": 28937,
                "cost_for_plan": 40441,
                "chosen": true
              }
            ]
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "(`customer`.`c_discount` = 0.35)",
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "`customer`",
                  "attached": "(`customer`.`c_discount` = 0.35)"
                }
              ]
            }
          },
          {
            "refine_plan": [
              {
                "table": "`customer`"
              }
            ]
          }
        ]
      }
    },
    {
      "join_explain": {
        "select#": 1,
        "steps": [
        ]
      }
    }
  ]
}

复制表

{
  "steps": [
    {
      "join_preparation": {
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `customer_new`.`c_id` AS `c_id` from `customer_new` where (`customer_new`.`c_discount` = 0.35)"
          }
        ]
      }
    },
    {
      "join_optimization": {
        "select#": 1,
        "steps": [
          {
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "(`customer_new`.`c_discount` = 0.35)",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "multiple equal(0.35, `customer_new`.`c_discount`)"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "multiple equal(0.35, `customer_new`.`c_discount`)"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "multiple equal(0.35, `customer_new`.`c_discount`)"
                }
              ]
            }
          },
          {
            "substitute_generated_columns": {
            }
          },
          {
            "table_dependencies": [
              {
                "table": "`customer_new`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "ref_optimizer_key_uses": [
              {
                "table": "`customer_new`",
                "field": "c_discount",
                "equals": "0.35",
                "null_rejecting": false
              }
            ]
          },
          {
            "rows_estimation": [
              {
                "table": "`customer_new`",
                "range_analysis": {
                  "table_scan": {
                    "rows": 285901,
                    "cost": 41760
                  },
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY",
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "c_d_1",
                      "usable": true,
                      "key_parts": [
                        "c_discount",
                        "c_w_id",
                        "c_d_id",
                        "c_id"
                      ]
                    }
                  ],
                  "best_covering_index_scan": {
                    "index": "c_d_1",
                    "cost": 28939,
                    "chosen": true
                  },
                  "setup_range_conditions": [
                  ],
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_group_by_or_distinct"
                  },
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "c_d_1",
                        "ranges": [
                          "0.35 <= c_discount <= 0.35"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": true,
                        "using_mrr": false,
                        "index_only": true,
                        "rows": 5822,
                        "cost": 590.29,
                        "chosen": true
                      }
                    ],
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    }
                  },
                  "chosen_range_access_summary": {
                    "range_access_plan": {
                      "type": "range_scan",
                      "index": "c_d_1",
                      "rows": 5822,
                      "ranges": [
                        "0.35 <= c_discount <= 0.35"
                      ]
                    },
                    "rows_for_plan": 5822,
                    "cost_for_plan": 590.29,
                    "chosen": true
                  }
                }
              }
            ]
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "`customer_new`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "ref",
                      "index": "c_d_1",
                      "rows": 5822,
                      "cost": 590.28,
                      "chosen": true
                    },
                    {
                      "access_type": "range",
                      "range_details": {
                        "used_index": "c_d_1"
                      },
                      "chosen": false,
                      "cause": "heuristic_index_cheaper"
                    }
                  ]
                },
                "condition_filtering_pct": 100,
                "rows_for_plan": 5822,
                "cost_for_plan": 590.28,
                "chosen": true
              }
            ]
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "(`customer_new`.`c_discount` = 0.35)",
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "`customer_new`",
                  "attached": null
                }
              ]
            }
          },
          {
            "refine_plan": [
              {
                "table": "`customer_new`"
              }
            ]
          }
        ]
      }
    },
    {
      "join_explain": {
        "select#": 1,
        "steps": [
        ]
      }
    }
  ]
}

其中原表的cost
mysql中cost对比测试_第4张图片
带索引表中table_scan的值
table_scan

你可能感兴趣的:(mysql笔记)