• unknown's avatar
    Fix LP BUG#715069 · 43acceeb
    unknown authored
    Analysis:
    The wrong result is a consquence of sorting the subquery
    result and then selecting only the first row due to the
    artificial LIMIT 1 introduced by the fix_fields phase.
    Normally, if there is an ORDER BY in a subquery, the ORDER
    is removed (Item_in_subselect::select_in_like_transformer),
    however if a GROUP BY is transformed into ORDER, this happens
    later, after the removal of the ORDER clause of subqueries, so
    we end up with a subquery with an ORDER clause, and an artificially
    added LIMIT 1.
    
    The reason why the same works in the main 5.3 without MWL#89, is
    that the 5.3 performs all subquery transformations, including
    IN->EXISTS before JOIN::optimize(). The beginning of JOIN::optimize
    does:
      if (having || (select_options & OPTION_FOUND_ROWS))
        select_limit= HA_POS_ERROR;
    which sets the limit back to infinity, thus 5.3 sorts the whole
    subquery result, and IN performs the lookup into all subquery result
    rows.
    
    Solution:
    Sorting of subqueries without LIMIT is meaningless. Since LIMIT in
    subqueries is not supported, the patch removes sorting by setting
      join->skip_sort_order= true
    for each subquery JOIN object. This improves a number of execution
    plans to not perform unnecessary sorting at all.
    43acceeb
explain.result 13.5 KB