Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-37020

Derived table merge optimization does not work for delete and update

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.6, 10.11, 11.4, 11.8, 10.5(EOL), 12.0(EOL), 12.1
    • None
    • Optimizer
    • None

    Description

      In the example below table merge optimization is not used for delete with derived table but with view it is used:

      --source include/have_sequence.inc
       
      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_10;
      create table t3 (a int, b int); insert into t3 select seq, seq from seq_1_to_10;
       
       
      explain delete t1.* from t1, (select t2.b as b1, t3.b as b2 from t2, t3 where t3.a=t2.a) DT where t1.b=DT.b1  and b2<1;
      set optimizer_switch='derived_with_keys=off';
      explain select * from t1, (select t2.b as b1 from t2, t3 where t3.a=t2.a) DT where t1.b+1=1+DT.b1;
      explain delete t1.* from t1, (select t2.b as b1 from t2, t3 where t3.a=t2.a) DT where t1.b+1=1+DT.b1;
       
      create view DT as select t2.b as b1 from t2, t3 where t3.a=t2.a;
      explain delete t1.* from t1, DT where t1.b+1=1+DT.b1;
       
      drop view DT;
      drop table t1,t2,t3;
      

      explain delete t1.* from t1, (select t2.b as b1, t3.b as b2 from t2, t3 where t3.a=t2.a) DT where t1.b=DT.b1  and b2<1;
      id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
      1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	10	Using where
      1	PRIMARY	<derived2>	ref	key0	key0	5	test.t1.b	10	Using where
      2	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	10	
      2	DERIVED	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; Using join buffer (flat, BNL join)
      

      explain select * from t1, (select t2.b as b1 from t2, t3 where t3.a=t2.a) DT where t1.b+1=1+DT.b1;
      id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
      1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	10	
      1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	10	Using where; Using join buffer (flat, BNL join)
      1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; Using join buffer (incremental, BNL join)
      

      explain delete t1.* from t1, (select t2.b as b1 from t2, t3 where t3.a=t2.a) DT where t1.b+1=1+DT.b1;
      id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
      1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	10	
      1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	100	Using where
      2	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	10	
      2	DERIVED	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; Using join buffer (flat, BNL join)
      

      explain delete t1.* from t1, DT where t1.b+1=1+DT.b1;
      id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
      1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	10	
      1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	10	Using where
      1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	10	Using where
      

      In TABLE_LIST::init_derived() there is a code (added in MDEV-28965) that disabled derived table merge optimization:

      bool forced_no_merge_for_update_delete=
                 belong_to_view ? belong_to_view->updating :
                                 !unit->outer_select()->outer_select();
      

       
      (gdb) print forced_no_merge_for_update_delete
        $28 = true
      

      But in earlier versions derived table merge optimization does not work too.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              lstartseva Lena Startseva
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.