当前位置: 代码迷 >> Sql Server >> 急求下面语句的优化1解决方法
  详细解决方案

急求下面语句的优化1解决方法

热度:89   发布时间:2016-04-27 12:53:16.0
急急急..求下面语句的优化1
//排队挂机数
select RingDuration,QueueDuration from EXTERNAL_CDR.dbo.CDRMAIN where sessionid not in (select sessionid from EXTERNAL_CDR.dbo.CDRMAIN where TalkDuration>0 and sessionid in(select sessionid from EXTERNAL_CDR.dbo.CDRMAIN where TalkDuration=0 )) and sessionid in(select sessionid from EXTERNAL_CDR.dbo.CDRMAIN tic where TalkDuration=0 and Direction=1 and exists(select 1 from dbo.t_user_Users tuu where exists (select 1 from dbo.t_user_UserCombo uc left join dbo.t_user_Users uu on uc.c_userID=uu.c_userID where uu.c_state=1 and uc.c_comboID='559' and uc.c_beginTime <=getDate() and uc.c_endTime>getDate() and uc.c_state=1 and tuu.c_userID=uc.c_userID) and tic.CallerNum_bak=tuu.c_mobile) and starttime>=1330531200 and starttime<1331049600)

------解决方案--------------------
not in可以用not exists替换,
主意确认决胜属性字段,where,group by,order by后面出现的字段加索引


------解决方案--------------------
尼玛,这么多嵌套子查询
写个临时表吧
------解决方案--------------------
该加索引的加索引,not in后面的子查询返回的数据量大的话就用not exists给替换了
------解决方案--------------------
嵌套太多了,如果可以的话换换临时表试试
------解决方案--------------------
用临时表。分步处理吧。。。。。。。。
------解决方案--------------------
探讨

大神们求解啊..................

------解决方案--------------------
SQL code
SELECT  sessionidFROM    EXTERNAL_CDR.dbo.CDRMAINWHERE   TalkDuration > 0        AND sessionid IN ( SELECT   sessionid                           FROM     EXTERNAL_CDR.dbo.CDRMAIN                           WHERE    TalkDuration = 0 )--这句的意思就是得到TalkDuration既大于0又有等于0的sessionid--等价于SELECT  sessionid FROM    ( SELECT * FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration > 0  UNION ALL  SELECT * FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration = 0) a GROUP BY sessionid HAVING  ( COUNT(1) > 1 )
------解决方案--------------------
SQL code
-- 把in 和 not in 都去掉;WITH m AS(    SELECT  sessionid FROM        ( SELECT * FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration > 0      UNION ALL      SELECT * FROM EXTERNAL_CDR.dbo.CDRMAIN WHERE TalkDuration = 0    ) a GROUP BY sessionid HAVING  ( COUNT(1) > 1 )),m1 AS(     SELECT  sessionid            FROM    EXTERNAL_CDR.dbo.CDRMAIN tic            WHERE   TalkDuration = 0                    AND Direction = 1                    AND EXISTS ( SELECT 1                                 FROM   dbo.t_user_Users tuu                                 WHERE  EXISTS ( SELECT 1                                                 FROM   dbo.t_user_UserCombo uc                                                        LEFT JOIN dbo.t_user_Users uu ON uc.c_userID = uu.c_userID                                                 WHERE  uu.c_state = 1                                                        AND uc.c_comboID = '559'                                                        AND uc.c_beginTime <= GETDATE()                                                        AND uc.c_endTime > GETDATE()                                                        AND uc.c_state = 1                                                        AND tuu.c_userID = uc.c_userID )                                        AND tic.CallerNum_bak = tuu.c_mobile )                    AND starttime >= 1330531200                    AND starttime < 1331049600)SELECT  RingDuration ,        QueueDurationFROM    EXTERNAL_CDR.dbo.CDRMAIN a        LEFT JOIN m b ON a.sessionid = b.sessionid        LEFT JOIN m1 c ON a.sessionid = c.sessionidWHERE   b.sessionid IS NULL        AND c.sessionid IS NOT NULL
  相关解决方案