SQL 中使用order By后,查询慢,加上主键 和 需要排序的字段组合排序 速度有很大的提升

在SQL Server查询数据测试,数据约三万条, 数据字段以时间倒序排序,

sql:

select ID, column1,column2,column3,record_date from  table  where ...... order by record_date  desc

此时查询数据需要15秒左中 ,将orderby 修改为  order by ID desc,record_date desc 后,查询的数据一秒不到即可查询出来

 

在linq中,排序的时候,一定要用new 排序的对象,不然ID 将不会被加入到SQL中

 

linq:

var t = from a in t where ......select a;

t = t.orderby(t=>t.ID).orderby(t=>t.record_date)  此处的ID在解释成SQL时,不会在SQL中

应写为:

t = t.orderby(t=>new{t.ID,t.record_date})

 

posted on 2016-05-26 11:38  beggar_  阅读(2181)  评论(0编辑  收藏  举报