日期:2014-05-18  浏览次数:20508 次

SQL语句优化
语句如下:
(1)
select   *   from   tbizCity   c,tbizrcd   r,tbizsvr   s,tbizbrand   b,tbizgroup   g,tbiztype   t   where   r.branchCode=c.branchCode   and   r.brandId   =   b.brandId   and   r.bizCode   =   s.bizCode   and   r.brandid=s.brandid   and   s.grpId   =   g.grpId   and   s.typeId   =   t.typeId   and   status= 'OK '   and   (date(r.optTime)   between   '2007-07-13 '   and   '2007-09-13 ')   and   ditch=1     and   r.branchCode   in( ' ', 'GZ ', 'SZ ', 'ST '//这里有20个左右的城市代码)   and   r.bizCode   in( '20202 ', '21030 ', '60208 '//这里可能有一两百个业务代码)   and   r.brandId   in(3,2,1)

(2)
select   *   from   tbizCity   c,tbizrcd   r,tbizsvr   s,tbizbrand   b,tbizgroup   g,tbiztype   t   where   r.branchCode=c.branchCode   and   r.brandId   =   b.brandId   and   r.bizCode   =   s.bizCode   and   r.brandid=s.brandid   and   s.grpId   =   g.grpId   and   s.typeId   =   t.typeId   and   status= 'OK '   and   (date(r.optTime)   between   '2007-07-13 '   and   '2007-09-13 ')   and   ditch=1     and   (r.branchCode   =   ' '   or   r.branchCode   =   'GZ '   or   r.branchCode   =   'SZ '   or   r.branchCode   =   'ST '//有20个左右的城市代码)   and   (r.bizCode   =   '20202 '   or   r.bizCode   =   '21030 '   or   r.bizCode   =   '60208 '//这里可能有一两百个业务代码)   and   (r.brandId   =   3   or   r.brandId   =   2   or   r.brandId   =   1)

请问是用(1)的in谓词好还是用(2)的or好一些?或者大家有更好的办法吗?我对SQL语句不怎么懂。要连接6张表,表里面的记录很庞大

------解决方案--------------------
一样

用IN,简短点,在编译成查询执行计划时,会自动编译成OR的形式。

------解决方案--------------------
还有多表查询时用table1 a join table2 b on 条件比较好
------解决方案--------------------
(1)
select * from tbizCity c,tbizrcd r,tbizsvr s,tbizbrand b,tbizgroup g,tbiztype t where r.branchCode=c.branchCode and r.brandId = b.brandId and r.bizCode = s.bizCode and r.brandid=s.brandid and s.grpId = g.grpId and s.typeId = t.typeId and status= 'OK ' and (date(r.optTime) between '2007-07-13 ' and '2007-09-13 ') and ditch=1 and r.branchCode in( ' ', 'GZ ', 'SZ ', 'ST '//这里有20个左右的城市代码) and r.bizCode in( '20202 ', '21030 ', '60208 '//这里可能有一两百个业务代码) and r.brandId in(3,2,1)
-------------------------------------------------select * from tbizCity c,tbizrcd r,tbizsvr s,tbizbrand b,tbizgroup g,tbiztype t where r.branchCode=c.branchCode and r.brandId = b.brandId and r.bizCode = s.bizCode and r.brandid=s.brandid and s.grpId = g.grpId and s.typeId = t.typeId and status= 'OK ' and (date(r.optTime) > = '2007-07-13 ' and date(r.optTime) <= '2007-09-13 ') and ditch=1 and r.branchCode in( ' ', 'GZ ', 'SZ ', 'ST '//这里有20个左右的城市代码) and r.bizCode in( '20202 ', '21030 ', '60208 '//这里可能有一两百个业务代码) and r.brandId in(3,2,1)
in 和or 差不多


------解决方案--------------------
请问是用(1)的in谓词好还是用(2)的or好一些?或者大家有更好的办法吗?我对SQL语句不怎么懂。要连接6张表,表里面的记录很庞大