你的位置:首页 > 数据库

[数据库]收集统计信息让SQL走正确的执行计划


数据库环境:SQL SERVER 2005

今天在生产库里抓到一条跑得慢的SQL,语句不是很复杂,返回的数据才有800多行,

却执行了34分钟,甚至更久。

先看一下执行结果

我贴一下SQL。

SELECT a.dec_yt1 ,    b.dec_yt1 ,    a.dec_yt2 ,    b.dec_yt2 ,    a.dec_yt3 ,    b.dec_yt3FROM  ctlm8288 a ,    ( SELECT  t.corr_id ,          t.clerk_id ,          ROUND(SUM(ISNULL(t.dec_yt1, 0)), 2) AS dec_yt1 ,          ROUND(SUM(ISNULL(t.dec_yt2, 0)), 2) AS dec_yt2 ,          ROUND(SUM(ISNULL(t.dec_yt3, 0)), 2) AS dec_yt3     FROM   ( SELECT  b.corr_id ,                b.clerk_id ,                CASE WHEN d.feetype_flag = '1'                   THEN ROUND(SUM(a.dec_sqty * c.fee_price),                        2)                END AS dec_yt1 ,                CASE WHEN d.feetype_flag = '2'                   THEN ROUND(SUM(a.dec_sqty * c.fee_price),                        2)                END AS dec_yt2 ,                CASE WHEN d.feetype_flag = '3'                   THEN ROUND(SUM(a.dec_sqty * c.fee_price),                        2)                END AS dec_yt3           FROM   ctlm8666 a ,                ctlm8001 b ,                v_saft04 c ,                ctlm8206 d           WHERE   a.terminal_id = b.terminal_id                AND b.dept_id = c.dept_id                AND a.item_id = c.vitem_id                AND c.fee_id = d.fee_id                AND d.flag_t = 'Y'                AND a.fiscal_year = 2015                AND a.fiscal_period <= 6                AND c.cur_year = 2015                AND b.tertype_id LIKE '0102%'           GROUP BY b.corr_id ,                b.clerk_id ,                c.fee_id ,                d.feetype_flag          ) t     GROUP BY t.corr_id ,          t.clerk_id    ) bWHERE  a.corr_id = b.corr_id    AND a.clerk_id = b.clerk_id    AND a.fiscal_year = 2015    AND a.fiscal_period = 6

SQL的写法没太大问题,我试着执行下,然后就是漫长的等待过程...

30分过去了,还没出结果,我看了执行计划,就知道问题出在哪了。

(本文完)