sql - 无论如何,以提高查询或任何替代方式的性能
问题描述
我已经存储了要优化查询的过程。我需要在同一个临时表中设置三种不同类型的数据。以下是我的查询
SET @Counter = -3;
WHILE (@Counter <=-1)
BEGIN
insert into #tempTable (col1, col2,amount)
select col1,col2,
CASE
WHEN @Counter = -2 THEN MAX(col3) --sample actual is different
WHEN @Counter = -3 then sum(Col3)
WHEN @Counter = -1 col3
SET @Counter = @Counter + 1;
from #tempTable where amount>100
group by col1,col2,amount
END
任何优化它的方法
解决方案
为什么不简单地使用UNION ALL
?:
INSERT INTO #tempTable (col1, col2, amount)
SELECT col1, col2, MAX(col3)
FROM #tempTable
WHERE amount > 100
GROUP BY col1, col2
UNION ALL
SELECT col1, col2, SUM(col3)
FROM #tempTable
WHERE amount > 100
GROUP BY col1, col2
UNION ALL
SELECT col1, col2, col3
FROM #tempTable
WHERE amount > 100;
推荐阅读
- php - 使用链接中的所有表单信息将用户重定向到 PayPal 结帐页面是否安全?
- python-3.7 - 如果user_input为A,为什么当我编写函数以给出输出1时输出为空白
- python - 如何修复显示“无效的连续字节”的 UnicodeDecodeError?
- coq - 如何在 Coq 中证明 `(fun _ : nat => False) = Empty_set nat`?
- java - Count Duplicate Entries Firebase
- android - 在 android studio 错误上设置颤振
- django - 无法将条带付款保存到表中
- mongoose - 按嵌套填充()字段过滤
- react-admin - 如何在 hasura react admin 中动态更新数据提供者标头?
- c - 如何创建一个更改枚举器默认值的函数?