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

Union of uncorrelated subqueries becomes dependant

    XMLWordPrintable

Details

    Description

      This query:

      select * from a where id in (select id from a)

      correctly materializes the subquery before running the outer query. However, this query:

      select * from a where id in (select id from a union all select id from a)

      Does not. EXPLAIN shows the UNION and the subqueries as DEPENDANT, which is wrong as they are obviously uncorrelated.

      The fix for this is to use derived tables:

      select * from a where id in (select * from (select id from a union all select id from a) dq)

      We have 2 problems here:
      1) Obviously the union is causing the subqueries to become dependant and it in itself is dependant
      2) Another problem is that without the union the select is using the index, with the union it is using where instead - a serious performance killer as well.

      Question is, is the derived tables fix the only workaround? And is this a bug or a limitation in the optimizer?

      Note: the a table is a simple table with only id primary key column. I have a real-life case where this problem is causing some really bad performance, and I created this case just to illustrate the problem.

      Attachments

        Activity

          People

            psergei Sergei Petrunia
            qiui Piotr Blasiak
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

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