MySQL加入功能?这可以改善吗?

时间:2014-07-16 15:59:54

标签: mysql sql performance

因此,看看Concrete5的一些查询并尝试对其表进行一些索引。我遇到了一个我认为的问题"必须有一个更好的方法"但我的研究并没有把我带到任何地方。从语法上来说,我的问题是:"是否可以编写内部联接的if(p2.cID IS NULL, p1.cID, p2.cID)部分。"我不得不相信这会导致联接的效率低下,我已经附上了查询解释输出,希望其他人可以告诉我它是否真的重要。现在这是一个全新的安装,所以数据不多,但在内连接中使用函数似乎......对我来说很奇怪。

Join of Ifs Explain

查询:

select p1.cID, pt.ptHandle, c.cDateAdded, pagepaths.cPath 
from Pages p1 left join Pages p2 on (p1.cPointerID = p2.cID) 
left join PagePaths on (PagePaths.cID = p1.cID and PagePaths.ppIsCanonical = 1) 
left join PageSearchIndex psi on (psi.cID = if(p2.cID is null, p1.cID, p2.cID)) 
inner join CollectionVersions cv on (cv.cID = if(p2.cID is null, p1.cID, p2.cID) and cvID = 1) 
left join PageTypes pt on (pt.ptID = if(p2.cID is null, p1.ptID, p2.ptID)) 
inner join Collections c on (c.cID = if(p2.cID is null, p1.cID, p2.cID));

查询没有缓存的If Query的执行时间:0.00161268

注意使用if函数的所有连接。我能够通过联合查询获得相同的查询结果,但老实说,我不确定这样做是否更有效。还要记住,使用PHP动态添加where子句,但我现在不太关心它。我愿意接受有关如何改进这一建议的建议,或者大多数人会说这会扩展得很好。

更新

这是Union Query(使用较小的结果集运行得相当慢)和Explain:

Union Query Explain

select p1.cID, pt.ptHandle, c.cDateAdded, pagepaths.cPath 
from Pages p1 left join Pages p2 on (p1.cPointerID = p2.cID) 
left join PagePaths on (PagePaths.cID = p1.cID and PagePaths.ppIsCanonical = 1) 
left join PageSearchIndex psi on (psi.cID = p1.cID AND p2.cID is null) 
inner join CollectionVersions cv on (cv.cID = p1.cID AND p2.cID is null and cvID = 1) 
left join PageTypes pt on (pt.ptID = p1.ptID AND p2.cID is null) 
inner join Collections c on (c.cID = p1.cID AND p2.cID is null)
UNION
select p1.cID, pt.ptHandle, c.cDateAdded, pagepaths.cPath 
from Pages p1 left join Pages p2 on (p1.cPointerID = p2.cID) 
left join PagePaths on (PagePaths.cID = p1.cID and PagePaths.ppIsCanonical = 1) 
left join PageSearchIndex psi on (psi.cID = p2.cID AND p2.cID is not null) 
inner join CollectionVersions cv on (cv.cID = p2.cID AND p2.cID is not null and cvID = 1) 
left join PageTypes pt on (pt.ptID = p2.ptID AND p2.cID is not null) 
inner join Collections c on (c.cID = p2.cID AND p2.cID is not null);

查询没有缓存的联合查询的执行时间:0.00763072

更新2(Barmar的子查询版本)

所以这似乎与使用IF的当前版本的执行情况大不相同,但它确实提出了不同的查询执行计划。

Subquery Explain

查询:

SELECT p.cID, pt.ptHandle, c.cDateAdded, pp.cPath
FROM (SELECT IFNULL(p2.cID, p1.cID) as cID, IFNULL(p2.ptID, p1.ptID) AS ptID
      FROM Pages AS p1
      LEFT JOIN Pages AS p2 ON (p1.cPointerID = p2.cID)) AS p
left join PagePaths pp on (pp.cID = p.cID AND pp.ppIsCanonical = 1)
left join PageSearchIndex psi on (psi.cID = p.cID) 
inner join CollectionVersions cv on (cv.cID = p.cID and cvID = 1) 
left join PageTypes pt on (pt.ptID = p.ptID) 
inner join Collections c on (c.cID = p.cID);

查询没有缓存的子查询版本的执行时间:0.00190587

1 个答案:

答案 0 :(得分:1)

这是与子查询连接的版本,该子查询返回适当的ID:

SELECT p.cID, p2.ptHandle, c.cDateAdded, pp.cPath
FROM (SELECT IFNULL(p2.cID, p1.cID) as cID, IFNULL(p2.ptID, p1.ptID) AS ptID
      FROM Pages AS p1
      LEFT JOIN Pages AS p2 ON (p1.cPointerID = p2.cID)) AS p
left join PagePaths pp on (PagePaths.cID = p.cID AND pp.ppIsCanonical = 1)
left join PageSearchIndex psi on (psi.cID = p.cID) 
inner join CollectionVersions cv on (cv.cID = p.cID and cvID = 1) 
left join PageTypes pt on (pt.ptID = p.ptID) 
inner join Collections c on (c.cID = p.cID);