有一个库,内有100W记录
一条简单的sql语句
select Top10 id,title sort from table where sort in('aaa','bbb','ccc')
效率很差但是如果是
select Top10 id,title sort from table where sort in('aaa') 那么可能1s就可以查询完毕
请问该如何优化这条sql语句?谢谢建立了索引是 id和sort 

解决方案 »

  1.   

    试试:
    select Top10 id,title sort from table where sort='aaa' or sort='bbb' or sort='ccc'另外,在sort列上建索引可以加快查询速度.
      

  2.   

    create index i_id_tit on 表名(sort) including(id,title)
      

  3.   

    #1.
    select Top10 id,title sort from table where sort in('aaa')
    =
    select Top10 id,title,sort from table where sort = 'aaa' --用到了索引
    #2.
    select Top10 id,title,sort from table where sort in('aaa','bbb','ccc')
    =
    select Top10 id,title,sort from table where sort = 'aaa' or sort = 'bbb' or sort = 'ccc'
    #3. 试试
    select Top10 id,title,sort from table where sort = 'aaa' 
    union all
    select Top10 id,title,sort from table where sort = 'bbb' 
    union all
    select Top10 id,title,sort from table where sort =  'ccc'
      

  4.   

    #3查出来超过10条了。select Top10 id,title,sort from table where sort = 'aaa' or sort = 'bbb' or sort = 'ccc'
    sort加索引。
      

  5.   

    select Top10 id,title sort from table where sort in('aaa','bbb','ccc')查看下执行计划直接在SORT加索引 ID不要加试试 这样应该能用到索引的..