[MDEV-16214] Incorrect plan taken by the optimizer , uses INDEX instead of ref access with ORDER BY Created: 2018-05-18  Updated: 2019-05-20  Resolved: 2019-05-20

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0, 10.1, 10.2, 10.3
Fix Version/s: 10.3.16, 10.4.6

Type: Bug Priority: Major
Reporter: Varun Gupta (Inactive) Assignee: Varun Gupta (Inactive)
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-8306 Complete cost-based optimization for ... Stalled
relates to MDEV-15777 Use inferred IS NOT NULL predicates i... Closed

 Description   

Create the test dataset

--source include/have_innodb.inc
create table t1(a int) engine=innodb;
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
create table t2(
  id int primary key,
  key1 int,
  col1 int,
  key(key1)
) engine=innodb;
 
insert into t2
  select
    A.a + B.a*10 + C.a*100 + D.a* 1000,
    A.a + 10*B.a,
    123456
from t1 A, t1 B, t1 C, t1 D;
 
alter table t2 add key2 int;
update t2 set key2=key1;
alter table t2 add key(key2);
analyze table t2;
flush tables;

Then run the query

explain select
   (SELECT
      concat(id, '-', key1, '-', col1)
    FROM t2
    WHERE t2.key1 = t1.a
    ORDER BY t2.key2 ASC LIMIT 1)
from
  t1;

The query plan is

+------+--------------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
| id   | select_type        | table | type | possible_keys | key  | key_len | ref       | rows | Extra                       |
+------+--------------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
|    1 | PRIMARY            | t1    | ALL  | NULL          | NULL | NULL    | NULL      |   10 |                             |
|    2 | DEPENDENT SUBQUERY | t2    | ref  | key1          | key1 | 5       | test.t1.a |   49 | Using where; Using filesort |
+------+--------------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+

Now I run the query

explain select
     (SELECT
        concat(id, '-', key1, '-', col1)
     FROM t2
      WHERE t2.key1 = t1.a and t2.key1 is NOT NULL
     ORDER BY t2.key2 ASC LIMIT 1)
from t1;

+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+
| id   | select_type        | table | type  | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+
|    1 | PRIMARY            | t1    | ALL   | NULL          | NULL | NULL    | NULL |   10 |             |
|    2 | DEPENDENT SUBQUERY | t2    | index | key1          | key2 | 5       | NULL |    2 | Using where |
+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+

As I see none of the elements for key1 is NULL, so the second query should behave exactly as the first one but it doesn't.



 Comments   
Comment by Varun Gupta (Inactive) [ 2018-05-18 ]

After discussing this with psergey ,
if ref(const) is used in that case the estimate provided by quick_keys is more precise than the estimate from rec_per_key

So here when we add the t2.key1 IS NOT NULL condition and make a ref access on
t2.key1 we don't have ref(const) here.
If we had a condition like t2.key1 = const and t2.key1 IS NOT NULL , then the estimate provided by quick_keys should be used

Apart from ref(const) , we consider estimate from quick keys if ref is using the same key
parts as quick select does

Comment by Varun Gupta (Inactive) [ 2018-06-11 ]

Patch
http://lists.askmonty.org/pipermail/commits/2018-June/012603.html

Comment by Sergei Petrunia [ 2019-05-16 ]

Taking over as we discussed the issue in connection with another bug (MDEV-15777).

The patch is ok to push, but please replace the loop with a single comparison like tab->ref.const_ref_part_map == make_prev_keypart_map(tab->ref.key_parts).

Comment by Sergei Petrunia [ 2019-05-16 ]

Ok to push

Comment by Varun Gupta (Inactive) [ 2019-05-17 ]

Patch
http://lists.askmonty.org/pipermail/commits/2019-May/013780.html

Comment by Sergei Petrunia [ 2019-05-19 ]

https://lists.launchpad.net/maria-developers/msg11834.html

Comment by Sergei Petrunia [ 2019-05-19 ]

Ok to push after the review input is addressed.

Generated at Thu Feb 08 08:27:13 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.