当前位置: 代码迷 >> Sql Server >> sqlserver的t-sql类乎不支持 显式的并行语法?解8皇后引发的疑问
  详细解决方案

sqlserver的t-sql类乎不支持 显式的并行语法?解8皇后引发的疑问

热度:23   发布时间:2016-04-24 19:01:32.0
sqlserver的t-sql好像不支持 显式的并行语法?解8皇后引发的疑问
sqlserver的t-sql好像不支持 显式的并行语法?
比如:
select * from tb1
union all
select * from tb2
union all
select * from tb3
如果能先同时并行执行3个select,再把它们的结果合起来,这样 比分别执行3个select,再把它们的结果合起来 要省掉2个select的时间了

这样,可以把解8皇后的一个sql,分拆为8个子sql的union all
子sql就是固定第一行的位置,其它7行才是全排列。。。。。。这样,如果子sql能并发执行,效率也就高了很多:
原来是8个8^7+1次union all,现在是1个8^7+1次union all

但是,好像没有语法指定子sql必须并行执行。。。。。。

实际在sql2k5下试验了一下,union all的子查询确没有被并发执行:
2cpu x 4核 x 超线程=16 (cpu),执行时,cpu总是只有6%(=1/16)的使用量

declare @c table (c1 tinyint,c2 tinyint,c3 tinyint,c4 tinyint,c5 tinyint,c6 tinyint,c7 tinyint,c8 tinyint,c9 tinyint,c10 tinyint,c11 tinyint,c12 tinyint)

--该表做笛卡尔积
declare @t table (p tinyint)

insert into @t 
      select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
union all select 11
union all select 12

insert @c

select
*
from @t t1 
cross join @t t2 cross join @t t3 cross join @t t4 cross join @t t5 cross join @t t6 cross join @t t7 cross join @t t8 cross join @t t9 cross join @t t10 cross join @t t11 cross join @t t12 
where t1.p=1
--竖
 and t1.p<>t2.p and t1.p<>t3.p and t1.p<>t4.p and t1.p<>t5.p and t1.p<>t6.p and t1.p<>t7.p and t1.p<>t8.p and t1.p<>t9.p and t1.p<>t10.p and t1.p<>t11.p and t1.p<>t12.p
 and t2.p<>t3.p and t2.p<>t4.p and t2.p<>t5.p and t2.p<>t6.p and t2.p<>t7.p and t2.p<>t8.p and t2.p<>t9.p and t2.p<>t10.p and t2.p<>t11.p and t2.p<>t12.p
 and t3.p<>t4.p and t3.p<>t5.p and t3.p<>t6.p and t3.p<>t7.p and t3.p<>t8.p and t3.p<>t9.p and t3.p<>t10.p and t3.p<>t11.p and t3.p<>t12.p
 and t4.p<>t5.p and t4.p<>t6.p and t4.p<>t7.p and t4.p<>t8.p and t4.p<>t9.p and t4.p<>t10.p and t4.p<>t11.p and t4.p<>t12.p
 and t5.p<>t6.p and t5.p<>t7.p and t5.p<>t8.p and t5.p<>t9.p and t5.p<>t10.p and t5.p<>t11.p and t5.p<>t12.p
 and t6.p<>t7.p and t6.p<>t8.p and t6.p<>t9.p and t6.p<>t10.p and t6.p<>t11.p and t6.p<>t12.p
 and t7.p<>t8.p and t7.p<>t9.p and t7.p<>t10.p and t7.p<>t11.p and t7.p<>t12.p
 and t8.p<>t9.p and t8.p<>t10.p and t8.p<>t11.p and t8.p<>t12.p
 and t9.p<>t10.p and t9.p<>t11.p and t9.p<>t12.p
 and t10.p<>t11.p and t10.p<>t12.p
 and t11.p<>t12.p

--右斜
 and t1.p<>t2.p-1 and t1.p<>t3.p-2 and t1.p<>t4.p-3 and t1.p<>t5.p-4 and t1.p<>t6.p-5 and t1.p<>t7.p-6 and t1.p<>t8.p-7 and t1.p<>t9.p-8 and t1.p<>t10.p-9 and t1.p<>t11.p-10 and t1.p<>t12.p-11
 and t2.p<>t3.p-1 and t2.p<>t4.p-2 and t2.p<>t5.p-3 and t2.p<>t6.p-4 and t2.p<>t7.p-5 and t2.p<>t8.p-6 and t2.p<>t9.p-7 and t2.p<>t10.p-8 and t2.p<>t11.p-9 and t2.p<>t12.p-10
 and t3.p<>t4.p-1 and t3.p<>t5.p-2 and t3.p<>t6.p-3 and t3.p<>t7.p-4 and t3.p<>t8.p-5 and t3.p<>t9.p-6 and t3.p<>t10.p-7 and t3.p<>t11.p-8 and t3.p<>t12.p-9
 and t4.p<>t5.p-1 and t4.p<>t6.p-2 and t4.p<>t7.p-3 and t4.p<>t8.p-4 and t4.p<>t9.p-5 and t4.p<>t10.p-6 and t4.p<>t11.p-7 and t4.p<>t12.p-8
 and t5.p<>t6.p-1 and t5.p<>t7.p-2 and t5.p<>t8.p-3 and t5.p<>t9.p-4 and t5.p<>t10.p-5 and t5.p<>t11.p-6 and t5.p<>t12.p-7
 and t6.p<>t7.p-1 and t6.p<>t8.p-2 and t6.p<>t9.p-3 and t6.p<>t10.p-4 and t6.p<>t11.p-5 and t6.p<>t12.p-6
 and t7.p<>t8.p-1 and t7.p<>t9.p-2 and t7.p<>t10.p-3 and t7.p<>t11.p-4 and t7.p<>t12.p-5
  相关解决方案