我有一个这样构造的视图:
CREATE VIEW [dbo].[myview] AS
SELECT
dbo.table1.field1
,dbo.table2.field2
,dbo.HeavyStoredProcedure(dbo.table1.field1) AS calculated_value
...
FROM
table1
INNER JOIN table2 ON (...)
WHERE
...
AND NOT( EXISTS (SELECT * from table3
WHERE (table3.somefield = dbo.HeavyStoredProcedure(dbo.table1.field1)))
OR
EXISTS (SELECT * from table4
WHERE (table4.anotherfield = dbo.HeavyStoredProcedure(dbo.table1.field1)))
)
这里的问题是“HeavyStoredProcedure”被多次调用,参数完全相同,从而减慢了整个过程的速度。
如何让我的视图只在 table1 中的每行调用一次“HeavyStoredProcedure”,然后在 WHERE 子句中重复使用计算结果两次?