需要优化查询并重写它,因为它需要花费大量时间在服务器上运行

时间:2015-02-17 14:28:51

标签: tsql ssms

我有这个查询并且花了很多时间来运行,有什么方法可以改进它,创建任何新的视图,,,我不能创建临时数据库表,因为加载时它们被DBA避免服务器

SELECT DISTINCT o.email AS eml_addr,
    UPPER(max(FORMAT(cl.clicktime, 'dd-MMM-yyyy'))) AS lst_clk_mnth,
    UPPER(max(FORMAT(o.opentime, 'dd-MMM-yyyy'))) AS lst_opn_mnth
FROM [open] o WITH (NOLOCK)
INNER JOIN view_mailing m WITH (NOLOCK)
    ON (o.mailingid = m.mailingid)
INNER JOIN view_campaign c WITH (NOLOCK)
    ON (c.campaignid = m.campaignid)
LEFT JOIN click cl WITH (NOLOCK)
    ON (
            o.mailingid = cl.mailingid
            AND o.email = cl.email
            )
WHERE c.clientid IN (
        SELECT clientid
        FROM client
        WHERE sameclient IN (
                SELECT sameclient
                FROM client
                WHERE clientid IN (219)
                )
        )
    AND CONVERT(VARCHAR(8), cast(o.opentime AS DATETIME), 112) & gt;= 20130826
GROUP BY o.email;

谢谢

1 个答案:

答案 0 :(得分:0)

我没有看到双方的目的 派生表不使用索引
varchar中的日期不是最佳的

SELECT o.email AS eml_addr,
       UPPER(max(FORMAT(cl.clicktime, 'dd-MMM-yyyy'))) AS lst_clk_mnth,
       UPPER(max(FORMAT(o.opentime,   'dd-MMM-yyyy'))) AS lst_opn_mnth
  FROM [open] o WITH (NOLOCK)  
  LEFT JOIN click cl WITH (NOLOCK)
    ON o.mailingid = cl.mailingid
   AND o.email = cl.email         
  JOIN view_mailing m WITH (NOLOCK)
    ON o.mailingid = m.mailingid   
  JOIN view_campaign c WITH (NOLOCK)
    ON c.campaignid = m.campaignid
  join client with (NOLOCK) 
    on c.clientid = client.sameclient 
   and client.clientid IN (219) 
 WHERE CONVERT(VARCHAR(8), cast(o.opentime AS DATETIME), 112) & gt;= 20130826
 GROUP BY o.email;