Sql server 使用sql server 2008加速我的查询。交叉应用的替代方案

Sql server 使用sql server 2008加速我的查询。交叉应用的替代方案,sql-server,sql-server-2008,Sql Server,Sql Server 2008,我有一个执行速度非常慢的函数。我正在处理一个需要迁移数据的数据库,我无法控制它!。 理想情况下,我希望直接使用视图,因为该函数由视图调用,但我似乎只能通过调用函数来实现 === 视图应按orderNo返回dummytable中的任何内容。如果orderNo的paymenttype为“利息”,则余额应为利息,如果“税”应为税 在现实生活中,我将有200000行和更多行,通过使用交叉应用,它似乎会减慢很多 有没有比使用CrossApply更好的方法来获取数据 这里的Noddy示例(对于示例的semp

我有一个执行速度非常慢的函数。我正在处理一个需要迁移数据的数据库,我无法控制它!。 理想情况下,我希望直接使用视图,因为该函数由视图调用,但我似乎只能通过调用函数来实现

=== 视图应按orderNo返回dummytable中的任何内容。如果orderNo的paymenttype为“利息”,则余额应为利息,如果“税”应为税

在现实生活中,我将有200000行和更多行,通过使用交叉应用,它似乎会减慢很多

有没有比使用CrossApply更好的方法来获取数据

这里的Noddy示例(对于示例的semplicity来说,数据和数据类型只是虚构的)


这与您的示例查询几乎相同。它将为您提供一行结果,您的查询将在其中重复匹配
@OrderNo
的所有行的值

select sum(case when T1.PaymentType = 'Tax' then cast(T1.Balance as money) else 0 end) as Tax,
       sum(case when T1.PaymentType = 'Interest' then cast(T1.Balance as money) else 0 end) as Interest,
       sum(case when T1.PaymentType = 'Deposit' then cast(T1.Balance as money) else 0 end) as Deposit
from DummyTable as T1
where T1.OrderNo = @OrderNo
顺便说一句,您应该确保表中
OrderNo
的数据类型与变量
@OrderNo
相同。看起来您正在处理整数,因此应该更改表。如果这对您来说是不可能的,那么您需要将
@OrderNo
更改为
varchar(255)
,如果您想在
OrderNo
上使用索引

select sum(case when T1.PaymentType = 'Tax' then cast(T1.Balance as money) else 0 end) as Tax,
       sum(case when T1.PaymentType = 'Interest' then cast(T1.Balance as money) else 0 end) as Interest,
       sum(case when T1.PaymentType = 'Deposit' then cast(T1.Balance as money) else 0 end) as Deposit
from DummyTable as T1
where T1.OrderNo = @OrderNo