日期:2014-05-17  浏览次数:20537 次

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 an