函数功能:
指定的表的次序(循环条件中表示不同表位置的值递增)求解单表s与局部最优查询树的最优连接。
代码分析:
static void
optimize_straight_join(JOIN *join, table_map join_tables)
{
……
for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)//遍历remaining_tables中的每个表s
{
/* Find the best access method from 's' to the current partial plan */
best_access_path(join, s, join->thd, join_tables, idx,
record_count, read_time);
//通过调用best_access_path函数得到局部查询计划的最优花费保存到“join->positions[idx].records_read”和“join->positions[idx].read_time”,然后计算当前最优花费(best_access_path函数体中的最后,为这2个变量赋值)
/* compute the cost of the new plan extended with 's' */
record_count*= join->positions[idx].records_read;
read_time+= join->positions[idx].read_time;
join_tables&= ~(s->table->map);
++idx; //保障下次调用best_access_path函数时,从本次循环使用的表s的下一个表的位置正确
}
read_time+= record_count / (double) TIME_FOR_COMPARE;
……
join->best_read= read_time; //得到本次连接的最优花费
}