忙改一下的排序能否快一
select top 1000 id,username,usercode,isnull (dCompDt,convert(smalldatetime,'2078-06-06')) as newCompDt from tablename order by get_newCompDt desc
==========================
dCompDt是完成,已完成的(有)排在後面用倒序排,完成的排在前面(都是null值),
用上面的newCompDt方式排好慢啊,行要8秒左右,有30,已top 1000了啊。
可以改成什的法更快?
[解决办法]
你都没where语句,当然慢拉,全表扫描的。把get_newCompDt做个降序的聚集索引,可以减少order by 的开销。
[解决办法]
select top 1000 id,username,usercode
from tablename
order by case when dCompDt=null then 0 else 1 end,dCompDt desc
[解决办法]
select top 1000 id,username,usercode,isnull (dCompDt,convert(smalldatetime,'2078-06-06')) as newCompDt from tablename order by
case when dCompDt is null then 0 else 1 end ,dCompDt desc
[解决办法]
id 主键+dCompDt 索引
语句改成
- SQL code
select top 1000 id,username,usercode,isnull (dCompDt,convert(smalldatetime,'2078-06-06')) as newCompDt from tablenamecase when dCompDt is null then 0 else 1 end,dCompDt desc
[解决办法]