优化连接的SQL CTE - Server 2008

时间:2013-08-28 20:08:46

标签: sql sql-server-2008 tsql join

我真的需要以下查询的帮助;对于CTE和连接,执行需要44秒以上,但是,当我运行单独的select语句时,每个只需要1秒。为什么加入它们需要44秒?

WITH Z
     AS (SELECT GM,
                YQ,
                IG
         FROM   FN_qryFYQI(@pYQ1)),
     Y
     AS (SELECT GM,
                YQ,
                IG
         FROM   FN_qryFYQI(@pYQ2))
SELECT Z.GM,
       Z.IG AS IG1,
       Y.IG AS IG2
FROM   Y
       INNER JOIN Z
         ON Y.GM = Z.GM 

1 个答案:

答案 0 :(得分:1)

试试这个 -

SELECT  Z.GM
    ,   Z.IG AS IG1
    ,   Y.IG AS IG2
FROM FN_qryFYQI(@pYQ1) Y
JOIN (
    SELECT Z.GM, Z.IG
    FROM FN_qryFYQI(@pYQ2) Z
) Z ON Y.GM = Z.GM