<?xml version="1.0" encoding="utf-8"?>
<launchpad-bug id="718703">
  <date_last_updated>2011-07-08 08:47:41.884958+00:00</date_last_updated>
  <api_links>
    <bug_api_link>https://api.launchpad.net/1.0/bugs/718703</bug_api_link>
    <bug_owner_link>https://api.launchpad.net/1.0/~pstoev-askmonty</bug_owner_link>
    <milestone_link>https://api.launchpad.net/1.0/maria/+milestone/5.3</milestone_link>
    <linked_branches_collection_link>https://api.launchpad.net/1.0/bugs/718703/linked_branches</linked_branches_collection_link>
    <activity_link>https://api.launchpad.net/1.0/bugs/718703/activity</activity_link>
  </api_links>
  <bug_web_link>https://bugs.launchpad.net/bugs/718703</bug_web_link>
  <owner>Philip Stoev</owner>
  <assignee>Timour Katchaounov</assignee>
  <milestone_title>Maria 5.3</milestone_title>
  <duplicate_link></duplicate_link>
  <duplicate_bug_id></duplicate_bug_id>
  <title>EXPLAIN evaluates subqueries when inside VIEWs</title>
  <status>Invalid</status>
  <importance>Wishlist</importance>
  <created>2011-02-14 12:32:54.924123+00:00</created>
  <description>
