日期:2014-05-18  浏览次数:20470 次

问一个sql 语句
select [ID] from tradelist 
where tid in (SELECT tid FROM Trade group by tid) and status= 'WAIT_SELLER_SEND_GOODS' and iid ='STO' and (print_ex is null or print_ex=0) 



select [ID] from tradelist 
where tid in (SELECT tid FROM Trade group by tid) and status= 'WAIT_SELLER_SEND_GOODS' and (print_ex is null or print_ex=0) 

这两个个语句 就差了一个条件 iid ='STO' 
为什么执行的速度就相差很多 后者比前者快很多 ? 有什么办法让效率高点?
知道的朋友麻烦帮助下

------解决方案--------------------
看执行计划,加了和不加有没有走索引
------解决方案--------------------
iid列上面没有索引吧,或者加上iid列破坏了原来的索引使用,你可以看看执行计划有没有什么变化没
------解决方案--------------------
确实没有利用到索引 呵呵
------解决方案--------------------
SQL code

select [ID] from (Select ID,tid,status,print_ex from tradelist where iid ='STO') as t
where tid in (SELECT tid FROM Trade) and status= 'WAIT_SELLER_SEND_GOODS'  and (print_ex is null or print_ex=0)

------解决方案--------------------
如果是MSSQL2005及以上版本,建议你将tid、status、iid、print_ex按照这个顺序组成复合索引。
------解决方案--------------------
补一条:
设计表的时候,print_ex字段默认值设置下。
is null 或者is not null 比较耗。
------解决方案--------------------
把字段iid设置为复合索引之一就不会出现效率很差的结果。