请问各位大侠:
 如下语句应该如何优化好呢?
select  year,name,address  from tb where year in('1912','1915','1916','1918');
我的数据库是sql server 2005 

解决方案 »

  1.   

    与其优化sql,不如给year字段建个索引
      

  2.   

    若括号里的字段很多把IN改为exist吧
      

  3.   

    多用exists
    select * from A where exists(select b from B where b=A.b)
      

  4.   

    该sql已经很简单了,再优化,效果也不会很明显
      

  5.   

    select year,name,address from tb where year='1912'
    union all
    select year,name,address from tb where year='1915'
    union all
    select year,name,address from tb where year='1916'
    union all
    select year,name,address from tb where year='1918' 
      

  6.   

    关键是我是在开发当然,而且那些年份都是通过checkbox选择得到的结果,将其作为条件的。所以。。
      

  7.   

    就这样就行吧,不用优化了吧。这sql也太简单了。。而且括号里也不可能会有大量的数据
      

  8.   

    exists
    是在不行拼 AND / OR 也比IN快,当然是在数据量大的轻快下,小数据量看不出来
      

  9.   

    这个不用怎么优化了。sqlserver会帮你优化的,如果year有索引,也会跑索引的。
      

  10.   

    但是关键是我的year不要有索引,如果有索引的话,得到的结果就不是我想要的结果了。
      

  11.   


    select year,name,address from tb where 
    year='1912'
    or year='1915'
    or year='1916'
    or year='1918'


    select year,name,address from 
    (
    select year,name,address from tb where 
    year='1912'
    union
    select year,name,address from tb where 
    year='1915'
    union
    select year,name,address from tb where 
    year='1916'
    union
    select year,name,address from tb where 
    year='1918'
    )
      

  12.   

    多用exists
    select * from A where exists(select b from B where b=A.b)
    也可以建个索引。