繁体   English   中英

SQL Server中的工会有什么替代选择吗?

[英]Any Alternatives to Unions in SQL Server?

因此,我正在创建一个查询,该查询将带回我需要的所有数据。

这是我的查询:

Declare @StartDate datetime 
Set @StartDate = '2/1/2018'

Declare @EndDate datetime 
Set @EndDate = '4/5/2018'

Declare @UserID int
Set @UserId = '153056'

;with EntData as
(
select 
    distinct (Entity_ID), a.user_ID, c.User_OrganizationalUnitID 
from 
    ViewMgmt as a
    ViewConsole b on a.Role_ID = b.RoleID
    ViewUsers c on a.User_ID = c.UserID
where 
    b.RoleID in ( 53354666, 5363960) and 
    a.User_ID = @UserID  and
    a.Entity_ID <> 6912276036227
)

select a.User_ID, a.User_Name, a.UOName, 
    b.C_OID, c.OName,
    d.CID, e.Affected
from view.a
    inner join view_Cool.a1 on a.User_ID = a1.UserID and a.CI_D = a1.CID
    inner join view_New.b on a.CI_D = b.C_ID
    left join view_Large.c on b.C_OID = c.OID
    left join view_Small.d on a.CI_D = d.CID
    left join view_Old.e on d.Cert_ID = e.CI_D and a.User_ID = e.User_ID
    inner join EntData on b.C_OID = EntityData.Entity_ID
where ((a.ExpirationDate between @StartDate and @EndDate and a.ExpirationDate = a.ExpirationDate) 
    or (a.ExpirationDate between @StartDate and @EndDate and a.ExpirationDate is null)) and (a.UI <> 6912276036227 or a.UI <> 1414)
    and a1.IsHidden = 0 and a.UCS <> 13

UNION 

select a.User_ID, a.User_Name, a.UOName, 
    b.C_OID, c.OName,
    d.CID, e.Affected
from view.a
    inner join view_Cool.a1 on a.User_ID = a1.UserID and a.CI_D = a1.CID
    inner join view_New.b on a.CI_D = b.C_ID
    left join view_Large.c on b.C_OID = c.OID
    left join view_Small.d on a.CI_D = d.CID
    left join view_Old.e on d.Cert_ID = e.CI_D and a.User_ID = e.User_ID
    inner join EntData g on a.UI = g.OID
where ((a.ExpirationDate between @StartDate and @EndDate and a.ExpirationDate = a.ExpirationDate) 
    or (a.ExpirationDate between @StartDate and @EndDate and a.ExpirationDate is null)) and a.UI <> 6912276036227 
    and a.UI = 1414 
    and (c.OName like '%VXA%' or c.OName like '%Amazon%' or a.CI_D in (414,4561))
    and a1.Hidden = 0 and a.UCS <> 13

最初,联合的第一部分就是我所拥有的全部,但是有人想要特定地查看一个ID的额外数据(a.UI = 1414)。 我不想为系统中的所有UI都带回更多数据,所以我做了一个联合,专门在一个UI中带回了额外的数据。 我想要的数据又回来了; 但是,现在,查询可能要花费4分钟以上的时间才能加载,而不是在一分钟内加载(第一条SELECT语句需要30-40秒)。 我一直在努力处理这段代码,现在已经准备好使其有效地工作。

我试图考虑是否有一种方法可以仅在以下情况下进行最后一次连接(在联合的最后部分进行内部连接EntData g在a.UI = g.OID上,而不是必须使用完全独立的SELECT语句)用户界面等于1414,但我认为这是不可能的。 我尝试将最后一个联接实现到第一个SELECT中,但是没有运行。 我还是SQL的新手,因此非常感谢您的帮助。

谢谢。

您的联合查询几乎相同。 尝试加入他们并向他们提出一个查询。 我认为此查询应返回相同的结果。 但如果有的话,将包括重复的行。

select a.User_ID, a.User_Name, a.UOName, 
    b.C_OID, c.OName,
    d.CID, e.Affected
from view.a
    inner join view_Cool.a1 on a.User_ID = a1.UserID and a.CI_D = a1.CID
    inner join view_New.b on a.CI_D = b.C_ID
    left join view_Large.c on b.C_OID = c.OID
    left join view_Small.d on a.CI_D = d.CID
    left join view_Old.e on d.Cert_ID = e.CI_D and a.User_ID = e.User_ID
    inner join EntData on b.C_OID = EntityData.Entity_ID
