如何编写更优化和简单的查询 - Parent Child 关系?
How to write a more optimized and simple query - Parent Child Relation?
我有一个 table 的医疗数据有关系(类似于 parent 和 child)。
为了简单起见,我考虑了具有实际 parent-child 关系的 table。
下面是 table:
Table 姓名:关系
Parent | Child
------ | ------
Mike |John
Aliss |John
John |Chris
Brad |David
Kate |Brad
Alexa |Shawn
Matt |Thoa
我已经编写了获取 GrandParent、Parent 和 Grandchild 关系的查询。
SELECT t1.grandchild,
t2.grandparent,
t1.parent,
t2.child
FROM (SELECT child AS Grandchild,
parent
FROM relations
WHERE parent IN (SELECT DISTINCT( r.parent )
FROM relations r
JOIN relations t
ON r.parent = t.child)) AS t1
INNER JOIN (SELECT parent AS Grandparent,
child
FROM relations
WHERE child IN (SELECT DISTINCT( r.parent )
FROM relations r
JOIN relations t
ON r.parent = t.child)) AS t2
ON t1.parent = t2.child
ORDER BY t1.grandchild;
这里有问题,现在实际数据有 30015924 行,当我 运行 使用上述查询的报告时,它需要很长时间才能获取数据。
我看到了执行计划,有很多 "Nested Loops" 和 Lazy 假脱机。
我正在尝试编写一个更高效的查询,它可以更快地在大型数据集上执行。
单独关系的联盟是否有效。
这是我写过的最有效的查询还是有更好的版本?
谢谢。
我经常发现,与使用嵌套派生查询相比,使用联合可以极大地加快查询时间。
这是一个更简单(并且可能在性能方面可能更好)的查询,以获得完全相同的结果:
首先,创建并填充示例数据(请在您以后的问题中省去这一步:
CREATE TABLE relations
(
Parent varchar(10),
Child varchar(10)
)
INSERT INTO relations VALUES
('Mike', 'John'),
('Aliss', 'John'),
('John', 'Chris'),
('Brad', 'David'),
('Kate', 'Brad'),
('Alexa', 'Shawn'),
('Matt', 'Thoa')
查询:
SELECT sg.child as grandchild,
fg.Parent as grandparent,
fg.child as parent,
sg.Parent as parent
FROM relations as fg -- stands for first generation
INNER JOIN
(
SELECT parent, child
FROM relations
) as sg ON fg.child = sg.parent -- second generation
结果:
grandchild grandparent parent parent
Chris Mike John John
Chris Aliss John John
David Kate Brad Brad
See a live demo on rextester(我也将您的查询粘贴到此处以比较结果。)
我有一个 table 的医疗数据有关系(类似于 parent 和 child)。 为了简单起见,我考虑了具有实际 parent-child 关系的 table。 下面是 table:
Table 姓名:关系
Parent | Child
------ | ------
Mike |John
Aliss |John
John |Chris
Brad |David
Kate |Brad
Alexa |Shawn
Matt |Thoa
我已经编写了获取 GrandParent、Parent 和 Grandchild 关系的查询。
SELECT t1.grandchild,
t2.grandparent,
t1.parent,
t2.child
FROM (SELECT child AS Grandchild,
parent
FROM relations
WHERE parent IN (SELECT DISTINCT( r.parent )
FROM relations r
JOIN relations t
ON r.parent = t.child)) AS t1
INNER JOIN (SELECT parent AS Grandparent,
child
FROM relations
WHERE child IN (SELECT DISTINCT( r.parent )
FROM relations r
JOIN relations t
ON r.parent = t.child)) AS t2
ON t1.parent = t2.child
ORDER BY t1.grandchild;
这里有问题,现在实际数据有 30015924 行,当我 运行 使用上述查询的报告时,它需要很长时间才能获取数据。
我看到了执行计划,有很多 "Nested Loops" 和 Lazy 假脱机。 我正在尝试编写一个更高效的查询,它可以更快地在大型数据集上执行。
单独关系的联盟是否有效。 这是我写过的最有效的查询还是有更好的版本?
谢谢。
我经常发现,与使用嵌套派生查询相比,使用联合可以极大地加快查询时间。
这是一个更简单(并且可能在性能方面可能更好)的查询,以获得完全相同的结果:
首先,创建并填充示例数据(请在您以后的问题中省去这一步:
CREATE TABLE relations
(
Parent varchar(10),
Child varchar(10)
)
INSERT INTO relations VALUES
('Mike', 'John'),
('Aliss', 'John'),
('John', 'Chris'),
('Brad', 'David'),
('Kate', 'Brad'),
('Alexa', 'Shawn'),
('Matt', 'Thoa')
查询:
SELECT sg.child as grandchild,
fg.Parent as grandparent,
fg.child as parent,
sg.Parent as parent
FROM relations as fg -- stands for first generation
INNER JOIN
(
SELECT parent, child
FROM relations
) as sg ON fg.child = sg.parent -- second generation
结果:
grandchild grandparent parent parent
Chris Mike John John
Chris Aliss John John
David Kate Brad Brad
See a live demo on rextester(我也将您的查询粘贴到此处以比较结果。)