我需要对来自多个性能密集型查询的结果进行分页(极限+偏移量)。
首先,我使用python的生成器模拟分页
first = 100
# Offset
skip = 50
cursor = 0
# Generator is returned by neo4j so we don't have a significant performance impact
q1_results = tx.run(.....)
q2_results = tx.run(.....)
for result in q1_results:
if cursor < first:
yield result
cursor += 1
for result in q2_results:
if cursor < first:
yield result
cursor += 1但是,这里的问题是执行偏移量:为了以编程的方式实现它,我必须再次迭代第一个结果,并这样做:
first = 100
# Offset
skip = 50
cursor = 0
skip_cursor = 0
# Generator is returned by neo4j so we don't have a significant performance impact
q1_results = tx.run(.....)
q2_results = tx.run(.....)
for result in q1_results:
if cursor < first & skip_cursor > skip:
yield result
cursor += 1
else:
skip_cursor += 1
for result in q2_results:
if cursor < first & skip_cursor > skip:
yield result
cursor += 1
else:
skip_cursor += 1 然后,我尝试将查询合并成一个大查询,但是它需要使用聚合函数(如collect和distinct),因此它对性能产生了巨大的影响,查询变得非常缓慢。
我想知道我是否遗漏了什么,在这种情况下是否有实现分页的正确方法。
发布于 2019-01-22 11:09:48
目前,正确的方法是在您的Cypher查询中使用SKIP和LIMIT。底层协议没有只返回部分查询结果的机制,因此即使使用代码,仍然可以生成、发送和缓冲整个结果集。
我们的路线图上有一个项目要引入完整的流控制,以及一个反应性API。这将启用对增量传递记录的完全堆栈支持,并提供跳过和取消流的选项。但这是一个复杂的变化,所以最早要到今年年底才会到来。在此之前,你最好的选择是使用Cypher关键字。
https://stackoverflow.com/questions/54280558
复制相似问题