graph - 为什么我的 Gremlin 查询会导致这么多请求?这是正确的行为吗?
问题描述
我正在尝试调试我在使用 AWS Neptune 时遇到的性能问题。我正在运行一些 Gremlin 查询,它们似乎总是在数据库上产生 30 个请求。我想知道我的查询是否做错了什么。
这个问题的奇怪之处在于它是突然发生的。以前,这一切正常,我们没有遇到性能问题。
我所做的每个调用都有两个通用查询,一个用于节点,一个用于边缘:
nodes = g.V(id).emit().repeat(__.out('manages')).dedup().project('label', 'name', 'job', 'department', 'manager').\
by(__.id()).by('name').by('job').by('department').by('manager').toList()
id_list = list(map(lambda node: node["label"], nodes))
edges = g.V(id).emit().repeat(__.out('manages')).dedup().bothE('similar_to').dedup().\
where(__.and_(__.inV().has(T.id, P.within(id_list)), __.outV().has(T.id, P.within(id_list)))).\
project('from', 'to', 'similarity').by(__.outV().id()).by(__.inV().id()).by('similarity').toList()
本质上,我有两种边缘类型:manages 和similar_to。我尝试使用“管理”边缘创建一棵树,然后在该树中找到所有“相似”边缘。
这个查询给出了想要的结果,但它是否未优化?
解决方案
两个遍历都遵循几乎相同的路径,这使得它们很容易组合:
g.V(id).
emit().
repeat(__.out('manages')).
aggregate('x').
bothE('similar_to').dedup().
filter(__.otherV().where(P.within('x'))).
project('from', 'to', 'similarity').
by(__.outV().id()).
by(__.inV().id()).
by('similarity').
toList()
现在我才意识到我们可以让它变得更简单。由于您要求由 连接的两个顶点都similar_to
属于x
,这意味着结果中的每条边都必须是 中任何顶点的出边x
。因此,我们可以只使用and ,而不是使用bothE
and otherV
(启用路径跟踪):outE
inV
g.V(id).
emit().
repeat(__.out('manages')).
aggregate('x').
outE('similar_to').dedup().
filter(__.inV().where(P.within('x'))). /* outV is already guaranteed to be within "x" */
project('from', 'to', 'similarity').
by(__.outV().id()).
by(__.inV().id()).
by('similarity').
toList()
推荐阅读
- css - 如何在 JavaFX CSS 中添加过渡到悬停效果?
- job-scheduling - CircleCI config.yml 中的错误
- django-forms - 在同一页面上添加和编辑评论
- python - 有没有办法在多个值上运行 np.where 而不仅仅是一个?
- algorithm - 我无法解决的算法,但为什么呢?
- tableau-api - 我正在尝试过滤表计算。尝试过滤表格计算时,总计不会改变。它的静态
- bash - 用于获取平均收视率最高的前 5 个电视节目的 shell 脚本。对于相同的评分,请选择最先出现的评分
- git - 将单个 git 提交(来自单独的分支)合并到多个单独的拉取请求中
- python - 如何在日期中的空格之间添加/
- r - 由于错误“库中的错误(MVN):没有名为'MVN'的包”,无法在 RStudio 中使用 MVN 库