Repository navigation
Conversation
|
Fails for embedded server builds: https://buildbot.mariadb.org/#/builders/719/builds/20922/steps/6/logs/stdio |
thd->change_item_tree(write_iter.ref(), expensive[i]);Do we really need this? As far as I know, AND/OR items at top level are transient. sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0;and will be restored for the next execution. |
2f17175 to
0c1b771
Compare
|
@spetrunia , agree on the unnecessary |
|
Note to self: The item is put into HAVING, which is not processed. If it was processed, would that be harmful? Suppose a part of EDIT: there is also |
spetrunia
left a comment
There was a problem hiding this comment.
Consider also this:
create table t1 ( a int , b int);
insert into t1 select seq, seq from seq_1_to_10;
create table t2 ( a int , b int);
insert into t2 select seq, seq from seq_1_to_100;
explain format=json
select * from t1
where t1.a >= (select max(t2.a) from t2 where t2.b=t1.b) and rand()<0.5;with the patch this gives:
"attached_condition": "rand() < 0.5 and t1.a >= (subquery#2)"
That is, the "coin toss" or "rand<0.5" is done before checking the subquery.
At first glance, this reordering looks like it might affect some users in a way that's not desirable. What do you think?
| is_expensive_processor() (a UDF, MATCH() and so on). | ||
| */ | ||
|
|
||
| static bool is_expensive_cond(Item *cond) |
There was a problem hiding this comment.
We need another name for this function to avoid confusion.
First proposal to get the ball rolling: is_cond_to_be_checked_last()
There was a problem hiding this comment.
I decided to inline it, so there is no confusion with the name any more.
| { | ||
| Item_func::Functype sp_func= Item_func::FUNC_SP; | ||
| return cond->with_subquery() || | ||
| cond->walk(&Item::is_expensive_processor, NULL, 0) || |
There was a problem hiding this comment.
Should this be
cond->walk(&Item::is_expensive_processor, NULL, 0)
or just
cond->is_expensive()
?
They seem to have some overlapping meaning. Are there any known reasons to prefer one over the other?
There was a problem hiding this comment.
Agree, cond->is_expensive() covers almost everything besides some corner cases that can be probably ignored.
| Item_func::Functype sp_func= Item_func::FUNC_SP; | ||
| return cond->with_subquery() || | ||
| cond->walk(&Item::is_expensive_processor, NULL, 0) || | ||
| cond->walk(&Item::find_function_processor, &sp_func, 0); |
There was a problem hiding this comment.
I'm wondering if we should check for Item_func_get_user_var and Item_func_set_user_var also ...
f6ee5df to
f008d56
Compare
|
@spetrunia, I addressed your concern about RAND(): an AND/OR that has a non-deterministic argument (one with
A read of a variable that the statement also assigns has I also mentioned in the commit message that processing HAVING will need care with |
|
f008d56 (Evaluate cheap subconditions before expensive ones) causes a plan regression. A query that used index_merge for every outer row before the commit now does a full scan of the inner table for every outer row. The cause: ReproducerThe only plan change is in the last row: with the parent build it is 4 rows read (index_merge), with f008d56 it is 4006 (full scan).
EXPLAIN shows range-checked-for-each-record on t2 in both builds.
Numbers are Why it happensWhen an AND of ORs is itself ORed with something else, the range optimizer keeps only the first imerge. The reorder changes which imerge is first.
Only range analysis that runs after
The range analysis at optimize time runs on Suggested fixI suggest keeping an original-order copy of the condition for range re-analysis, the same way
Whichever way you go, the query above could be added to |
|
Also: cached constant subqueries count as expensive. (subq) still reports with_subquery(), so it gets moved to the end, as in derived_cond_pushdown.result:10553 and the trigcond(... or t4.d is null) cases in subselect4.result. After the first evaluation it's effectively a constant, so in an OR it would short-circuit cheaply if left first. This isn't harmful, but checking const_item() first would be more accurate. The outer_ref_cond call ignores errors on the spot. The call at the add_cond_and_fix(... outer_ref_cond) site doesn't check thd->is_error() itself. |
@mariadb-RexJohnston , thanks for the input. How to handle index_merge differences... In my opinion, we should either Rationale for B: the order of conditions in the WHERE is essentially random, there is no accepted practice that one should "write conditions that index merge should use first" (Note that one may argue that in some databases the "shape" of the WHERE, Rationale for A: In some cases, MariaDB optimizer runs the range analyzer multiple times and we expect to get the same output every time... This has shown to have exceptions in edge cases, but should we add to the list.. |
|
constant subqueries can still be expensive to compute... |
|
It's an interesting case with RCFER (range checked for every record). If I manually rewrite the query and disable I get Regarding the point about constant subqueries, I agree with @spetrunia that even a single computation may be expensive, and moving them to the tail may avoid that. And the short-circuited evaluation (getting the cached value) should be comparable to a simple condition check in terms of execution time. And regarding the missing error check - I agree, worth adding. |
f008d56 to
5cb80af
Compare
|
Is this optimization used for single-table UPDATE/DELETEs ? I didn't check but looking at the code I suppose not. Should we enable it there too? |
| if (move_expensive_args_last(thd, item)) | ||
| { | ||
| if (!expensive && | ||
| !(expensive= thd->alloc<Item *>(args->elements))) |
There was a problem hiding this comment.
To show that the storage is temporary, please switch to using my_alloca/ my_afree.
There was a problem hiding this comment.
Turns out to be not that trivial. Plain my_alloca doesn't have a size limit, so a generated query with 50K subconditions can allocate 50K * (pointer size 8 bytes) = 400K while the default stack size is 292K per thread.
The other option is my_safe_alloca which falls back to my_malloc for large allocations. However, for multiple recursions we are still not guaranteed from stack overflow, so check_stack_overrun guard would be needed. And a my_safe_afree call will be needed.
|
Let's try to not put more things into sql_select.cc, please move the new function move_expensive_args_last() into |
|
We can have this done unconditionally in the |
…E and ON conditions The arguments of AND and OR are evaluated left to right with short-circuiting, in the order written in the query. An expensive subcondition written first is therefore evaluated even for rows that a cheap one would reject (AND) or accept (OR). Reorder the arguments of each AND/OR so that the expensive ones come after the cheap ones, keeping the relative order within each group. The walk goes down into nested AND/OR and into trigger conditions. A subcondition is expensive if it contains a subquery, a stored function call, or an item that marks itself as expensive through is_expensive_processor() (a UDF, MATCH()). An AND/OR that has a non-deterministic argument (one with RAND_TABLE_BIT) keeps its order, to prevent unwanted change of behavior. Nested lists are still reordered. The reordering is applied to: - the conditions attached to tables (select_cond, select->cond, on_precond), at the end of make_join_select() and before the attached_conditions_summary trace, so the trace shows the final order; - BNL/BNLH scan filters, in make_scan_filter(). They are built from the original WHERE/ON, and check_join_cache_usage() may build them after make_join_select(); - outer_ref_cond and pseudo_bits_cond, the conditions a subquery checks once per execution before running its join. The steps that later build new conditions from these (index condition pushdown, removal of duplicate BNL conditions) take subsets and keep the order. Not covered yet: HAVING, exec_const_cond, terms added after make_join_select() (the index condition remainder rejected by an engine, HAVING moved to a table's condition). Reordering HAVING would need care with Item_is_not_null_test and Item_ref_null_helper, which record NULLs for IN->EXISTS.
5cb80af to
ae81dcc
Compare
|
It was decided to implement the feature in |
…E and ON conditions
The arguments of AND and OR are evaluated left to right with short-circuiting, in the order written in the query. An expensive subcondition written first is therefore evaluated even for rows that a cheap one would reject (AND) or accept (OR).
Reorder the arguments of each AND/OR so that the expensive ones come after the cheap ones, keeping the relative order within each group. The walk goes down into nested AND/OR and into trigger conditions. A subcondition is expensive if it contains a subquery, a stored function call, or an item that marks itself as expensive through is_expensive_processor() (a UDF, MATCH()).
The reordering is applied to:
Not covered yet: HAVING, exec_const_cond, terms added after make_join_select() (the index condition remainder rejected by an engine, HAVING moved to a table's condition), and SLEEP()/GET_LOCK(), which do not mark themselves as expensive through
is_expensive_processor().