首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

幫忙改一上這個簡單的排序能否快一點

2012-10-20 
幫忙改一下這個簡單的排序能否快一點select top 1000 id,username,usercode,isnull (dCompDt,convert(smal

幫忙改一下這個簡單的排序能否快一點
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
[解决办法]
探讨

引用:
引用:

不用索引的情況下怎麼寫會好一點呢?因為我沒權限改數據庫


刚没看到,你没权利动数据那你优化?


我的sql是寫在網頁裡的,數據庫我動不了

热点排行