例:
select stuNo,stuName,stuSex,stuAddress from stuInfo
where stuNo in ('001','002','003','004','005','006','007','008','009')请问各位大侠,类似这样的sql语句该怎么优化,谢谢!!!!

解决方案 »

  1.   

    select stuInfo.stuNo,stuName,stuSex,stuAddress 
    from stuInfo join (select '001' as stuNO UNION ALL ....) AS T
      ON T.stuNo=stuInfo .stuNo
      

  2.   

    把stuInfo表的stuNo字段设为主键(并且聚集索引)。
      

  3.   

    select stuNo,stuName,stuSex,stuAddress from stuInfo
    where stuNo in ('001','002','003')类似这个语句优化能写详细点吗?  谢谢
      

  4.   


    1楼同学不要人云亦云地相信UNION比IN性能好。要看具体情况的。
    打开执行计划比较一下吧。
      

  5.   


    如果stuNo字段上有索引(聚集最好),这已经是最优的查询了。
      

  6.   

    select stuNo,stuName,stuSex,stuAddress from stuInfo
    where stuNo in ('001','002','003')
      and stuName in ('ad','fdf','ere')如果这样呢   ?  咋优化?
      

  7.   

    select stuNo,stuName,stuSex,stuAddress from stuInfo
    where stuNo in ('001','002','003')
    or stuName in ('wer','sf','df')咋优化?
      

  8.   


    如果stuName也需要经常查询的话,在这个字段上建非聚集索引。CREATE INDEX IX_stuInfo_stuName ON stuInfo(stuName)
      

  9.   

    你们说的都是操作表了,我想知道只操作sql怎么优化、、、、
      

  10.   


    这个不会用到这个索引吧
    CREATE INDEX IX_stuInfo_stuName ON stuInfo(stuName)
     
      

  11.   


    不一定。SQLServer的查询引擎远比很多人认为的要聪明。
    如果这个查询使用索引性能更高,就会用到索引。