[MDEV-18949] Analyze show ref(const,const) when we are using index scan Created: 2019-03-16  Updated: 2023-11-10

Status: Stalled
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.1, 10.2, 10.3, 10.4
Fix Version/s: 10.4

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


 Description   

The dataset used

create table t0 (a int);
INSERT INTO t0 VALUES (0),(0),(0),(0),(2),(0),(0),(1),(1),(0);
 
CREATE TABLE t1 (
a int(11) DEFAULT NULL,
b int(11) DEFAULT NULL,
d int(11) DEFAULT NULL,
KEY a_d (a,d),
KEY a_b (a,b)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into t1 select A.a , B.a, C.a from t0 A, t0 B, t0 C, t0 D;

MariaDB [test]> analyze select a,b,d from t1 where a=1 and d=2 order by b limit 20000;
+------+-------------+-------+-------+---------------+------+---------+-------------+-------+----------+----------+------------+-------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref         | rows  | r_rows   | filtered | r_filtered | Extra       |
+------+-------------+-------+-------+---------------+------+---------+-------------+-------+----------+----------+------------+-------------+
|    1 | SIMPLE      | t1    | index | a_d,a_b       | a_b | 10      | const,const | 10266 | 10000.00 |     1.95 |       2.00 | Using where |
+------+-------------+-------+-------+---------------+------+---------+-------------+-------+----------+----------+------------+-------------+

ref should be NULL in the ouput as we are using index scan , ref is set when we use ref-access



 Comments   
Comment by Varun Gupta (Inactive) [ 2020-06-18 ]

Another test case

CREATE TABLE t1 (a INT, b INT, KEY (a), KEY (b));
INSERT INTO t1 SELECT seq%2, seq FROM seq_1_to_100;

MariaDB [test]> EXPLAIN SELECT a,b FROM t1 WHERE a=1 ORDER BY b LIMIT 5;
+------+-------------+-------+-------+---------------+------+---------+------+------+-------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+-------------+-------+-------+---------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | t1    | index | a             | b    | 5       | NULL |    7 | Using where |
+------+-------------+-------+-------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
 
MariaDB [test]> ANALYZE SELECT a,b FROM t1 WHERE a=1 ORDER BY b LIMIT 5;
+------+-------------+-------+-------+---------------+------+---------+-------+------+--------+----------+------------+-------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref   | rows | r_rows | filtered | r_filtered | Extra       |
+------+-------------+-------+-------+---------------+------+---------+-------+------+--------+----------+------------+-------------+
|    1 | SIMPLE      | t1    | index | a             | b    | 5       | const |  100 |   9.00 |    69.00 |      55.56 | Using where |
+------+-------------+-------+-------+---------------+------+---------+-------+------+--------+----------+------------+-------------+
1 row in set (0.00 sec)

So if you look at the rows column in ANALYZE output it should 100, which is incorrect.
Also ref shows const which is also incorrect.

This is happening for ANALYZE because we reset the ref keyparts to 0 only when the query is an EXPLAIN query.

Comment by Varun Gupta (Inactive) [ 2020-06-18 ]

Patch
https://github.com/MariaDB/server/commit/b56ed787b801817aa5a2a5a6d729786c5da0c9dc

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