Index is ignored
Affects | Status | Importance | Assigned to | Milestone | ||
---|---|---|---|---|---|---|
MySQL Server |
Unknown
|
Unknown
|
||||
Percona Server moved to https://jira.percona.com/projects/PS | Status tracked in 5.7 | |||||
5.5 |
Invalid
|
Undecided
|
Unassigned | |||
5.6 |
Incomplete
|
Medium
|
Unassigned | |||
5.7 |
Incomplete
|
Medium
|
Unassigned |
Bug Description
I have 2 tables: small `a_datakind`, and big `data`, approx. 150GB (see attachment).
The query:
SELECT STRAIGHT_JOIN *
FROM data d
WHERE d.Data LIKE '%A223%'
AND d.DataKind
IN (
SELECT dk.DataKindID
FROM a_datakind dk
WHERE dk.Transform = 11
);
.. runs inefficiently, EXPLAIN gives:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY d ALL NULL NULL NULL NULL 4835390 Using where
2 SUBQUERY dk ref PRIMARY,Transform Transform 5 const 6 Using index
Adding "FORCE INDEX (Index_data)" to "data d" has no effect.
Without "STRAIGHT_JOIN" (which required because original query joins some tables), index `Index_data` is used OK:
EXPLAIN SELECT *
FROM data d
WHERE d.Data LIKE '%A223%'
AND d.DataKind
IN (
SELECT dk.DataKindID
FROM a_datakind dk
WHERE dk.Transform = 11
);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE dk ref PRIMARY,Transform Transform 5 const 6 Using index
1 SIMPLE d ref Index_data Index_data 4 main.dk.DataKindID 268632 Using where
So, in production, I have to collect datakinds by additional query:
SELECT GROUP_CONCAT(
.. and then use its output in the IN clause.
Able to reproduce the same with above table structure and PS 5.6.21.
mysql> show create table a_datakind \G ******* ******* ****** 1. row ******* ******* ******* ****** type` (`DataType`), datakind` (`ParentDataKind`), stage` (`ParentStageTy pe`), datakind` FOREIGN KEY (`ParentDataKind`) REFERENCES `a_datakind` (`DataKindID`)
*******
Table: a_datakind
Create Table: CREATE TABLE `a_datakind` (
`DataKindID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`DataName` varchar(100) NOT NULL DEFAULT '',
`DataDesc` text NOT NULL,
`DataType` tinyint(3) unsigned NOT NULL,
`ParentStageType` int(10) unsigned NOT NULL COMMENT 'Parent stage data refers to.',
`ParentDataKind` int(10) unsigned DEFAULT NULL,
`Sort` int(10) NOT NULL DEFAULT '0',
`AllowMany` varchar(60) NOT NULL DEFAULT '' COMMENT 'Several data can be associated with move',
`Readonly` varchar(100) NOT NULL DEFAULT '0' COMMENT 'Data are calculated at move / stage start.',
`Required` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'Data is required.',
`onChange` varchar(100) NOT NULL DEFAULT '' COMMENT 'Javascript code',
`onSave` varchar(100) NOT NULL DEFAULT '' COMMENT 'PHP code.',
`Param` text NOT NULL,
`Param2` varchar(100) NOT NULL DEFAULT '',
`Param3` varchar(200) NOT NULL DEFAULT '',
`DefValue` text NOT NULL COMMENT 'SQL code.',
`Transform` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`DataKindID`),
KEY `FK_a_datakind_
KEY `FK_a_datakind_
KEY `FK_a_datakind_
KEY `Transform` (`Transform`),
CONSTRAINT `FK_a_datakind_
) ENGINE=InnoDB DEFAULT CHARSET=cp1251 ROW_FORMAT=DYNAMIC
1 row in set (0.00 sec)
mysql> ******* ******* ****** 1. row ******* ******* ******* ****** ,`Data` (32)), parentdata` (`ParentData`), parentdata` FOREIGN KEY (`ParentData`) REFERENCES `data` (`DataID`) ON DELETE CASCADE
mysql> show create table data \G
*******
Table: data
Create Table: CREATE TABLE `data` (
`DataID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`DataKind` int(10) unsigned NOT NULL DEFAULT '0',
`ParentMove` int(10) unsigned NOT NULL DEFAULT '0',
`ParentData` int(10) unsigned DEFAULT NULL,
`Data` longblob NOT NULL,
PRIMARY KEY (`DataID`),
KEY `Index_data` (`DataKind`
KEY `FK_data_
KEY `FK_data_move` (`ParentMove`),
CONSTRAINT `FK_data_datakind` FOREIGN KEY (`DataKind`) REFERENCES `a_datakind` (`DataKindID`),
CONSTRAINT `FK_data_
) ENGINE=InnoDB DEFAULT CHARSET=cp1251 ROW_FORMAT=DYNAMIC
1 row in set (0.00 sec)
mysql> insert into a_datakind (DataName, DataDesc, DefValue, Transform) values ('abcded' ,'asdkjasd' ,'asdasd' ,1);
Query OK, 1 row affected, 3 warnings (0.01 sec)
mysql> insert into a_datakind (DataName, DataDesc, DefValue, Transform) values ('abcded' ,'asdkjasd' ,'asdasd' ,10);
Query OK, 1 row affected, 3 warnings (0.01 sec)
mysql> insert into a_datakind (DataName, DataDesc, DefValue, Transform) values ('abcded' ,'asdkjasd' ,'asdasd' ,15);
Query OK, 1 row affected, 3 warnings (0.01 sec)
mysql> insert into a_datakind (DataName, DataDesc, DefValue, Transform) values ('abcded' ,'asdkjasd' ,'asdasd' ,11);
Query OK, 1 row affected, 3 warnings (0.00 sec)
mysql> select * from a_datakind; ------+ ------- ---+--- ------- +------ ----+-- ------- ------- -+----- ------- ----+-- ----+-- ---...
+------