例:
select stuNo,stuName,stuSex,stuAddress from stuInfo
where stuNo in ('001','002','003','004','005','006','007','008','009')请问各位大侠,类似这样的sql语句该怎么优化,谢谢!!!!
select stuNo,stuName,stuSex,stuAddress from stuInfo
where stuNo in ('001','002','003','004','005','006','007','008','009')请问各位大侠,类似这样的sql语句该怎么优化,谢谢!!!!
from stuInfo join (select '001' as stuNO UNION ALL ....) AS T
ON T.stuNo=stuInfo .stuNo
where stuNo in ('001','002','003')类似这个语句优化能写详细点吗? 谢谢
1楼同学不要人云亦云地相信UNION比IN性能好。要看具体情况的。
打开执行计划比较一下吧。
如果stuNo字段上有索引(聚集最好),这已经是最优的查询了。
where stuNo in ('001','002','003')
and stuName in ('ad','fdf','ere')如果这样呢 ? 咋优化?
where stuNo in ('001','002','003')
or stuName in ('wer','sf','df')咋优化?
如果stuName也需要经常查询的话,在这个字段上建非聚集索引。CREATE INDEX IX_stuInfo_stuName ON stuInfo(stuName)
这个不会用到这个索引吧
CREATE INDEX IX_stuInfo_stuName ON stuInfo(stuName)
不一定。SQLServer的查询引擎远比很多人认为的要聪明。
如果这个查询使用索引性能更高,就会用到索引。