我有一个使用嵌套CTE的查询,它位于用户定义的函数中。我必须使用嵌套的CTE,因为我想重新使用先前选择的一些计算/案例语句。该查询与下面的内容类似。
;with cte1 as
(
select a, b from Table1
),
ct2 as
(
case when a =1 then 1 else 0 end as c, b from cte2
)
select * from cte2
我在一个从多个存储过程调用的udf中有这个。在此查询中进行了大量计算。当查询在函数外部运行时,我注意到性能差异。对于大约12,000条记录,当从SQL管理工作室运行查询时,它运行在11秒内,应用所有参数。当相同的参数提供给udf时,大约需要55秒。我试图将查询放在存储过程而不是udf中,但仍然是相同的55秒。看起来当从管理控制台运行查询时,它使用并行性进行查询,但不使用函数或存储过程。
此时这不是一个主要问题,但如果可以的话,我想达到相同的11秒演奏。有没有人遇到类似的情况?
答案 0 :(得分:1)
在存储过程中显示您的“设置”,并从SSMS中显示。我有同样的事情,SSMS更快,程序更慢。您有时可以解决此问题,因为SSMS运行时的设置与过程不同,设置相同,您可能会在过程中看到相同的性能。以下是一些显示设置的示例代码:
SELECT SESSIONPROPERTY ('ANSI_NULLS') --Specifies whether the SQL-92 compliant behavior of equals (=) and not equal to (<>) against null values is applied.
--1 = ON
--0 = OFF
SELECT SESSIONPROPERTY ('ANSI_PADDING') --Controls the way the column stores values shorter than the defined size of the column, and the way the column stores values that have trailing blanks in character and binary data.
--1 = ON
--0 = OFF
SELECT SESSIONPROPERTY ('ANSI_WARNINGS') --Specifies whether the SQL-92 standard behavior of raising error messages or warnings for certain conditions, including divide-by-zero and arithmetic overflow, is applied.
--1 = ON
--0 = OFF
SELECT SESSIONPROPERTY ('ARITHABORT') -- Determines whether a query is ended when an overflow or a divide-by-zero error occurs during query execution.
--1 = ON
--0 = OFF
SELECT SESSIONPROPERTY ('CONCAT_NULL_YIELDS_NULL') --Controls whether concatenation results are treated as null or empty string values.
--1 = ON
--0 = OFF
SELECT SESSIONPROPERTY ('NUMERIC_ROUNDABORT') --Specifies whether error messages and warnings are generated when rounding in an expression causes a loss of precision.
--1 = ON
--0 = OFF
SELECT SESSIONPROPERTY ('QUOTED_IDENTIFIER') --Specifies whether SQL-92 rules about how to use quotation marks to delimit identifiers and literal strings are to be followed.
--1 = ON
--0 = OFF
您可以将它们添加到结果集::
SELECT
col1, col2
,SESSIONPROPERTY ('ARITHABORT') AS ARITHABORT
,SESSIONPROPERTY ('ANSI_WARNINGS') AS ANSI_WARNINGS
,SESSIONPROPERTY ('...
FROM ...
如果您一次只使用一个,请先尝试ARITHABORT
。
见:Resolving an ADO timeout issue in VB6 和Why would SET ARITHABORT ON dramatically speed up a query?