这两个mysql查询产生完全相同的结果,但查询A是一个简单的联合,它有where postType子句嵌入在单个查询中,而查询B具有相同的where子句应用于虚拟表的外部选择,这是单个查询结果的联合。我担心来自查询B的虚拟表可能变得太大而没有充分的理由如果有很多行但是我有点困惑因为查询A的工作顺序如何;是否也不必为了排序结果而制作虚拟表或类似的东西。所有这些都可能取决于工会的工作顺序如何?如果一个联盟的命令也在制作临时表;那么查询A几乎等同于资源中的查询B(与查询A相比,我们在系统中实现查询B要容易得多)?请以任何可能的方式提供指导/建议。
查询A
SELECT `t1`.*, `t2`.ignorableBalancingColumnFromQuery1
FROM `t1` INNER JOIN `t2` ON
`t1`.websiteID= `t2`.ownerID
AND `t1`.authorID= `t2`.authorID
AND `t1`.authorID=1559 AND `t1`.postType="simplePost"
UNION
SELECT `t1`.*, 111 AS ignorableBalancingColumnFromQuery1
FROM `t1` where websiteID=1559 AND postType="simplePost"
ORDER BY postID limit 0,50
查询B
Select * from (
SELECT `t1`.*,`t2`.ignorableBalancingColumnFromQuery1
FROM `t1` INNER JOIN `t2` ON
`t1`.websiteID= `t2`.ownerID
AND `t1`.authorID= `t2`.authorID
AND `t1`.authorID=1559
UNION
SELECT `t1`.*, 111 AS ignorableBalancingColumnFromQuery1
FROM `t1` where websiteID=1559
)
As sigma where postType="simplePost" ORDER BY postID limit 0,50
解析查询
id type table type possible_keys keys key_len ref rows Extra 1 PRIMARY t2 ref userID userID 4 const 1 1 PRIMARY t1 ref authorID authorID 4 const 2 Usingwhere 2 UNION t1 ref websiteID websiteID 4 const 9 Usingwhere NULL UNIONRESULT <union1,2> ALL NULL NULL NULL NULL NULL Usingfilesort
解析查询B
id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 10 Using where; Using filesort 2 DERIVED t2 ref userID userID 4 1 2 DERIVED t1 ref authorID authorID 4 2 Using where 3 UNION t1 ref websiteID websiteID 4 9 NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL