[英]postgres - How to efficiently find if a matching pair exists for the given tables
items
id | name | group
-----------------------------------------------------------
1 | /foo/ | app1
2 | /foo/1 | app1
3 | /bar/2 | app1
4 | /foo/abc/def | app1
5 | /foo3/ | app2
6 | /bar3/ | app2
mapping
id_a | id_b
--------------
1 | 2
3 | 4
mapping.id_a and mapping.id_b have foreign key references to item.id
我需要使用 gino sqlalchemy 查找给定输入“名称”和“组”的映射表中 id_a 和 id_b 之间是否存在关联。
例子:
使用连接将是微不足道的:
SELECT TRUE
FROM items AS i1
JOIN mapping AS m ON i1.id = m.id_a
JOIN items AS i2 ON m.id_b = i2.id
WHERE i1.name = '/foo/'
AND i2.name = '/foo/1' AND i2.group = 'app1';
如果这返回一行,则存在匹配项。
为了获得良好的性能,请创建以下两个索引(如果它们尚不存在):
CREATE INDEX ON item (name, group);
CREATE INDEX ON mapping (id_b);
然后你应该得到一个有效的嵌套循环连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.