我知道线程很旧,有人问我同样的问题,我做了一个测试,结果如下...
以FacCurrencyRate = 14264记录,而TestFunction如果独立执行则返回105。
SELECT F.*, x.CurrencyKey, x.CurrencyName
FROM (
SELECT CurrencyKey, CurrencyName FROM dbo.TestFunction()
) x
INNER JOIN [dbo].[FactCurrencyRate] F ON x.CurrencyKey = f.CurrencyKey;
执行时间是...
(14264 rows affected)
Table 'FactCurrencyRate'. Scan count 1, logical reads 75, physical reads 1, read-ahead reads 73, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'DimCurrency'. Scan count 1, logical reads 2, physical reads 1, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 31 ms, elapsed time = 749 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
如果我使用建议的答案,如下所示...
select F.*, x.CurrencyKey, x.CurrencyName from [dbo].[FactCurrencyRate] F
cross apply dbo.TestFunction() x
执行时间和结果计数为...
(1497720 rows affected)
Table 'FactCurrencyRate'. Scan count 1, logical reads 75, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 1, logical reads 38110, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'DimCurrency'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 2106 ms, elapsed time = 43242 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 0 ms.
我在这里看到的是内部查询带来了更正确的结果集,执行时间更加高效。用更好的方法纠正我,以实现相同目标!