极慢的SQL查询 - XML Path函数

时间:2018-03-29 14:26:34

标签: sql sql-server

这是最后的努力,看看是否有人有关于如何提高此SQL查询的执行速度的一些指示。 XML映射路径确实减慢了此查询的速度。执行时间超过21秒:

 SELECT [ldid],
       l.loadnumber,
       l.expectdt,
       l.receivedt,
       l.actualarrvdt,
       l.carrcode,
       c.carrdesc,
       cpl.refnum AS PONum,
       c.phone,
       l.source,
       l.pallets,
       l.comment,
       l.drivercell,
       [loadtrackingstatusid],
       Stuff((SELECT *
              FROM   (SELECT ','
                             + Cast(Cast(cpl.qty AS INT) AS VARCHAR)
                             + ' ' + Ltrim(Rtrim(p.proddesc)) + ' '
                             + Ltrim(Rtrim(p.prodpack) ) AS x
                      FROM   custpoline cpl
                             JOIN product p
                               ON p.prodid = cpl.prodid
                      WHERE  ( ( cpl.loadnumber = l.loadnumber )
                               AND l.source = 'LD' )
                              OR ( ( cpl.refnum = l.loadnumber )
                                   AND l.source = 'PO' )
                      UNION ALL
                      SELECT ',' + Cast(Cast(i.qty AS INT) AS VARCHAR)
                             + ' ' + Ltrim(Rtrim(p.proddesc)) + ' '
                             + Ltrim(Rtrim(p.prodpack))
                      FROM   inventory i
                             JOIN product p
                               ON p.prodid = i.prodid
                      WHERE  ( i.referencenum = l.loadnumber )
                             AND l.source = 'IX') a
              FOR xml path(''), type).value('.', 'varchar(max)'), 1, 1, '') AS
       proddesc
FROM   [Sales].[dbo].[load] l
LEFT JOIN carrier c ON c.carrcode = l.carrcode
LEFT JOIN custpoline cpl ON cpl.loadnumber = l.loadnumber
WHERE  startdt IS NULL
       AND enddt IS NULL
       AND expectdt >= Dateadd(day, -1, Getdate())
       AND expectdt <= Dateadd(day, 1, Getdate())
       AND cpl.warehouse = 'GU'
ORDER  BY expectdt DESC  

我尝试删除ltrimrtrim函数。我尝试了不同的演员数据类型。我希望有人有一些想法。

谢谢大家。

0 个答案:

没有答案