首页 > 解决方案 > 在 AWS Neptune 上的 50M 边缘响应时间较短的 Gremlin

问题描述

我正在用 gremlin 实现一个推荐系统。我有 200 万用户和 2 万本书。当用户购买一本书时,它会为这本书创造优势。我在图上总共有 50M 边。我正在使用基本查询,但运行速度太慢。我打算实时给出回应。

query = graph.V().hasLabel('user').hasId(user_id).match(
    _.as('u1').out().hasLabel('book').dedup().fold().as_('u1_books'),
    _.as('u1').V().hasLabel('user').as_('u2'),
    _.as('u2').out().hasLabel('book').dedup().fold().as_('u2_books')) \
    .where('u1', neq('u2')).as_('m') \
    .project('u1', 'u2', 'b1', 'b2') \
    .by(select('u1').id_()) \
    .by(select('u2').id_()) \
    .by(select('u1_books'').as_('n').select('m').select('u2_books'').unfold().where(within('n')).count()) \
    .by(union(select('u1_books''), select('u2_books'')).unfold().dedup().count()) \
    .project('u1', 'u2', 'int', 'un', 'similarity') \
    .by('u1') \
    .by('u2') \
    .by('b1') \
    .by('b2') \
    .by(__.math('b1/b2')).order().by('similarity', Order.desc).limit(LIMIT)

这是我的查询。我找不到加快响应速度的方法。任何帮助表示赞赏。

编辑

    query = g.V().hasId(user).as_('u1').out().aggregate('u1_books').in_().where(neq('u1')).as_('u2')\
    .project('u1', 'u2', 'int', 'un') \
    .by(select('u1').id_()) \
    .by(select('u2').id_()) \
    .by(select('u1_books').as_('n').select('u2').out().where(within('n')).count()) \
    .by(union(select('u1_books'), select('u2').out()).unfold().dedup().count()).toList()

我考虑了您的回答并做出了调整。我现在尝试运行的查询就是这个,甚至更简单。但是,我无法得到回应。它给出了超时错误。

标签: gremlinamazon-neptunegremlin-servergremlinpython

解决方案


我以稍微不同的方式重写了查询 - 在 project() 语句之前提前执行尽可能多的遍历。

正如@Kelvin 在评论中提到的那样,Neptune 中的无界 in() 步骤可能会出现问题,因为它们不绑定到索引。您确实需要将您拥有的任何边缘标签添加到 in() 步骤中以使其表现良好。

这是查询的另一个版本:

g.V(user).as('u1').
    out('edge-label').as('u1_books').
    in('edge-label').where(neq('u1')).as('u2').
    select('u1','u2','u1_books').
        group().by(select('u1','u2')).unfold().
    project('u1','u2','int','un').
        by(select(keys).select('u1')).
        by(select(keys).select('u2')).
        by(select(values).unfold().dedup().count()).
        by(select(keys).select('u1','u2').select(values).
            unfold().in('edge-label').dedup().count())

为了进一步排除故障,我们需要查看此查询的 Gremlin Profile API 的输出,以了解正在执行多少数据和多少索引操作: https ://docs.aws.amazon.com/neptune /latest/userguide/gremlin-profile-api.html


推荐阅读