views:

67

answers:

1

Ok we have a simple udf that takes a XML integer list and returns a table:

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)
)

In a stored procedure we create a temp table using this 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;

For a simple list of 10 JobNumbers in SQL2005 this returns in less than 1 second, in 2008 this run against the exact same data returns in 7 min. This is on a much faster machine with more memory. Any ideas?

If i change the SP to do this instead the performance becomes comparable to the 2005 version.

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  A: 

Verify that the servers have the same indexes.

Also check that the statistics are up to date:

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

For more help, post the difference between the query execution plans (set showplan_text on)

Andomar
The indexes are the same, the stats all have today's date, and the plans show no difference. I changed the SP (see above) to store the udf results into a table variable and it improved the performance, however I still fail to get why there is a big difference in the original.
B Thomsen
@B Thomson: At least the times in the execution plan should be different? Can you see which part is taking longer?
Andomar
It turned out the stats were out of date, I ran your query on the wrong database. It looks like the scheduled task to update them wasn't set up on the new server.
B Thomsen