AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 96098
Accepted
Eric S
Eric S
Asked: 2015-03-25 09:09:35 +0800 CST2015-03-25 09:09:35 +0800 CST 2015-03-25 09:09:35 +0800 CST

以最佳方式找到关系链的末端

  • 772

我有一个包含 id 列、父列和子列的表。因此,对于每条记录,我知道在链关系中它之前是什么记录,之后是什么记录,但是从单个记录中,我不知道它在链中的什么位置,也不知道它的第一部分或最后一部分是什么连锁,链条。

出于演示目的,假设我们有以下设置。我意识到这种设置存在许多设计问题,但这是我必须解决的问题。

CREATE TABLE Relationships (
ID VARCHAR(4), 
ParentID VARCHAR(4),
ChildID VARCHAR(4)
)

-- Insert root entries with their children
INSERT INTO Relationships (ID, ParentID, ChildID)
VALUES ('0001', '', '0003'), ('0002', '', '0004')

-- Now add further entries for each relationship chain
INSERT INTO Relationships(ID, ParentID, ChildID)
VALUES('0003', '0001', '0005'), ('0005', '0003', '0006'), ('0006', '0005', '0007'), ('0007', '0006', ''), 
  ('0004', '0002', '')

--Now we have two chains of 0001 -> 0003 -> 0005 -> 0006 -> 0007 and 0002 ->   0004

通过像下面这样的递归 CTE,我可以找到所有记录在它们的链中是如何相关的以及它们在链中的位置。

WITH RelationshipChain AS (
SELECT ID, ParentID, ChildID, 0 AS Seq, ID AS RootID
FROM Relationships WHERE ParentID = ''
UNION ALL
SELECT r2.ID, r2.ParentID, r2.ChildID, rc.Seq + 1 AS Seq, rc.RootID AS RootID
FROM Relationships r2 
INNER JOIN RelationshipChain rc ON rc.ChildID = r2.ID 
)

SELECT * FROM RelationshipChain
ORDER BY RootID, Seq

对于大约 200 万条记录,这在大约 30 秒内运行,这非常好,但是如果我尝试还包括链的最后部分,则运行时间是 4 倍。目前,我正在这样做:

WITH RelationshipChain AS (
SELECT ID, ParentID, ChildID, 0 AS Seq, ID AS RootID
FROM Relationships WHERE ParentID = ''
UNION ALL
SELECT r2.ID, r2.ParentID, r2.ChildID, rc.Seq + 1 AS Seq, rc.RootID AS RootID
FROM Relationships r2 
INNER JOIN RelationshipChain rc ON rc.ChildID = r2.ID 
)

SELECT * 
FROM RelationshipChain rc
CROSS APPLY (
SELECT MAX(Seq) AS FinalSeq FROM RelationshipChain WHERE rc.RootID = RootID
) AS b
CROSS APPLY (
SELECT ID AS LastChild FROM RelationshipChain WHERE b.FinalSeq = seq AND rc.RootID = RootID
) AS c
ORDER BY RootID, Seq

有没有办法更有效地做到这一点?

sql-server performance
  • 1 1 个回答
  • 2030 Views

1 个回答

  • Voted
  1. Best Answer
    AakashM
    2015-03-26T01:06:17+08:002015-03-26T01:06:17+08:00

    我没有你的数据集,所以无法测试这是否更好,但感觉更好。构建链后,我们将它们反转以在每个链中找到“childest”项目,然后重新加入原始链。

    -- The same as your first CTE
    ;WITH RelationshipChain AS (
    SELECT ID, ParentID, ChildID, 0 AS Seq, ID AS RootID
    FROM Relationships WHERE ParentID = ''
    UNION ALL
    SELECT r2.ID, r2.ParentID, r2.ChildID, rc.Seq + 1 AS Seq, rc.RootID AS RootID
    FROM Relationships r2 
    INNER JOIN RelationshipChain rc ON rc.ChildID = r2.ID 
    )
    
    -- This CTE is new and reverses the sense of the chains
    , BackChain AS (
    SELECT ID, RootID, ROW_NUMBER() OVER (PARTITION BY RootID ORDER BY Seq DESC) BackSeq
    FROM RelationshipChain
    )
    
    -- Now we join each chain to the childest item in the reverse chain
    SELECT 
        rc.ID
        , rc.ParentID
        , rc.ChildID
        , rc.Seq
        , rc.RootID
        , bc.ID ChainEndID
    FROM RelationshipChain rc INNER JOIN BackChain bc ON RC.RootID = bc.RootID AND bc.BackSeq = 1
    ORDER BY RootID, Seq
    

    样本数据的结果:

    ID   ParentID ChildID Seq         RootID ChainEndID
    ---- -------- ------- ----------- ------ ----------
    0001          0003    0           0001   0007
    0003 0001     0005    1           0001   0007
    0005 0003     0006    2           0001   0007
    0006 0005     0007    3           0001   0007
    0007 0006             4           0001   0007
    0002          0004    0           0002   0004
    0004 0002             1           0002   0004
    

    索引将有助于提高性能。

    • 2

相关问题

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve