日期:2014-05-17  浏览次数:20507 次

sql求优化
SELECT a.node, a.nodename, a.chanid, b.isPTZ,c.dvrPort,o.OrgCode,d.id FROM B AS a LEFT JOIN B AS b ON a.chanId = b.ID
LEFT JOIN C AS c ON b.DvrID = c.ID  
LEFT JOIN D o on o.id = c.orgid 
LEFT JOIN E d on d.vischanno = b.channo and b.channo !='' and b.channo is not null 
where a.chanGroupId in (13673,13674,13887,1376113,……)

where in 这个里面有很多(100个以上),之前我是传了一个一维数组进去查询,但是这样查询速度很慢,请优化方法。

------解决方案--------------------
SQL code
SELECT  a.node ,
        a.nodename ,
        a.chanid ,
        b.isPTZ ,
        c.dvrPort ,
        o.OrgCode ,
        d.id
FROM    B AS a
        LEFT JOIN B AS b ON a.chanId = b.ID
        LEFT JOIN C AS c ON b.DvrID = c.ID
        LEFT JOIN D o ON o.id = c.orgid
        LEFT JOIN E d ON d.vischanno = b.channo
                         AND b.channo != ''
                         AND b.channo IS NOT NULL
WHERE   a.chanGroupId IN ( 13673, 13674, 13887, 1376113 ) 
--这里如果有1000个以上,不如LZ再建立一张表(建好索引),用exists判断一下即可。

--chanGroupId 也建好索引即可。