我试图从使用join的两个大表中选择:
EXPLAIN SELECT SQL_NO_CACHE
e.*
FROM `table_A` e
JOIN
(SELECT id FROM `table_B` /* FORCE index (primary, index_A) */
WHERE id > 338107 AND `index_field_A` = 900000000 AND `index_field_B` = 1
ORDER BY id) AS c
ON `c`.`id` = `e`.`fk_id`
WHERE e.`some_field` IS NULL;
LIMIT 2000;
/* BEST EXPLAIN RESULT
USING intersect(index_A,index_B); USING WHERE; USING INDEX
*/
我将当前id存储在应用程序端(338107),以便能够通过批处理获得总结果(从开始id到最大id) 如果我摆脱 ORDER BY id 没有问题但是我不确定默认情况下MySQL命令PK。 如果我在没有JOIN的情况下使用单独的SELECT,那么TOO没有问题:
SELECT id FROM `table_B` WHERE id > 338107
AND `index_field_A` = 900000000 AND `index_field_B` = 1 ORDER BY id
但是没用了
如果我强制索引,我可以获得更好的解析:
FORCE index (primary, index_A)
但它离现实太远了。
我可以在没有负面后果的情况下摆脱ORDER BY ID吗?
另外:订购PK字段是自动增量,InnoDB表
答案 0 :(得分:0)
这样的事情怎么样?
SELECT *
FROM `table_A`
WHERE `some_field` IS NULL
AND `fk_id` IN (
SELECT `id`
FROM `table_B`
WHERE id > 338107 AND `index_field_A` = 900000000 AND `index_field_B` = 1
)
ORDER BY `fk_id`
;
或
SELECT e.*
FROM `table_B` AS c
LEFT JOIN `table_A` AS e
ON c.`id` = e.`fk_id`
AND e.`someField` IS NULL
WHERE c.id > 338107
AND c.`index_field_A` = 900000000
AND c.`index_field_B` = 1
HAVING e.fk_id IS NOT NULL
ORDER BY e.`fk_id`
;