我有这个征税查询需要5分钟,我希望将其结果存储在一个表中,后台将对其进行查询。
此外,该表应每小时更新一次,并完全用新数据替换表的内容。
我找到了this Server Agent solution,根据我的理解,我应该这样做:
这是最佳方式吗?
这是有问题的查询。它超过了大量的行,我认为这是主要的速度影响因素。
WITH
salesCTE AS (
select
concat(year(m.addate), '-', format(m.addate, 'MM')) AS ym,
year(m.addate) as y,
format(m.addate, 'MM') as m,
sum(M.anvalue) as salesRev
FROM tHE_Move m
WHERE (
RIGHT(LEFT(M.acKey,5),3) = '300'
OR RIGHT(LEFT(M.acKey,5),3) = '305'
OR RIGHT(LEFT(M.acKey,5),3) = '319'
OR RIGHT(LEFT(M.acKey,5),3) = '380'
OR RIGHT(LEFT(M.acKey,5),3) = '355'
OR RIGHT(LEFT(M.acKey,5),3) = '360'
OR RIGHT(LEFT(M.acKey,5),3) = '3X1'
OR RIGHT(LEFT(M.acKey,5),3) = '395'
) and m.adDate between '01.01.2014' and '01.01.2030'
GROUP BY
concat(year(m.addate), '-', format(m.addate, 'MM')),
year(m.addate),
format(m.addate, 'MM')
),
retailCTE AS (
select
concat(year(m.addate), '-', format(m.addate, 'MM')) AS ym,
year(m.addate) as y,
format(m.addate, 'MM') as m,
sum(M.anvalue) as retailRev
FROM tHE_Move m
where (
RIGHT(LEFT(M.acKey,5),3) = '321'
OR RIGHT(LEFT(M.acKey,5),3) = '322'
OR RIGHT(LEFT(M.acKey,5),3) = '323'
OR RIGHT(LEFT(M.acKey,5),3) = '324'
OR RIGHT(LEFT(M.acKey,5),3) = '325'
OR RIGHT(LEFT(M.acKey,5),3) = '326'
OR RIGHT(LEFT(M.acKey,5),3) = '327'
OR RIGHT(LEFT(M.acKey,5),3) = '328'
OR RIGHT(LEFT(M.acKey,5),3) = '329'
OR RIGHT(LEFT(M.acKey,5),3) = '331'
OR RIGHT(LEFT(M.acKey,5),3) = '332'
OR RIGHT(LEFT(M.acKey,5),3) = '333'
OR RIGHT(LEFT(M.acKey,5),3) = '334'
OR RIGHT(LEFT(M.acKey,5),3) = '335'
OR RIGHT(LEFT(M.acKey,5),3) = '336'
OR RIGHT(LEFT(M.acKey,5),3) = '337'
OR RIGHT(LEFT(M.acKey,5),3) = '338'
OR RIGHT(LEFT(M.acKey,5),3) = '339'
OR RIGHT(LEFT(M.acKey,5),3) = '341'
OR RIGHT(LEFT(M.acKey,5),3) = '342'
OR RIGHT(LEFT(M.acKey,5),3) = '343'
OR RIGHT(LEFT(M.acKey,5),3) = '344'
OR RIGHT(LEFT(M.acKey,5),3) = '345'
OR RIGHT(LEFT(M.acKey,5),3) = '346'
OR RIGHT(LEFT(M.acKey,5),3) = '347'
OR RIGHT(LEFT(M.acKey,5),3) = '348'
OR RIGHT(LEFT(M.acKey,5),3) = '349'
OR RIGHT(LEFT(M.acKey,5),3) = '352'
OR RIGHT(LEFT(M.acKey,5),3) = '353'
) and m.adDate between '01.01.2014' and '01.01.2030'
GROUP BY
concat(year(m.addate), '-', format(m.addate, 'MM')),
year(m.addate),
format(m.addate, 'MM')
)
SELECT
s1.ym,
s1.salesRev,
(s1.salesRev / s2.salesRev - 1) * 100 salesDelta,
r1.retailRev,
(r1.retailRev / r2.retailRev - 1) * 100 retailDelta,
s1.salesRev + r1.retailRev totalRev,
((s1.salesRev + r1.retailRev) / (s2.salesRev + r2.retailRev) - 1) * 100 totalDelta
FROM salesCTE s1
left join salesCTE s2
on s2.y = s1.y - 1 and s1.m = s2.m
left join retailCTE r1
on s1.ym = r1.ym
left join retailCTE r2
on r2.y = r1.y - 1 and r1.m = r2.m
order by s1.ym desc
正如评论中所提到的,FORMAT
是一个巨大的性能击球手;而我的庞大我真的意味着巨大的。拿这个DB<>Fiddle使用FORMAT
和CONVERT
来改变日期的值。使用CONVERT
的查询执行如果46ms(在db小提琴上),但FORMAT
查询花了1218毫秒!这慢了26倍。
将format(m.addate, 'MM')
更改为RIGHT('00' + CONVERT(varchar(2),DATEPART(MONTH,DATEADD(DAY,N-1,0))),2)
将对您的查询产生显着的性能优势。虽然后者看起来更复杂,但它(如小提琴演示)将大幅度地执行FORMAT
。老实说,我建议永远不要使用FORMAT
,微软弄错了这个功能。
不过,我也建议将RIGHT(LEFT(M.acKey,5),3)
作为持久列添加到您的表中:
ALTER TABLE tHE_Move ADD {Meaningful Name} AS RIGHT(LEFT(M.acKey,5),3) PERSISTED;
然后,您还可以将该值添加到索引(新的或现有的),它也将极大地有益于查询的性能;也许只需要几秒钟。