开发者

How to improve the performance of this query that spans across multiple databases?

here is my query taking nearly 20 mins. pls suggest me changes to increase performance

SELECT DISTINCT
    CONVERT(varchar(10),x.notice_date,120)  Date,
    Y.branch_name,
    count(case when x.status='broken' and x.branch_nam开发者_如何转开发e=y.branch_name then 1 end) 
      Broken,
    count(case when x.type='Lote' and x.branch_name=y.branch_name then 1 end)
      Lost,
    (   SELECT COUNT(A.car_no)
        FROM DB2.dbo.z_mat A
        WHERE DateAdd(Day, DateDiff(Day, 0,a.notice_date), 0)
              = DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
          AND a.branch_name=y.branch_name
    ) mat,
    (   SELECT COUNT(B.car_no)
        FROM DB2.dbo.z_cat B
        WHERE DateAdd(Day, DateDiff(Day, 0,b.notice_date), 0)
              = DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
          AND b.branch_name=y.branch_name
    ) cat,
    (   SELECT COUNT(C.car_no)
        FROM DB2.dbo.z_pat C
        WHERE DateAdd(Day, DateDiff(Day, 0,c.notice_date), 0)
              = DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
          AND c.branch_name=y.branch_name
    ) pat
FROM DB1.dbo.Cars x
   , DB2.dbo.Branch Y
WHERE DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
      > '2011-01-01'
GROUP BY CONVERT(varchar(10),x.notice_date,120)
       , DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)
       , y.branch_name


This might help. Give it a try.

SELECT          
DISTINCT        CONVERT(VARCHAR(10), car.notice_date, 120) AS NoticeDate
            ,   brc.branch_name
            ,   COUNT(CASE WHEN car.status = 'broken'   AND car.branch_name = brc.branch_name   THEN 1 END) Broken
            ,   COUNT(CASE WHEN car.status = 'Lote'     AND car.branch_name = brc.branch_name   THEN 1 END) Lost
            ,   mat.mat_count
            ,   cat.cat_count
            ,   pat.pat_count
FROM            DB1.dbo.Cars        car
            ,   DB2.dbo.Branch      brc
CROSS APPLY     (   
                    SELECT  COUNT(mat.car_no)   mat_count
                    FROM    DB2.dbo.z_mat       mat
                    WHERE   DATEDIFF(d, mat.notice_date, car.notice_date) = 0
                    AND     mat.branch_name     = brc.branch_name
                ) mat
CROSS APPLY     (   
                    SELECT  COUNT(cat.car_no)   cat_count
                    FROM    DB2.dbo.z_cat       cat
                    WHERE   DATEDIFF(d, cat.notice_date, car.notice_date) = 0
                    AND     cat.branch_name     = brc.branch_name
                ) cat
CROSS APPLY     (   
                    SELECT  COUNT(pat.car_no)   pat_count
                    FROM    DB2.dbo.z_pat       pat
                    WHERE   DATEDIFF(d, pat.notice_date, car.notice_date) = 0
                    AND     pat.branch_name     = brc.branch_name
                ) pat
WHERE           car.notice_date > '2011-01-01'
GROUP BY        CONVERT(VARCHAR(10), car.notice_date, 120)
            ,   brc.branch_name


SELECT CONVERT(varchar(10),x.notice_date,120) Date,Y.branch_name,
COUNT(case when x.status='broken' then 1 end) Broken,
COUNT(case when x.type='Lote' then 1 end) Lost,
SUM(mat) mat, SUM(cat) cat,SUM(pat) pat
 FROM DB1.dbo.Cars x 
      JOIN DB2.dbo.Branch Y ON x.branch_name=y.branch_name
      -- group by date and branch name for z_mat table
      LEFT JOIN (select COUNT(car_no) mat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_mat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS a
          ON a.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = a.notice_date
      -- group by date and branch name for z_cat table    
      LEFT JOIN (select COUNT(car_no) cat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_cat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS b
          ON b.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = b.notice_date
      -- group by date and branch name for z_pat table        
     LEFT JOIN (select COUNT(car_no) pat,branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) notice_date from DB2.dbo.z_pat GROUP BY branch_name,DateAdd(Day, DateDiff(Day,notice_date), 0)) AS c
          ON c.branch_name = y.branch_name AND DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0) = c.notice_date 
WHERE DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0)>'2011-01-01' 
GROUP BY CONVERT(varchar(10),x.notice_date,120),DateAdd(Day, DateDiff(Day, 0,x.notice_date), 0),y.branch_name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