积极答复者
Can I use "union" instead of "or"

问题
-
Today When I optimize the sql, I encounter a problem. The Sql is following:
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId is null
union
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId = dbo.UF_GetParentOUId(d.OUID)I run this sql and it spends about 3 senconds, and then I modify it as follow:
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId is null or c.OUId = dbo.UF_GetParentOUId(d.OUID)and What happen? It takes me about 12 minutes.
I don't know Why they are so different?
Thank you for everyone tell me the reason.
郭勇成
答案
-
就你提供的信息无法判断应该不应该替换,把你执行的计划贴上来看看 你把union ->union all 省了排序更快Today When I optimize the sql, I encounter a problem. The Sql is following:
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId is null
union
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId = dbo.UF_GetParentOUId(d.OUID)I run this sql and it spends about 3 senconds, and then I modify it as follow:
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId is null or c.OUId = dbo.UF_GetParentOUId(d.OUID)and What happen? It takes me about 12 minutes.
I don't know Why they are so different?
Thank you for everyone tell me the reason.
郭勇成
More: blog.csdn.net/happyflystone- 已标记为答案 中國風MVP, Moderator 2010年1月25日 2:16
全部回复
-
就你提供的信息无法判断应该不应该替换,把你执行的计划贴上来看看 你把union ->union all 省了排序更快Today When I optimize the sql, I encounter a problem. The Sql is following:
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId is null
union
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId = dbo.UF_GetParentOUId(d.OUID)I run this sql and it spends about 3 senconds, and then I modify it as follow:
select d.OUID,c.Id,d.ArchiveDate,GETDATE(),0,0,0
from c
join d on c.OUId is null or c.OUId = dbo.UF_GetParentOUId(d.OUID)and What happen? It takes me about 12 minutes.
I don't know Why they are so different?
Thank you for everyone tell me the reason.
郭勇成
More: blog.csdn.net/happyflystone- 已标记为答案 中國風MVP, Moderator 2010年1月25日 2:16