select mtlineid from bus_gz_dtline where stationid in(select stationid from bus_gz_stations where (stationname like '%"+linestart+"%' or stationname like '%"+lineend+"%') group by stationid) group by mtlineid这样一句应该如何优化?

解决方案 »

  1.   

    select distinct  mtlineid  from bus_gz_dtline as A
    inner join bus_gz_stations  as b 
    on A.stationid  = B.stationid 
    where B.stationname like '%"+linestart+"%' or B.stationname like '%"+lineend+"%' "
      

  2.   

    按照上面那位的改了,但是还是好慢啊,因为,这只是一条语句,里面查出来好多结果,还搜多一次
    select top 1 id,lineid,linetype,fstation,estation from bus_gz_line where id="+rt["mtlineid"]+" and linetype='上行'//这个是查出路线的
    select stationid from bus_gz_dtline where mtlineid="+id+" order by zhanci asc//这个是查出所有站点的
      

  3.   

    stationname like '%"+linestart+"%' 这种是无法利用索引的,并且单条记录处理速度也很慢。对于3万条记录,如果你能改为“stationname like '"+linestart+"%'”和“stationname like '"+lineend+"%'”相信可以提高10几倍速度。或者你使用专业一点的设计,使用全文检索系统,不要用“like”查询这种东西来充当检索。