当前位置: 代码迷 >> Sql Server >> 加了个hint,为何结果不一样,求解释.解决思路
  详细解决方案

加了个hint,为何结果不一样,求解释.解决思路

热度:112   发布时间:2016-04-27 13:58:35.0
加了个hint,为何结果不一样,求解释.
SQL code
create table ap(id int not null,  yx varchar(30) constraint pk_ap primary key (id))insert into apselect 1,'aaa' union allselect 2,'bbb' union allselect 3,'ccc' union allselect 4,'ddd' union allselect 5,'eee'select * from apid          yx----------- ------------------------------1           aaa2           bbb3           ccc4           ddd5           eeedeclare @r1 varchar(50),@r2 varchar(50)select @r1='',@r2=''select @[email protected]+yx+',' from ap where id>=3order by case when yx='yyy' then '1' else yx endselect @r2=''select @[email protected]+yx+',' from ap where id>=3order by case when yx='yyy' then '1' else yx endoption(force order)select @r1 r1, @r2 r2r1                                                 r2-------------------------------------------------- --------------------------------------------------ccc,ddd,eee,                                       eee,(1 row(s) affected)


------解决方案--------------------

------解决方案--------------------
select @r2=''
select @[email protected]+yx+',' from ap where id>=3
order by case when yx='yyy' then '1' else yx end
option(force order)

问题在红色这里。
------解决方案--------------------
表示疑惑中,难道和哈希表的生成有关?
------解决方案--------------------
SQL code
declare @r1 varchar(50),@r2 varchar(50)select @r1='',@r2=''select @[email protected]+yx+',' from ap where id>=3order by 1,--yx  --case when yx='yyy' then '1' else yx endselect @r2=''select @[email protected]+yx+',' from ap where id>=3order by 1,--yx --case when yx='yyy' then '1' else yx endoption(force order)select @r1 r1, @r2 r2
------解决方案--------------------
option (force order)

FORCE ORDER

指定在查询优化过程中保持由查询语法指示的联接顺序。

http://msdn.microsoft.com/zh-cn/library/ms181714%28v=SQL.90%29.aspx




------解决方案--------------------
看了下执行计划,第2个语句比第1个语句少了一次“计算标量”,难道问题就处在这里?
------解决方案--------------------
经测试,这个不是option(force order)造成的。以下两段的结果是一样的。
SQL code
declare @r1 varchar(50),@r2 varchar(50)select @r1='',@r2=''select @[email protected]+yx+',' from ap where id>=3order by 1select @r2=''select @[email protected]+yx+',' from ap where id>=3order by 1 option(force order)select @r1 r1, @r2 r2
------解决方案--------------------
查资料去,猜测应该和指定的排序有关系,验证中...
------解决方案--------------------
SQL code
declare @r1 varchar(12) set @r1=''select @[email protected]+yx+',' from ap where id>=3 order by yxselect @r1/*ccc,ddd,eee,*/declare @r1 varchar(12) set @r1=''select @[email protected]+yx+',' from ap where id>=3 order by yx option(force order)select @r1/*ccc,ddd,eee,*/declare @r1 varchar(12) set @r1=''select @[email protected]+yx+',' from ap where id>=3 order by 1select @r1/*eee,*/declare @r1 varchar(12) set @r1=''select @[email protected]+yx+',' from ap where id>=3 order by 1 option(force order)select @r1/*eee,*/declare @r1 varchar(12) set @r1=''select @[email protected]+yx+',' from ap where id>=3 order by case when yx='yyy' then '1' else yx endselect @r1/*ccc,ddd,eee,*/declare @r1 varchar(12) set @r1=''select @[email protected]+yx+',' from ap where id>=3 order by case when yx='yyy' then '1' else yx end option(force order)select @r1/*eee,*/
------解决方案--------------------
  相关解决方案