我有一个执行两种不同形式的查询,我都尝试过,但是第一个比另一个要快,但是我读到子查询对性能有好处,然后我想知道为什么第一个是在较短的时间内执行,这是更好的选择。
我有一个查询缝:
SELECT count(*) as cantProductos,
(select count(*) from Table2
join Table1 on Table2.idT=Table1.idT
where Table2.state=3
) as cant1,
(select count(*) from table2
join otherTable
where otherTable.origin=10
) as cant2 FROM Table1 WHERE Table1.state=1
但是我改变了
SELECT SUM(CASE WHEN Table1.state=1 THEN 1 ELSE 0 END) cantProducts
SUM(CASE WHEN table2.state=3 THEN 1 ELSE 0 END) as cant1,
SUM(CASE WHEN otherTable.origin=10 THEN 1 ELSE 0 END) as cant2
FROM Table1
LEFT JOIN table2 on Table2.idT=Table1.idT
LEFT JOIN otherTable on 0=0
感谢您的解释。