SQL查询效率比较。
写SQL查询多表的时候一定要注意,能尽量少用连接最好少用,因为同是两个表数据的查询,用连接查询查询的数据量是笛卡尔积(TABLE1*TABLE2),而嵌套查询的话效率就高多了,例子如下:
第一种直接挂机:
select p.pn, s.sn, s.msn, p.partid, s.partserid
from amicos.pnrreg p, amicos.snrreg s ,amicos.irc i
where s.partid = p.partid
and p.irc = i.irc
and i.engine = ‘X’
and s.msn = ’73Q01704′
and s.avail=’IN’
第二种:1秒搞定
select s.sn,s.partserid,s.msn from amicos.snrreg s
where exists (select p.partid from amicos.pnrreg p,amicos.irc i
where p.irc=i.irc and i.engine=’X')
and s.avail=’IN’
and s.msn = ’73Q01704′
http://pengdelin1984.blog.163.com/blog/#m=0