isnull(o.userdefinedstatus,0)<5---查看有沒有用到userdefinedstatus列的索引,沒用到時使用 with(index=索引名)指定

解决方案 »

  1.   

    選中語句Ctrl+L可查看執行計劃
      

  2.   

    --TRY
    select * from sale where isnull(o.userdefinedstatus,0) in(0,1,2,3,4)
      

  3.   

    如果是你这个语句要经常执行的话,我建议你先把这张表 的userdefinedstatus字段转成 数字类型,然后写between语句
      

  4.   

    那到底是isnull(o.userdefinedstatus,0) in(0,1,2,3,4)
    这个运行快还是isnull(o.userdefinedstatus,0)<5还是这个运行快呢?能否分析为什么会快?
      

  5.   

    看执行计划
    set showplan_Text on
      

  6.   

    个人觉得isnull(o.userdefinedstatus,0) <5快,in的话还要执行一次查询
      

  7.   

    StmtText
    -------------------------------------------------------------------------
    select * from objective o where isnull(o.userdefinedstatus,0)<5(1 行受影响)StmtText
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      |--Clustered Index Scan(OBJECT:([dzh_mobile].[dbo].[OBJECTIVE].[pk_objective_guid] AS [o]), WHERE:(CONVERT_IMPLICIT(int,isnull([dzh_mobile].[dbo].[OBJECTIVE].[USERDEFINEDSTATUS] as [o].[USERDEFINEDSTATUS],'0     '),0)<(5)))(1 行受影响)StmtText
    -------------------------------------------------------------------------------select * from objective o where isnull(o.userdefinedstatus,0) in(0,1,2,3,4)(1 行受影响)StmtText
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      |--Clustered Index Scan(OBJECT:([dzh_mobile].[dbo].[OBJECTIVE].[pk_objective_guid] AS [o]), WHERE:(CONVERT_IMPLICIT(int,isnull([dzh_mobile].[dbo].[OBJECTIVE].[USERDEFINEDSTATUS] as [o].[USERDEFINEDSTATUS],'0     '),0)=(4) OR CONVERT_IMPLICIT(int,isnull([(1 行受影响)这个执行计划怎么看,从上面分析可以看出什么东西出来呢?
      

  8.   

    select * from sale where isnull(o.userdefinedstatus,0)<=4 . 不过建议不要有NULL值,给字段一个Default值那样查询的效率就更高了.