<![CDATA[If a subquery is present in a view, EXPLAIN evaluates the query and takes as much time as executing the query itself. Executing EXPLAIN directly on the query from the view definition is instantaneous.

MariaDB [test]> EXPLAIN /*!50100 PARTITIONS */ SELECT * FROM transforms.view_1706_merge;                                                                      +----+--------------------+------------+------------+--------+-------------------------+-----------------+---------+--------------------+------+-------------------------------------------------+
| id | select_type        | table      | partitions | type   | possible_keys           | key             | key_len | ref                | rows | Extra                                           |
+----+--------------------+------------+------------+--------+-------------------------+-----------------+---------+--------------------+------+-------------------------------------------------+
|  1 | PRIMARY            | <derived2> | NULL       | ALL    | NULL                    | NULL            | NULL    | NULL               |  160 |                                                 |
|  2 | DERIVED            | alias1     | NULL       | ALL    | PRIMARY,col_varchar_key | NULL            | NULL    | NULL               |  100 |                                                 |
|  2 | DERIVED            | alias2     | NULL       | ALL    | NULL                    | NULL            | NULL    | NULL               |  100 | Using where                                     |
|  3 | DEPENDENT SUBQUERY | SQ1_alias1 | NULL       | index  | NULL                    | col_varchar_key | 9       | NULL               |   20 | Using index                                     |
|  3 | DEPENDENT SUBQUERY | SQ1_alias2 | NULL       | ALL    | col_varchar_key         | NULL            | NULL    | NULL               |   20 | Range checked for each record (index map: 0x20) |
|  3 | DEPENDENT SUBQUERY | SQ1_alias3 | NULL       | eq_ref | PRIMARY,col_int_key     | PRIMARY         | 4       | test.SQ1_alias2.pk |    1 | Using index condition; Using where              |
+----+--------------------+------------+------------+--------+-------------------------+-----------------+---------+--------------------+------+-------------------------------------------------+
6 rows in set (28.86 sec)

backtrace during EXPLAIN which shows that handler functions are being called:

#0  0x08749bea in _checkchunk (irem=0xb6cca220, filename=0x88f3990 "ha_myisam.cc", lineno=776) at safemalloc.c:488
#1  0x08749e08 in _sanity (filename=0x88f3990 "ha_myisam.cc", lineno=776) at safemalloc.c:533
#2  0x0874944d in _myfree (ptr=0xa75c3398, filename=0x88f3990 "ha_myisam.cc", lineno=776, myflags=0) at safemalloc.c:279
#3  0x084a948a in ha_myisam::open (this=0x90c44f10, name=0xb6cb7920 "./test/C", mode=39, test_if_locked=2) at ha_myisam.cc:776
#4  0x083cf02e in handler::ha_open (this=0x90c44f10, table_arg=0xb4d8758, name=0xb6cb7920 "./test/C", mode=39, test_if_locked=2) at handler.cc:2108
#5  0x083cedbf in handler::clone (this=0xb534698, mem_root=0xb455ba8) at handler.cc:2050
#6  0x084a8b93 in ha_myisam::clone (this=0xb534698, mem_root=0xb455ba8) at ha_myisam.cc:557
#7  0x084959cd in DsMrr_impl::setup_two_handlers (this=0xb534a0c) at multi_range_read.cc:947
#8  0x08495678 in DsMrr_impl::dsmrr_init (this=0xb534a0c, h_arg=0xb534698, seq_funcs=0x915dd7c0, seq_init_param=0xa7567d40, n_ranges=1, mode=4,
    buf=0x91000030) at multi_range_read.cc:871
#9  0x084ad9ee in ha_myisam::multi_range_read_init (this=0xb534698, seq=0x915dd7c0, seq_init_param=0xa7567d40, n_ranges=1, mode=4, buf=0x91000030)
    at ha_myisam.cc:2224
#10 0x083b2e0b in QUICK_RANGE_SELECT::reset (this=0xa7567d40) at opt_range.cc:10500
#11 0x08325f86 in join_init_read_record (tab=0xa759ad68) at sql_select.cc:14289
#12 0x08325e28 in join_init_quick_read_record (tab=0xa759ad68) at sql_select.cc:14258
#13 0x08324500 in sub_select (join=0xa75ca2d8, join_tab=0xa759ad68, end_of_records=false) at sql_select.cc:13516
#14 0x08324bcf in evaluate_join_record (join=0xa75ca2d8, join_tab=0xa759ab90, error=0) at sql_select.cc:13714
#15 0x083246a9 in sub_select (join=0xa75ca2d8, join_tab=0xa759ab90, end_of_records=false) at sql_select.cc:13559
#16 0x0832398b in do_select (join=0xa75ca2d8, fields=0xa75c8274, table=0x0, procedure=0x0) at sql_select.cc:13063
#17 0x0830b3cb in JOIN::exec (this=0xa75ca2d8) at sql_select.cc:2450
#18 0x0823d1d0 in subselect_single_select_engine::exec (this=0xa7598a58) at item_subselect.cc:2751
#19 0x08237917 in Item_subselect::exec (this=0xa7598988) at item_subselect.cc:550
#20 0x08239385 in Item_exists_subselect::val_bool (this=0xa7598988) at item_subselect.cc:1248
#21 0x081fee87 in Item_func_not::val_int (this=0xa7598a80) at item_cmpfunc.cc:287
#22 0x081c0962 in Item::val_bool (this=0xa7598a80) at item.cc:187
#23 0x0820aa83 in Item_cond_or::val_int (this=0xa75bf4c0) at item_cmpfunc.cc:4689
#24 0x08242486 in Item_func_trig_cond::val_int (this=0xa75bf6b0) at item_cmpfunc.h:449
#25 0x081c0962 in Item::val_bool (this=0xa75bf6b0) at item.cc:187
#26 0x0820a98b in Item_cond_and::val_int (this=0xa75bf7c0) at item_cmpfunc.cc:4671
#27 0x083248f7 in evaluate_join_record (join=0xa75b8d98, join_tab=0xa75bee70, error=0) at sql_select.cc:13614
#28 0x083246a9 in sub_select (join=0xa75b8d98, join_tab=0xa75bee70, end_of_records=false) at sql_select.cc:13559
#29 0x08324bcf in evaluate_join_record (join=0xa75b8d98, join_tab=0xa75bec98, error=0) at sql_select.cc:13714
#30 0x083246a9 in sub_select (join=0xa75b8d98, join_tab=0xa75bec98, end_of_records=false) at sql_select.cc:13559
#31 0x0832398b in do_select (join=0xa75b8d98, fields=0xa758c0b4, table=0x0, procedure=0x0) at sql_select.cc:13063
#32 0x0830b3cb in JOIN::exec (this=0xa75b8d98) at sql_select.cc:2450
#33 0x0830bb91 in mysql_select (thd=0xb453bf8, rref_pointer_array=0xa758c124, tables=0xa75c79a0, wild_num=0, fields=..., conds=0xa7598f98, og_num=0,
    order=0x0, group=0x0, having=0xa7599a20, proc_param=0x0, select_options=2416200192, result=0xa7599c00, unit=0xa758bd84, select_lex=0xa758c020)
    at sql_select.cc:2667
