代码之家  ›  专栏  ›  技术社区  ›  Ross Bush

缩减数据集相对于大型数据集的总体顺序

  •  0
  • Ross Bush  · 技术社区  · 2 年前

    在一个过程中使用了一种方案来创建数据集中从一天到下一天的变化增量。查询需要在视图的整个结果集中获得记录的总体顺序,同时只返回记录的子集。我想,这一切都很顺利,直到一个丑陋的视图推送了应用程序的30秒ado超时。我原以为解决这个问题很容易,但事实证明这是无所畏惧的。视图使用的表有数百万行,但索引是保持不变的。

    Query1-只返回视图上数百万条记录的2个键和row_number,其中有一堆左联接可能会被优化掉。(我不得不将其从select*改为row_number,因为*是问题所在)

    查询2- 根据25个ID的键列表加入大视图,然后加入具有总数据偏移量的Query1。

    查询3 删除连接两个数据集的重复项。

    DECLARE @KeysToSelect TABLE(Key1 INT)
    INSERT INTO @KeysToSelect VALUES(139743),(139878),(139953)
    
    DECLARE @BigView TABLE(Key1 INT, Key2 INT, Area DECIMAL(18,2), BuildingType NVARCHAR(25))
    INSERT @BigView VALUES
    (100, NULL, 0,''),
    (101, NULL, 0,''),
    (200, NULL, 0,''),
    (201, NULL, 0,''),
    (139743, NULL, 8475.00,'Industrial'),
    (139743, NULL, 593.00,  'Office'),
    (139744, NULL, 0,''),
    (139745, NULL, 0,''),
    (139746, NULL, 0,''),
    (139747, NULL, 0,''),
    (139878, NULL, 1268.00,'Office'),
    (139878, NULL, 15534.00,'Warehouse'),
    (139879, NULL, 0,''),
    (139880, NULL, 0,''),
    (139881, NULL, 0,''),
    (139953, 6, 20000.00,'Warehouse'),
    (139953, 14,20000.00,'Office'),
    (149956, NULL, 0,''),
    (149957, NULL, 0,''),
    (149958, NULL, 0,'')
    
    ;WITH OveralOrderInData AS
    (
        SELECT
            --!!!! Cant't SELECT * FROM @BigView Here because it hits a 30 second timeout limit.  This is what the solve is for. 
            --It would be easy just to return the data with a rank, however, in this case going into a skinny slice of the data to get overall count
            --and joining that against a limited subset of the larger viuew returns in under s second
            --Grabbing the 2 keys and ranking takes less than a second, lots of left joins and views calling views
            ROW_NUMBER() OVER (ORDER BY vw.Key1 ASC, vw.Key2 ASC) AS OrderInData,
            CASE WHEN Keys.Key1 IS NULL THEN NULL ELSE SUM(CASE WHEN Keys.Key1 IS NULL THEN NULL ELSE 1 END) OVER (ORDER BY vw.Key2 ASC,vw.Key1 ASC ROWS UNBOUNDED PRECEDING) END AS MatchedOrderInSearchKeys,
            CASE WHEN Keys.Key1 IS NULL THEN 0 ELSE 1 END AS IsMatched,
            vw.Key1,
            vw.Key2
        FROM
            @BigView vw
            LEFT OUTER JOIN @KeysToSelect Keys ON Keys.Key1 = vw.Key1
    )
    ,Normalized AS
    (
        --Second dive into the data, this time, we can filter records based on a very selected Key list and join back our overall order data
        --this can cause doubles and quadruples. Basically, a count will be associated with each duplicate Key1 and Key 2 value.
        --somehow turn these over and rank the duplicates as 1 and 2 and 1 and 2 as opposed to 1 and 1 and 1 and 1 :(
        SELECT
            O.OrderInData,
            O.MatchedOrderInSearchKeys,
            O.Key1,
            O.Key2,
            vw.Area , 
            vw.BuildingType,    
            DENSE_RANK() OVER(PARTITION BY O.Key1 ,O.Key2 ORDER BY OrderInData) AS DistributeOrder, 
            *
        FROM
            OveralOrderInData O
            INNER JOIN @BigView vw ON vw.Key1 = O.Key1 AND (vw.Key2 = O.Key2 OR O.Key2 IS NULL)
        WHERE
            O.IsMatched = 1
    )
    SELECT 
        * 
    FROM 
        Normalized
    WHERE   
        DistributeOrder = 1
    

    上面的查询返回的数据中,实现了为Key1和Key2的重复项找到的第一个OrderInData,如下所示:

    订单输入数据 键1 键2 地区 建筑类型
    5. 139743 无效的 8475 工业的
    5. 139743 无效的 593 办公室
    11 139878 无效的 1268 办公室
    11 139878 无效的 15534 仓库
    16 139953 6. 20000 仓库
    17 139953 14 20000 办公室

    所需输出

    订单输入数据 键1 键2 地区 建筑类型
    5. 139743 无效的 8475 工业的
    6. 139743 无效的 593 办公室
    11 139878 无效的 1268 办公室
    12 139878 无效的 15534 仓库
    16 139953 6. 20000 仓库
    17 139953 14 20000 办公室

    我的下一步行动是在子查询中创建虚拟化表,其中包含针对键中项目的不同顺序,然后再次连接,然而,这将需要另外两个子查询,这应该是一个快速解决方案。在这一点上,有没有我愚蠢到看不见的银弹?

    1 回复  |  直到 2 年前
        1
  •  1
  •   testing-for-ya    2 年前

    在不尝试对之前的任何内容进行反向工程的情况下,最终选择可以只具有来源于行号的表达式:

    SELECT
        RealOrderInData = OrderInData - 1
          + ROW_NUMBER() OVER (PARTITION BY MatchedOrderInSearchKeys 
            ORDER BY BuildingType), -- should pick something more deterministic
        * 
    FROM 
        Normalized
    WHERE   
        DistributeOrder = 1;
    

    查询的其余部分很可能会被合并/简化,但这在晚上是一个艰巨的任务。:-)