FOR XML PATH:DISTINCT排序成本增加

时间:2018-03-15 14:46:07

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

我想在连接字符串中只放入唯一值。我的代码目前是:

SELECT  PITEM2.orderid,
                    (SELECT  ISNULL(E.FIRSTNAME + ' ' + E.LASTNAME,' ') + ', ' AS [text()]
                     FROM F_PURCHASEITEM PITEM1
                     LEFT JOIN E__EMPLOYEE E ON e.EMPLOYEEID=PITEM1.APPROVED_BY
                     WHERE PITEM1.ORDERID = PITEM2.ORDERID
                       AND PITEM1.PISTATUS =
                         (SELECT POSTATUSID
                          FROM F_POSTATUS
                          WHERE POSTATUSNAME = 'Invoice Received') GROUP By ISNULL(E.FIRSTNAME + ' ' + E.LASTNAME,' ') + ', '
                       FOR XML PATH ('') ) [EmployeeNames]
                  FROM F_PURCHASEITEM PITEM2
                  WHERE ORDERID=305089 Group By PITEM2.orderid

这给了我期望的输出,但查询的成本增加了,执行计划显示Distinct排序为46.3%。

enter image description here

如何降低这种不同的成本呢?

0 个答案:

没有答案