SQL Server 2008 vs 2005 udf xml性能问题

时间:2010-05-18 21:05:21

标签: sql-server sql-server-2008 sql-server-2008-r2

好的,我们有一个简单的udf,它接受一个XML整数列表并返回一个表:

CREATE FUNCTION [dbo].[udfParseXmlListOfInt]
(
    @ItemListXml XML (dbo.xsdListOfInteger)
)
RETURNS TABLE 
AS
RETURN
(   --- parses the XML and returns it as an int table ---
    SELECT ListItems.ID.value('.','INT') AS KeyValue 
      FROM @ItemListXml.nodes('//list/item') AS ListItems(ID)
)

在存储过程中,我们使用此UDF

创建临时表
INSERT INTO @JobTable 
    (JobNumber, JobSchedID, JobBatID, StoreID, CustID, CustDivID, BatchStartDate, BatchEndDate, UnavailableFrom)
SELECT JOB.JobNumber, 
       JOB.JobSchedID,
       ISNULL(JOB.JobBatID,0), 
       STO.StoreID, 
       STO.CustID, 
       ISNULL(STO.CustDivID,0),
       AVL.StartDate, 
       AVL.EndDate,
       ISNULL(AVL.StartDate, DATEADD(day, -8, GETDATE()))
  FROM dbo.udfParseXmlListOfInt(@JobNumberList) TMP
       INNER JOIN dbo.JobSchedule       JOB ON (JOB.JobNumber = TMP.KeyValue)
       INNER JOIN dbo.Store             STO ON (STO.StoreID = JOB.StoreID)
       INNER JOIN dbo.JobSchedEvent     EVT ON (EVT.JobSchedID = JOB.JobSchedID AND EVT.IsPrimary = 1)
       LEFT OUTER JOIN dbo.Availability AVL ON (AVL.AvailTypID = 5 AND AVL.RowID = JOB.JobBatID)
 ORDER BY JOB.JobSchedID;

对于SQL2005中10个JobNumbers的简单列表,这将在不到1秒的时间内返回,在2008年,这将在7分钟内针对完全相同的数据返回运行。这是一台内存更快的机器。有什么想法吗?

如果我改变SP来执行此操作,则性能将与2005版本相当。

DECLARE @JobNumbers TABLE
(
        JobNumber       INT     NOT NULL 
    );

    INSERT INTO @JobNumbers
        (JobNumber)
    SELECT * from dbo.udfParseXmlListOfInt(@JobNumberList);


INSERT INTO @JobTable 
    (JobNumber, JobSchedID, JobBatID, StoreID, CustID, CustDivID, BatchStartDate, BatchEndDate, UnavailableFrom)
SELECT JOB.JobNumber, 
       JOB.JobSchedID,
       ISNULL(JOB.JobBatID,0), 
       STO.StoreID, 
       STO.CustID, 
       ISNULL(STO.CustDivID,0),
       AVL.StartDate, 
       AVL.EndDate,
       ISNULL(AVL.StartDate, DATEADD(day, -8, GETDATE()))
  FROM @JobNumbers TMP
           INNER JOIN dbo.JobSchedule       JOB ON (JOB.JobNumber = TMP.JobNumber)
       INNER JOIN dbo.Store             STO ON (STO.StoreID = JOB.StoreID)
       INNER JOIN dbo.JobSchedEvent     EVT ON (EVT.JobSchedID = JOB.JobSchedID AND EVT.IsPrimary = 1)
       LEFT OUTER JOIN dbo.Availability AVL ON (AVL.AvailTypID = 5 AND AVL.RowID = JOB.JobBatID)
 ORDER BY JOB.JobSchedID;

1 个答案:

答案 0 :(得分:2)

验证服务器是否具有相同的索引。

同时检查统计信息是否是最新的:

SELECT 
    object_name = Object_Name(ind.object_id),
    IndexName = ind.name,
    StatisticsDate = STATS_DATE(ind.object_id, ind.index_id)
FROM SYS.INDEXES ind
order by STATS_DATE(ind.object_id, ind.index_id) desc

要获得更多帮助,请发布查询执行计划(set showplan_text on

之间的差异