我正在寻找一种方法来优化以下内容:
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是此表上的索引字段。问题:每个子查询在同一组条目上重新运行-三次重新运行太多了。
发布于 2009-11-19 19:08:24
你可以这样做:
select
sum(case when txnType in (3,20) then amount else 0 end) as pendingAmount,
sum(case txnType when 11 then 1 else 0 end) as pendingReturn,
sum(case txnType when 5 then 1 else 0 end) as pendingBlock
from
Txn_Log
where
gid = @gid发布于 2009-11-19 19:09:50
你能不能不做这样的事
SELECT sum(amount),count(1), txnType
FROM Txn_log
WHERE gid = @gid AND
txnType in (3,5,11,20)
group by txnType然后以编程的方式处理剩下的部分?
https://stackoverflow.com/questions/1762540
复制相似问题