[MDEV-620] LP:731069 - Needless look-ups for NULL keys in equi-joins Created: 2011-03-08  Updated: 2015-12-02

Status: Confirmed
Project: MariaDB Server
Component/s: None
Affects Version/s: 5.3.12, 5.5.36, 10.0.10, 5.5, 10.0, 10.1
Fix Version/s: 10.1

Type: Bug Priority: Trivial
Reporter: Igor Babaev Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: Launchpad, upstream

Attachments: XML File LPexportBug731069.xml    

 Description   

If the join condition is of the form <t2.key>=<t1.no_key>, then the server performs no index look-ups for the values of t1,no_key that are NULLS when joining tables t1 and t2.
However if the condition is of the form <t2.key>=<expression>(<t1.no_key>) the look-ups for NULL keys are
performed.

This can be seen from the following example:

CREATE TABLE t1 (a int) ;
INSERT INTO t1 VALUES (NULL), (1), (NULL), (NULL), (2);
CREATE TABLE t2 (a int, INDEX idx(a)) ;
INSERT INTO t2 VALUES (7), (4), (1), (NULL), (5), (2), (1), (NULL), (9);

MariaDB [test]> EXPLAIN SELECT * FROM t1,t2 WHERE t2.a=t1.a;
+----+-------------+-------+------+---------------+------+---------+-----------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref       | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+-----------+------+-------------+
|  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL      |    5 | Using where |
|  1 | SIMPLE      | t2    | ref  | idx           | idx  | 5       | test.t1.a |    2 | Using index |
+----+-------------+-------+------+---------------+------+---------+-----------+------+-------------+
2 rows in set (0.00 sec)

MariaDB [test]> FLUSH STATUS;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [test]> SELECT * FROM t1,t2 WHERE t2.a=t1.a;
+------+------+
| a    | a    |
+------+------+
|    1 |    1 |
|    1 |    1 |
|    2 |    2 |
+------+------+
3 rows in set (0.00 sec)
 
MariaDB [test]> SHOW STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 0     |
| Handler_read_key      | 2     |
| Handler_read_next     | 3     |
| Handler_read_prev     | 0     |
| Handler_read_rnd      | 0     |
| Handler_read_rnd_next | 6     |
+-----------------------+-------+
6 rows in set (0.00 sec)

MariaDB [test]> EXPLAIN SELECT * FROM t1,t2 WHERE t2.a=t1.a+0;
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                    |
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------+
|  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL |    5 |                          |
|  1 | SIMPLE      | t2    | ref  | idx           | idx  | 5       | func |    2 | Using where; Using index |
+----+-------------+-------+------+---------------+------+---------+------+------+--------------------------+
2 rows in set (0.00 sec)

MariaDB [test]> FLUSH STATUS;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [test]> SELECT * FROM t1,t2 WHERE t2.a=t1.a+0;
+------+------+
| a    | a    |
+------+------+
|    1 |    1 |
|    1 |    1 |
|    2 |    2 |
+------+------+
3 rows in set (0.00 sec)
 
MariaDB [test]> SHOW STATUS LIKE 'Handler_read%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 0     |
| Handler_read_key      | 5     |
| Handler_read_next     | 9     |
| Handler_read_prev     | 0     |
| Handler_read_rnd      | 0     |
| Handler_read_rnd_next | 6     |
+-----------------------+-------+
6 rows in set (0.00 sec)



 Comments   
Comment by Igor Babaev [ 2011-03-08 ]

Re: Needless look-ups for NULL keys in equi-joins
This is an optimization problem. That's why I set the importance to 'Low'.

Comment by Rasmus Johansson (Inactive) [ 2011-11-11 ]

Launchpad bug id: 731069

Comment by Elena Stepanova [ 2014-04-13 ]

Also reproducible on MySQL 5.6.17, 5.7.4.

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