where ((a.ExpirationDate between @StartDate and @EndDate and a.ExpirationDate = a.ExpirationDate) 
    or (a.ExpirationDate between @StartDate and @EndDate and a.ExpirationDate is null)) and (a.UI <> 6912276036227)
    and a1.IsHidden = 0 and a.UCS <> 13

    and 1 = case 
                when a.UI = 1414 
                    case
                        when c.OName like '%VXA%' or c.OName like '%Amazon%' or a.CI_D in (414,4561) then 1
                        else 0 
                    end
                else 1
            end

我重构并添加了一些注释。

DECLARE @StartDate datetime 
  SET @StartDate = '2018-02-01'  --- Use ISO 8601 dates. YYYY-MM-DD
DECLARE @EndDate datetime 
  SET @EndDate = '2018-04-05'  --- Use ISO 8601 dates. YYYY-MM-DD
DECLARE @UserID int
  SET @UserId = 153056  --- Remove single quotes. You're assigning a string to an int. 

; WITH EntData AS (
    SELECT DISTINCT Entity_ID , OID /* This needs to be included for later JOIN. Remove the other columns you don't use. */
    FROM ViewMgmt a  --- USE ANSI-92 syntax. Pretty please.
    INNER JOIN ViewConsole b on a.Role_ID = b.RoleID
        AND b.RoleID in ( 53354666, 5363960 )
    INNER JOIN ViewUsers c on a.User_ID = c.UserID
    WHERE  
        a.User_ID = @UserID  
        AND a.Entity_ID <> 6912276036227
)
, UnionedQueries AS ( -- Combine the common parts of the UNIONed queries into a second CTE for reuse.
    SELECT a.User_ID, a.User_Name, a.UOName
        , b.C_OID
        , c.OName
        , d.CID
        , e.Affected
        , a.UI  -- Added for UNION
        , a.CI_D -- Added for UNION
    FROM view.a a
    INNER JOIN view_Cool.a1 a2 ON a.User_ID = a1.UserID 
        AND a.CI_D = a1.CID
        AND a1.IsHidden = 0  --- Move this filter into the INNER JOIN. It will reduce the JOINed resultset.
    INNER JOIN view_New.b b ON a.CI_D = b.C_ID
        LEFT JOIN view_Large.c c ON b.C_OID = c.OID

    /* These JOINs are connecting across multiple tables. Make sure it's returning what you think it is how it should be. */
    LEFT JOIN view_Small.d d ON a.CI_D = d.CID 
    LEFT JOIN view_Old.e e ON d.Cert_ID = e.CI_D 
            AND a.User_ID = e.User_ID

    WHERE (
            ( a.ExpirationDate BETWEEN @StartDate AND @EndDate ) /* ??? and a.ExpirationDate = a.ExpirationDate ??? Typo? What was this supposed to do? */ 
            OR 
            ( a.ExpirationDate IS NULL ) --If this is checking for NULL, it won't be BETWEEN @StartDate and @EndDate
            --- These two conditions could be combined as ISNULL(a.ExpirationDate,@StartDate), but that is very micro-optimization.
        ) 
        and a.UI NOT IN ( 6912276036227, 1414 )  -- This is functionally the same as using two <>s, just easier to follow.
        and a.UCS <> 13
)
/* Now that the common query is already run, we can just use those results to get our final UNION */
SELECT u1.User_ID, u1.User_Name, u1.UOName, u1.C_OID, u1.OName, u1.CID, u1.Affected
FROM UnionedQueries u1
INNER JOIN EntData ON u1.C_OID = EntData.Entity_ID  -- This JOIN seems to be the only significant difference between the two queries.

UNION

SELECT u2.User_ID, u2.User_Name, u2.UOName, u2.C_OID, u2.OName, u2.CID, u2.Affected
FROM UnionedQueries u2
INNER JOIN EntData g on u2.UI = g.OID -- This JOIN seems to be the only significant difference between the two queries.
WHERE u2.UI = 1414 
    AND ( 
        u2.OName LIKE '%VXA%' 
        OR u2.OName LIKE '%Amazon%' 
        OR u2.CI_D IN ( 414,4561 )
    )
;

注意:这将需要测试。 我不知道EntData CTE会过滤查询多少数据,因此将其排除到最后可能会导致主要查询中的数据集更大。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM