Bug #60417 | slow VIEWS by using subselect as column | ||
---|---|---|---|
Submitted: | 10 Mar 2011 15:28 | Modified: | 27 Mar 2015 2:54 |
Reporter: | Richard Teubel | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Views | Severity: | S4 (Feature request) |
Version: | 5.1.51, 5.5.8 | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | FUNCTION, slow, subselect, VIEW |
[10 Mar 2011 15:28]
Richard Teubel
[10 Mar 2011 17:30]
Valeriy Kravchuk
I think the following EXPLAIN results: mysql> explain SELECT ID FROM `ViewWithSubselect`; +----+-------------+------------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+------+---------------+------+---------+------+------+-------+ | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 5 | | | 2 | DERIVED | mytable | ALL | NULL | NULL | NULL | NULL | 5 | | | 3 | SUBQUERY | mytable2 | ALL | NULL | NULL | NULL | NULL | 1 | | +----+-------------+------------+------+---------------+------+---------+------+------+-------+ 3 rows in set (5.04 sec) mysql> explain SELECT ID FROM `ViewWithoutSubselect`; +----+-------------+---------+-------+---------------+-------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------+-------+---------------+-------+---------+------+------+-------------+ | 1 | SIMPLE | mytable | index | NULL | Group | 5 | NULL | 5 | Using index | +----+-------------+---------+-------+---------------+-------+---------+------+------+-------------+ 1 row in set (0.00 sec) explain the situation. In case of subselect optimizer has to "materialize" the result of select (derived table), and call slow function every time in the process. Without subselect optimizer just access ID from the base table using index and, thus, does not call slow function. Time to execute EXPLAIN shows that this materialization actually happens at optimization stage, not at query execution stage. Optimizer has to use TEMPTABLE algorithm for this view, and this is described in the manual actually (see http://dev.mysql.com/doc/refman/5.5/en/view-algorithms.html): "If the MERGE algorithm cannot be used, a temporary table must be used instead. MERGE cannot be used if the view contains any of the following constructs: - Aggregate functions (SUM(), MIN(), MAX(), COUNT(), and so forth) ... - Subquery in the select list ..." So I think that this is a feature request to process view as MERGE in this case, but not a bug formally.
[11 Mar 2011 12:08]
Richard Teubel
Thanks for your answer. I change the severity to feature request.
[11 Mar 2011 12:43]
Valeriy Kravchuk
So, we have a valid feature request here for processing views differently (as MERGE) in case when subselect-based column from the view is NOT really accessed.
[27 Mar 2015 2:54]
Paul DuBois
Noted in 5.7.7, 5.8.0 changelogs. Previously, derived tables (subqueries) or views in the FROM clause could not be merged into the outer query if they contained subqueries of their own. Instead, they were processed using materialization. This restriction has now been lifted so that queries previously executed using materialization can be executed more quickly using merging.
[12 Dec 2015 22:21]
Paul DuBois
Revised changelog entry: Previously, derived tables (subqueries) or views in the FROM clause could not be merged into the outer query if they contained subqueries in the SELECT list. Instead, they were processed using materialization. This restriction has now been lifted so that queries previously executed using materialization can be executed more quickly using merging.