我正在寻找一种优化以下内容的方法:
SELECT
(SELECT SUM(amount) FROM Txn_Log WHERE gid=@gid AND txnType IN (3, 20)) AS pendingAmount,
(SELECT COUNT(1) FROM Txn_Log WHERE gid = @gid AND txnType = 11) AS pendingReturn,
(SELECT COUNT(1) FROM Txn_Log WHERE gid = @gid AND txnType = 5) AS pendingBlock
其中@gid 是一个参数,gid 是该表上的一个索引字段。问题:每个子查询在同一组条目上重新运行 - 三个重新运行两个太多了。