(partial plans)类比当前最好的完整计划。 完成搜索一条最佳QEP的关键路线见sql/sql_select.cc,find_best()。它执行了所有可能计划的详尽搜索,从而保证它最终将找到一个最佳的一条。
如下我们描述find_best()方法的伪代码。这是递归的,所以一些输入变量被标记了,以表明到目前为止,他们从前一个的迭代来的。
remaining_tables = {t1, ..., tn}; /* all tables referenced in a query */
procedure find_best( partial_plan in, /* in, partial plan of tables-joined-so-far */ partial_plan_cost, /* in, cost of partial_plan */ remaining_tables, /* in, set of tables not referenced in partial_plan */ best_plan_so_far, /* in/out, best plan found so far */ best_plan_so_far_cost)/* in/out, cost of best_plan_so_far */ { for each table T from remaining_tables { /* Calculate the cost of using table T. Factors that the optimizer takes into account may include: Many rows in table (bad) Many key parts in common with tables so far (very good) Restriction mentioned in the WHERE clause (good) Long key (good) Unique or primary key (good) Full-text key (bad) Other factors that may at some time be worth considering: Many columns in key Short average/maximum key length Small table file F |