我无法使用 MySQL InnoDB 优化器优化请求。以下查询(查询 1)高效运行:
explain select * from ah_problems
where rnid in (6022342, 6256614, 5842714, 6302489)
and fieldid in (5,6);
计划(计划一)如下:
id select_type table type possible_keys key key_len ref rows Extra
= ====== =========== ===== =============================== ============= ======= === ==== =====
1 SIMPLE ah_problems range CONSTRAINTFIELDID,RNID__FIELDID RNID__FIELDID 8 33 Using where
到现在为止还挺好。
而下面稍作修改的查询(查询 2)将采用灾难性的执行计划:
explain select * from ah_problems
where rnid in (select rec.rnid as record_id from ar_records rec where rnid in (6022342, 6256614, 5842714, 6302489))
and fieldid in (5, 6)
结果是一样的,但是计划(计划2)现在是这样做的:
id select_type table type possible_keys key key_len ref rows Extra
= ====== =========== ===== ================== ======== ======= ==== ======= =====
1 PRIMARY ah_problems ALL CONSTRAINTFIELDID 36177754 Using where
2 DEPENDENT SUBQUERY rec unique_subquery PRIMARY PRIMARY 4 func 1 Using index; Using where
如果您想知道,那个新的子查询...
select rec.rnid as record_id from ar_records rec where rnid in (6022342, 6256614, 5842714, 6302489)
...只不过返回在查询 1 中硬编码的四行:
6022342
6256614
5842714
6302489
所以查询 (1) 和 (2) 是等价的。
你猜怎么着,我需要查询 2,而不是一个。我希望查询 2 与查询 1 一样高效。我尝试了以下方法:
查询 3:添加
FORCE INDEX(RNID_FIELDID)
到查询 2。MySQL 直接忽略它。解释 select * from ah_problems force index (rnid__fieldid) where rnid in (select rec.rnid as record_id from ar_records rec where rnid in (6022342, 6256614, 5842714, 6302489)) 和 fieldid in (5,6)
执行计划与计划2相同。
查询 4:
ORDER BY RNID, FIELDID
向查询 3 添加一个。我在其他一些问题上看到这可能会欺骗优化器。它没有帮助。解释 select * from ah_problems force index (rnid__fieldid) where rnid in (select rec.rnid as record_id from ar_records rec where rnid in (6022342, 6256614, 5842714, 6302489)) and fieldid in (5, 6) order by rnid, fieldid
计划 4 现在正在使用索引,但行数仍然是灾难性的:
id select_type table type possible_keys key key_len ref rows Extra
= ====== =========== ===== ================== ======== ======= ==== ======= =====
1 PRIMARY ah_problems index RNID__FIELDID 8 36179307 Using where
2 DEPENDENT SUBQUERY rec unique_subquery PRIMARY PRIMARY 4 func 1 Using index; Using where
如果这有帮助,这就是我的ah_problems
表格的定义。不幸的是,我无法更改表格的定义。我能做些什么来让 MySQL 优化器使用计划 1 来攻击ah_problems
查询 2 中的表吗?
CREATE TABLE `ah_problems` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Identifier for update statements',
`RNID` int(11) NOT NULL COMMENT 'Record number',
`FIELDID` int(11) NOT NULL COMMENT 'Which field is value in',
`VALUE` varchar(255) NOT NULL COMMENT 'The value the field got on MODIFIED_DATE',
`PREVIOUSID` int(11) DEFAULT NULL COMMENT 'Reference to previous value',
`MODIFIED_DATE` datetime NOT NULL COMMENT 'When was it changed',
`MODIFIED_GROUPID` int(11) DEFAULT NULL COMMENT 'In what group did modified_userid change it',
`MODIFIED_USERID` int(11) NOT NULL COMMENT 'Who changed it',
PRIMARY KEY (`ID`),
KEY `CONSTRAINTFIELDID` (`FIELDID`),
KEY `CONSTRAINTMODIFIED_GROUPID` (`MODIFIED_GROUPID`),
KEY `CONSTRAINTMODIFIED_USERID` (`MODIFIED_USERID`),
KEY `CONSTRAINTPREVIOUSID` (`PREVIOUSID`),
KEY `RNID__FIELDID` (`RNID`,`FIELDID`),
CONSTRAINT `HPRB_FIELD` FOREIGN KEY (`FIELDID`) REFERENCES `ad_fields` (`ID`),
CONSTRAINT `HPRB_MODIFIED_GROUP` FOREIGN KEY (`MODIFIED_GROUPID`) REFERENCES `ap_groups` (`ID`),
CONSTRAINT `HPRB_MODIFIED_USER` FOREIGN KEY (`MODIFIED_USERID`) REFERENCES `ap_users` (`ID`),
CONSTRAINT `HPRB_PREVIOUS` FOREIGN KEY (`PREVIOUSID`) REFERENCES `ah_problems` (`ID`) ON DELETE CASCADE,
CONSTRAINT `HPRB_RN` FOREIGN KEY (`RNID`) REFERENCES `ar_records` (`RNID`)
) ENGINE=InnoDB AUTO_INCREMENT=72305308 DEFAULT CHARSET=utf8 COMMENT='PTR history'$$