#34 0x084499c8 in mysql_derived_filling (thd=0xb453bf8, lex=0xb455298, orig_table_list=0xa758b0f0) at sql_derived.cc:295
#35 0x08449303 in mysql_handle_derived (lex=0xb455298, processor=0x84497e5 <mysql_derived_filling(THD*, LEX*, TABLE_LIST*)>) at sql_derived.cc:56
#36 0x082ed03e in open_and_lock_tables_derived (thd=0xb453bf8, tables=0xa758b0f0, derived=true) at sql_base.cc:5117
#37 0x082a8ac7 in open_and_lock_tables (thd=0xb453bf8, tables=0xa758b0f0) at mysql_priv.h:1631
#38 0x082a1e98 in execute_sqlcom_select (thd=0xb453bf8, all_tables=0xa758b0f0) at sql_parse.cc:5036
#39 0x082990bb in mysql_execute_command (thd=0xb453bf8) at sql_parse.cc:2234
#40 0x082a4644 in mysql_parse (thd=0xb453bf8, rawbuf=0xa758af28 "EXPLAIN /*!50100 PARTITIONS */ SELECT * FROM transforms.view_1706_merge", length=71,]]>  </description>
  <activities>
    <activity datechanged="2011-02-14T12:32:54.924123+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[]]>      </newvalue>
      <whatchanged>bug</whatchanged>
      <person>Philip Stoev</person>
      <message>added bug</message>
    </activity>
    <activity datechanged="2011-02-14T12:40:17.044289+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[database dump https://bugs.launchpad.net/maria/+bug/718703/+attachment/1849148/+files/bug718703.dump.zip]]>      </newvalue>
      <whatchanged>attachment added</whatchanged>
      <person>Philip Stoev</person>
      <message></message>
    </activity>
    <activity datechanged="2011-02-14T12:40:23.809859+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[5.3]]>      </newvalue>
      <whatchanged>maria: milestone</whatchanged>
      <person>Philip Stoev</person>
      <message></message>
    </activity>
    <activity datechanged="2011-02-14T12:40:30.544721+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[Timour Katchaounov (timour)]]>      </newvalue>
      <whatchanged>maria: assignee</whatchanged>
      <person>Philip Stoev</person>
      <message></message>
    </activity>
    <activity datechanged="2011-02-14T12:44:57.083862+00:00">
      <oldvalue>
<![CDATA[]]>      </oldvalue>
      <newvalue>
<![CDATA[lp:~maria-captains/maria/5.3-mwl89]]>      </newvalue>
      <whatchanged>branch linked</whatchanged>
      <person>Philip Stoev</person>
      <message></message>
    </activity>
    <activity datechanged="2011-02-14T15:42:00.301800+00:00">
      <oldvalue>
<![CDATA[Undecided]]>      </oldvalue>
      <newvalue>
<![CDATA[Low]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-03-02T11:12:05.768305+00:00">
      <oldvalue>
<![CDATA[Low]]>      </oldvalue>
      <newvalue>
<![CDATA[Wishlist]]>      </newvalue>
      <whatchanged>maria: importance</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-03-08T14:42:57.600615+00:00">
      <oldvalue>
<![CDATA[New]]>      </oldvalue>
      <newvalue>
<![CDATA[Triaged]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
    <activity datechanged="2011-07-08T08:47:41.373312+00:00">
      <oldvalue>
<![CDATA[Triaged]]>      </oldvalue>
      <newvalue>
<![CDATA[Invalid]]>      </newvalue>
      <whatchanged>maria: status</whatchanged>
      <person>Timour Katchaounov</person>
      <message></message>
    </activity>
  </activities>
  <comments>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/718703/comments/1" datecreated="2011-02-14T12:40:17.044289+00:00">
      <person>Philip Stoev</person>
      <subject>
<![CDATA[Re: EXPLAIN evaluates subqueries when inside VIEWs]]>      </subject>
      <content>
<![CDATA[To reproduce, load the attached dump and then run EXPLAIN SELECT * FROM transforms.view_1706_merge;

Automatic simplification is not available for bugs like this one.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/718703/comments/2" datecreated="2011-05-06T21:00:03.627401+00:00">
      <person>Timour Katchaounov</person>
      <subject>
<![CDATA[Re: EXPLAIN evaluates subqueries when inside VIEWs]]>      </subject>
      <content>
<![CDATA[This bug is related to processing of derived tables, which will be changed
substantially by MWL#106 (http://askmonty.org/worklog/Server-Sprint/?tid=106).

The bug should be analyzed after MWL#106 is merged with MWL#89.]]>      </content>
    </comment>
    <comment commentlink="https://api.launchpad.net/1.0/maria/+bug/718703/comments/3" datecreated="2011-07-08T08:47:26.323536+00:00">
      <person>Timour Katchaounov</person>
      <subject>
<![CDATA[Re: EXPLAIN evaluates subqueries when inside VIEWs]]>      </subject>
      <content>
<![CDATA[As expected, the bug is no longer reproducible after MWL#106
was merged into 5.3, because the core of this task was to avoid
evaluation of derived tables during optimization.
]]>      </content>
    </comment>
  </comments>
  <branches>
    <branch>lp:~maria-captains/maria/5.3-mwl89</branch>
  </branches>
  <messages>
    <message created="2011-02-14 12:40:17.044289+00:00" owner="Philip Stoev">
<![CDATA[To reproduce, load the attached dump and then run EXPLAIN SELECT * FROM transforms.view_1706_merge;

Automatic simplification is not available for bugs like this one.]]>      <attachment link="https://bugs.launchpad.net/bugs/718703/+attachment/1849148" type="Unspecified">
        <title>database dump</title>
        <file>LPexportBug718703_bug718703.dump.zip</file>
      </attachment>
    </message>
  </messages>
</launchpad-bug>
