我有一个包含两种节点的图表:User和Activity。用户可以是彼此的朋友。我想获取用户的新闻源。以下是我的询问:
MATCH (me:User) WHERE me.id = '#{opts[:user_id]}'
MATCH (me)-[:FRIEND*0..1]->(user)-[:CREATE_ACTIVITY]->(activity)
RETURN activity.id AS id,
activity.created_at AS created_at,
activity.activity_type AS activity_type,
{
id: user.id,
name: user.name,
avatar_name: user.avatar_name
} AS user
ORDER BY activity.created_at
LIMIT 10
但是,此查询效率不高,因为它似乎不应用索引并将扫描所有数据。另外,在我在Activity:created_at上添加索引之后,没有任何改变。
有人能提供一种有效的方法来获取新闻源吗?
更新
我正在使用Ruby gem。
我在User和Activity的id字段以及Activity的created_at上都有索引。我期待ORDER BY将使用created_at上的索引,但它没有。
答案 0 :(得分:1)
稍微重写一下查询并使用params:
MATCH (me:User)-[:FRIEND*0..1]->(user)
WHERE me.id = {user_id}
WITH user LIMIT 1000
MATCH user-[:CREATE_ACTIVITY]->(activity)
RETURN activity.id AS id,
activity.created_at AS created_at,
activity.activity_type AS activity_type,
{
id: user.id,
name: user.name,
avatar_name: user.avatar_name
} AS user
ORDER BY activity.created_at
LIMIT 10
帕拉姆可以帮助加快速度,但我怀疑这是问题所在。用户有多少朋友?用户的id
属性上是否有索引?那么created_at
呢?另请注意,WHERE me.id =
与WHERE ID(me) =
不同。第一个检查id
属性,第二个检查内部新ID
答案 1 :(得分:0)
确保您拥有所说的索引(即它们显示在个人资料中),
您可以尝试将查询更改为:
MATCH (me:User) WHERE me.id = '#{opts[:user_id]}'
MATCH (me)-[:FRIEND*0..1]->(user)
WITh distinct user
MATCH (user)-[:CREATE_ACTIVITY]->(activity)
RETURN activity.id AS id,
activity.created_at AS created_at,
activity.activity_type AS activity_type,
{
id: user.id,
name: user.name,
avatar_name: user.avatar_name
} AS user
ORDER BY activity.created_at
LIMIT 10