mysql version: 5.6.10-enterprise-commercial-advanced
Create Table: CREATE TABLE `t_1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`num` bigint(20) unsigned NOT NULL,
`start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`end_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`comment` binary(204) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`,`num`,`end_time`)
) ENGINE=InnoDB AUTO_INCREMENT=100015 DEFAULT CHARSET=utf8;
Create Table: CREATE TABLE `t_2` (
`id2` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id1` int(10) unsigned NOT NULL,
`num` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id2`,`id1`)
) ENGINE=InnoDB AUTO_INCREMENT=100015 DEFAULT CHARSET=utf8;
for ((i=0;i<=10000;i++))
do
mysql -uroot --exec="insert into t_1 (num,comment) values($i,$i)" test;
mysql -uroot --exec="insert into t_2 (id1) values($i)" test;
done
The right index length, key_len=12
mysql (root@localhost) > explain select t_1.* from t_1 use key(id) where t_1.id between 100 and 105 and t_1.num>900 and t_1.end_time>'2013-12-27 13:50:45'
-> ;
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
| 1 | SIMPLE | t_1 | range | id | id | 12 | NULL | 1 | Using index condition |
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
However, if we join table t1,t2
mysql (root@localhost) > explain select t_1.* from t_1 use key(id) where t_1.id in (select id1 from t_2 where t_2.id2 between 1000 and 1050)
-> and t_1.num>900 and t_1.end_time>'2013-12-27 13:50:45';
+----+-------------+-------+-------+---------------+---------+---------+--------------+------+-------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+--------------+------+-------------------------------------------+
| 1 | SIMPLE | t_2 | range | PRIMARY | PRIMARY | 4 | NULL | 5 | Using where; Using index; Start temporary |
| 1 | SIMPLE | t_1 | ref | id | id | 4 | test.t_2.id1 | 1 | Using index condition; End temporary |
+----+-------------+-------+-------+---------------+---------+---------+--------------+------+-------------------------------------------+
2 rows in set (0.00 sec)
The key_len is 4, only the t1.id is used, not (id,num,end_time).
Any suggestions about this?
Thanks
Shenglin
Create Table: CREATE TABLE `t_1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`num` bigint(20) unsigned NOT NULL,
`start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`end_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`comment` binary(204) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`,`num`,`end_time`)
) ENGINE=InnoDB AUTO_INCREMENT=100015 DEFAULT CHARSET=utf8;
Create Table: CREATE TABLE `t_2` (
`id2` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id1` int(10) unsigned NOT NULL,
`num` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id2`,`id1`)
) ENGINE=InnoDB AUTO_INCREMENT=100015 DEFAULT CHARSET=utf8;
for ((i=0;i<=10000;i++))
do
mysql -uroot --exec="insert into t_1 (num,comment) values($i,$i)" test;
mysql -uroot --exec="insert into t_2 (id1) values($i)" test;
done
The right index length, key_len=12
mysql (root@localhost) > explain select t_1.* from t_1 use key(id) where t_1.id between 100 and 105 and t_1.num>900 and t_1.end_time>'2013-12-27 13:50:45'
-> ;
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
| 1 | SIMPLE | t_1 | range | id | id | 12 | NULL | 1 | Using index condition |
+----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
However, if we join table t1,t2
mysql (root@localhost) > explain select t_1.* from t_1 use key(id) where t_1.id in (select id1 from t_2 where t_2.id2 between 1000 and 1050)
-> and t_1.num>900 and t_1.end_time>'2013-12-27 13:50:45';
+----+-------------+-------+-------+---------------+---------+---------+--------------+------+-------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+--------------+------+-------------------------------------------+
| 1 | SIMPLE | t_2 | range | PRIMARY | PRIMARY | 4 | NULL | 5 | Using where; Using index; Start temporary |
| 1 | SIMPLE | t_1 | ref | id | id | 4 | test.t_2.id1 | 1 | Using index condition; End temporary |
+----+-------------+-------+-------+---------------+---------+---------+--------------+------+-------------------------------------------+
2 rows in set (0.00 sec)
The key_len is 4, only the t1.id is used, not (id,num,end_time).
Any suggestions about this?
Thanks
Shenglin