在微软的文档上只看到这样的说法,至于效率以及使用上并没有找到相应的文档。
在网上找了一下相关的资料,有的文章说UNION ALL效率更高,因为UNION会进行排序,有的文章说UNION效率高,我实际测试的一个语句中,UNION效率确实比UNION ALL效率高很多,使用UNION的消耗是106左右,而使用UNION ALL则达到了4050左右。
不知道有没有关于这两个之间的区别、使用、效率等方面比较详细的说法呢?
8 个解决方案
#1
帮顶!关注!!!(个人感觉Union ALL效率高些吧,毕竟他不需要排除重复的记录!!!也不能概论吧!哈哈)
#2
具体情况具体分析。不知道你的UNION ALL语句是什么样子的。
#3
不知道你是怎么测试的
1、工鞥不一样,返回的结果都可能不一样,有啥好比较的
2、就算你两个select 不会重复,union 也比union all 多了一个合并的操作
当然要慢了
1、工鞥不一样,返回的结果都可能不一样,有啥好比较的
2、就算你两个select 不会重复,union 也比union all 多了一个合并的操作
当然要慢了
#4
SELECT R.TableID, R.TableNumber, R.SampNo, C.Design, C.KZ_Design, NULL AS Expr1, S.Cement, S.Grit, NULL AS Expr2, NULL AS StoneDiameter, S.Water, S.DecWater,
S.Coal, S.Other, NULL AS OtherEx, R.UseArea, R.proj_id, R.UnitID, R.UnitName, R.RecordID, R.CheckDate, C.CementSign, C.GritDiameter, NULL AS Stone1Diameter, NULL AS Expr3,
C.DecWaterType, C.CoalGrade, C.OtherType, NULL AS OtherTypeEx, C.OtherName, NULL AS OtherNameEx, '大石' AS BigStoneName, '小石' AS SmallStoneName, NULL AS GritRatio, S.Cubage, S.LandSlide,
NULL AS Ya3d, NULL AS Yz3d, T.Ya AS Ya7d, T.Yz AS Yz7d, E.Ya AS Ya28d, E.Yz, CASE WHEN C.LandSlideMethod = 0 THEN '(mm)' ELSE '(S)' END AS LandSlideEx
FROM dbo.CementTest31 AS C INNER JOIN
dbo.TableRecord AS R ON R.TableID = 35331 AND C.SelfID = R.RecordID AND (ISNULL(R.IsDel,0) <> 1) INNER JOIN
dbo.CementTest31S AS S ON C.SelfID = S.JoinID AND C.SelNo = S.ToneNo LEFT OUTER JOIN
(SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.CementTest32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJ70_90_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJT70_2009_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJT98_2010_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1) AS T ON T.RecordID = C.SelfID AND T.ToneNo = C.SelNo AND T.AgeDay = 7 LEFT OUTER JOIN
(SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.CementTest32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJ70_90_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJT70_2009_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJT98_2010_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1) AS E ON E.RecordID = C.SelfID AND E.ToneNo = C.SelNo AND E.AgeDay = 28
这是视图中其中的一段,这个视图是由多个类似这样的语句块通过UNION ALL连接的。
还请指点!
#5
嗯,所以我想了解下关于这两个关键词区别、使用、效率的比较详细的解说
#6
个人感受在多表时Union All 比Union快,但是单表时Union 则快
#7
如果这个效率是由于其他原因引起的,也请一并指点下,谢谢!
#8
这应该分区视图玩法,不过楼主写的有问题,
可以考虑吧把CementTest32 ,JGJ70_90_32,JGJT70_2009_32,JGJT98_2010_32 写成一个视图v
再写语句:
Select ... ...
FROM dbo.CementTest31 AS C
INNER JOIN dbo.TableRecord AS R ON R.TableID = 35331
AND C.SelfID = R.RecordID
AND ( ISNULL(R.IsDel, 0) <> 1 )
INNER JOIN dbo.CementTest31S AS S ON C.SelfID = S.JoinID
AND C.SelNo = S.ToneNo
Left Outer Join [视图v] As T On t.SelfID=R. ...
Left Outer Join [视图v] As E On t.SelfID=R. ...
根据执行计划来判断效率慢的位置,如何效率慢问题出现在[视图v]上,可以考虑对做成静态视图。
可以考虑吧把CementTest32 ,JGJ70_90_32,JGJT70_2009_32,JGJT98_2010_32 写成一个视图v
再写语句:
Select ... ...
FROM dbo.CementTest31 AS C
INNER JOIN dbo.TableRecord AS R ON R.TableID = 35331
AND C.SelfID = R.RecordID
AND ( ISNULL(R.IsDel, 0) <> 1 )
INNER JOIN dbo.CementTest31S AS S ON C.SelfID = S.JoinID
AND C.SelNo = S.ToneNo
Left Outer Join [视图v] As T On t.SelfID=R. ...
Left Outer Join [视图v] As E On t.SelfID=R. ...
根据执行计划来判断效率慢的位置,如何效率慢问题出现在[视图v]上,可以考虑对做成静态视图。
#1
帮顶!关注!!!(个人感觉Union ALL效率高些吧,毕竟他不需要排除重复的记录!!!也不能概论吧!哈哈)
#2
具体情况具体分析。不知道你的UNION ALL语句是什么样子的。
#3
不知道你是怎么测试的
1、工鞥不一样,返回的结果都可能不一样,有啥好比较的
2、就算你两个select 不会重复,union 也比union all 多了一个合并的操作
当然要慢了
1、工鞥不一样,返回的结果都可能不一样,有啥好比较的
2、就算你两个select 不会重复,union 也比union all 多了一个合并的操作
当然要慢了
#4
SELECT R.TableID, R.TableNumber, R.SampNo, C.Design, C.KZ_Design, NULL AS Expr1, S.Cement, S.Grit, NULL AS Expr2, NULL AS StoneDiameter, S.Water, S.DecWater,
S.Coal, S.Other, NULL AS OtherEx, R.UseArea, R.proj_id, R.UnitID, R.UnitName, R.RecordID, R.CheckDate, C.CementSign, C.GritDiameter, NULL AS Stone1Diameter, NULL AS Expr3,
C.DecWaterType, C.CoalGrade, C.OtherType, NULL AS OtherTypeEx, C.OtherName, NULL AS OtherNameEx, '大石' AS BigStoneName, '小石' AS SmallStoneName, NULL AS GritRatio, S.Cubage, S.LandSlide,
NULL AS Ya3d, NULL AS Yz3d, T.Ya AS Ya7d, T.Yz AS Yz7d, E.Ya AS Ya28d, E.Yz, CASE WHEN C.LandSlideMethod = 0 THEN '(mm)' ELSE '(S)' END AS LandSlideEx
FROM dbo.CementTest31 AS C INNER JOIN
dbo.TableRecord AS R ON R.TableID = 35331 AND C.SelfID = R.RecordID AND (ISNULL(R.IsDel,0) <> 1) INNER JOIN
dbo.CementTest31S AS S ON C.SelfID = S.JoinID AND C.SelNo = S.ToneNo LEFT OUTER JOIN
(SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.CementTest32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJ70_90_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJT70_2009_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJT98_2010_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1) AS T ON T.RecordID = C.SelfID AND T.ToneNo = C.SelNo AND T.AgeDay = 7 LEFT OUTER JOIN
(SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.CementTest32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJ70_90_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJT70_2009_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1
UNION
SELECT Ya, Yz, T1.RecordID, ToneNo, AgeDay FROM dbo.JGJT98_2010_32 T1
INNER JOIN dbo.TableRecord R1 ON T1.SelfID = R1.RecordID AND R1.IsDel <> 1) AS E ON E.RecordID = C.SelfID AND E.ToneNo = C.SelNo AND E.AgeDay = 28
这是视图中其中的一段,这个视图是由多个类似这样的语句块通过UNION ALL连接的。
还请指点!
#5
嗯,所以我想了解下关于这两个关键词区别、使用、效率的比较详细的解说
#6
个人感受在多表时Union All 比Union快,但是单表时Union 则快
#7
如果这个效率是由于其他原因引起的,也请一并指点下,谢谢!
#8
这应该分区视图玩法,不过楼主写的有问题,
可以考虑吧把CementTest32 ,JGJ70_90_32,JGJT70_2009_32,JGJT98_2010_32 写成一个视图v
再写语句:
Select ... ...
FROM dbo.CementTest31 AS C
INNER JOIN dbo.TableRecord AS R ON R.TableID = 35331
AND C.SelfID = R.RecordID
AND ( ISNULL(R.IsDel, 0) <> 1 )
INNER JOIN dbo.CementTest31S AS S ON C.SelfID = S.JoinID
AND C.SelNo = S.ToneNo
Left Outer Join [视图v] As T On t.SelfID=R. ...
Left Outer Join [视图v] As E On t.SelfID=R. ...
根据执行计划来判断效率慢的位置,如何效率慢问题出现在[视图v]上,可以考虑对做成静态视图。
可以考虑吧把CementTest32 ,JGJ70_90_32,JGJT70_2009_32,JGJT98_2010_32 写成一个视图v
再写语句:
Select ... ...
FROM dbo.CementTest31 AS C
INNER JOIN dbo.TableRecord AS R ON R.TableID = 35331
AND C.SelfID = R.RecordID
AND ( ISNULL(R.IsDel, 0) <> 1 )
INNER JOIN dbo.CementTest31S AS S ON C.SelfID = S.JoinID
AND C.SelNo = S.ToneNo
Left Outer Join [视图v] As T On t.SelfID=R. ...
Left Outer Join [视图v] As E On t.SelfID=R. ...
根据执行计划来判断效率慢的位置,如何效率慢问题出现在[视图v]上,可以考虑对做成静态视图。