SQL Server中联合的任何替代方案?

时间:2018-02-12 16:09:56

标签: sql 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秒),而不是在一分钟内加载。我已经和这段代码争吵了一段时间了,我已经准备好让它高效工作了。

我试图想想是否有办法做最后一次连接(在联合的最后一部分的a.UI = g.OID上的内部联接EntData g,而不是必须处理完全独立的SELECT语句)只有当用户界面等于1414时,我才认为这是可能的。我尝试将最后一次连接实现到第一个SELECT中,但它没有运行。我仍然是SQL的新手,所以任何帮助都会非常感激。

谢谢。

2 个答案:

答案 0 :(得分:1)

您的联合查询几乎相同。尝试加入它们并从中进行一次查询。我认为这个查询应该返回相同的结果。但如果存在,则会包含重复的行。

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

答案 1 :(得分:1)

我重构并添加了一些笔记。

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过滤查询的数据量是多少,因此将其排除到最后可能会导致主查询中的数据集大得多。