我有一个优化问题,我想首先执行子查询
EXPLAIN
SELECT *
FROM `References_galaxia`
, Link_galaxia
WHERE linkReferenced IN ( SELECT id
FROM Link_galaxia
WHERE idConceptStart IN (616269,616268,615721)
AND idConceptLink = 315
AND idConceptTarget = 29209
)
AND `References_galaxia`.linkReferenced = Link_galaxia.id
AND `References_galaxia`.idConcept IN (416,36053,36088,36037)
主查询将大约一百万条记录的数据量返回到内存中。而子查询
SELECT id FROM Link_galaxia WHERE idConceptStart IN (616269,616268,615721)AND idConceptLink = 315 AND idConceptTarget = 29209)
返回少量数据。如何管理首先执行子查询?
解释的结果
1 PRIMARY References_galaxia ALL unique_ref,linkReferenced NULL NULL NULL 9163156 Using where
1 PRIMARY Link_galaxia eq_ref PRIMARY PRIMARY 4 eds_sandra.References_galaxia.linkReferenced 1
2 DEPENDENT SUBQUERY Link_galaxia eq_ref PRIMARY,idx_name,idConceptStart,idConceptStart_4,idConceptTarget,idConceptLink PRIMARY 4 References_galaxia.linkReferenced 1 Using where
非常感谢你的帮助!
答案 0 :(得分:0)
SELECT *
FROM `References_galaxia`
, Link_galaxia
WHERE `References_galaxia`.linkReferenced = Link_galaxia.id
AND `References_galaxia`.idConcept IN (416,36053,36088,36037)
AND Link_galaxia.idConceptStart IN (616269,616268,615721)
AND Link_galaxia.idConceptLink = 315
AND Link_galaxia.idConceptTarget = 29209
答案 1 :(得分:0)
如下所示的查询似乎更为理想:
SELECT columns, I, actually, want
FROM `References_galaxia` r
JOIN Link_galaxia x
ON x.id = r.linkreferenced
JOIN Link_galaxia y
ON y.id = r.linkReferenced
WHERE r.idConcept IN (416,36053,36088,36037)
AND y.idConceptStart IN (616269,616268,615721)
AND y.idConceptLink = 315
AND y.idConceptTarget = 29209
有关提高此查询性能的帮助,我们需要查看所有相关表的表创建语句,以及EXPLAIN。
答案 2 :(得分:0)
只需在变量
中运行修改后的子查询和商店IDSELECT id INTO @idvar FROM Link_galaxia
WHERE idConceptStart IN (616269,616268,615721)
AND idConceptLink = 315
AND idConceptTarget = 29209
将此var(@idvar)包含在主SQL
中SELECT *
FROM `References_galaxia`
, Link_galaxia
WHERE linkReferenced IN @idvar
AND `References_galaxia`.linkReferenced = Link_galaxia.id
AND `References_galaxia`.idConcept IN (416,36053,36088,36037);
完整代码可以与事务:
START TRANSACTION;
SELECT id INTO @idvar FROM Link_galaxia
WHERE idConceptStart IN (616269,616268,615721)
AND idConceptLink = 315
AND idConceptTarget = 29209;
SELECT *
FROM `References_galaxia`
, Link_galaxia
WHERE linkReferenced IN @idvar
AND `References_galaxia`.linkReferenced = Link_galaxia.id
AND `References_galaxia`.idConcept IN (416,36053,36088,36037);
COMMIT;