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

Need to optimize the query and rewrite it as it taking a lot of time on running on server

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

由 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;

谢谢

我看不出 distinct 和 group by 的目的
派生 table 不使用索引
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;