我正在尝试在单个查询中创建多个Users节点。它们还在同一查询中与朋友关系链接。当我查询查询时,我看到对于每个属性,我设置了一个关系,进行数据库调用。当每个关系有很多关系和属性时,这会使查询变得昂贵。如何降低此查询的成本?
查询如下:
PROFILE MERGE (user: User {myId: "1"}) WITH user
UNWIND [
{myId: "2",userLastName: "2",userAddressId: "1",hasImage: false,randomId: 0},
{myId: "3",userLastName: "3",userAddressId: "3",hasImage: false,randomId: 0},
{myId: "4",userLastName: "4",userAddressId: "1",hasImage: false,randomId: 0},
{myId: "5",userLastName: "5",userAddressId: "1",hasImage: false,randomId: 0},
{myId: "6",userLastName: "6",userAddressId: "1",hasImage: false,randomId: 0},
{myId: "7",userLastName: "7",userAddressId: "1",hasImage: false,randomId: 0},
{myId: "8",userLastName: "8",userAddressId: "1",hasImage: false,randomId: 0},
{myId: "9",userLastName: "9",userAddressId: "1",hasImage: false,randomId: 0}
] as row
MERGE (friend: User {myId: row.myId}) ON CREATE SET friend.name = row.userLastName
MERGE (user)-[c:KNOWS]->(friend) ON CREATE SET c = row
如何避免对此查询进行如此多的数据库调用?
注意:已在db上设置CREATE CONSTRAINT ON (user:User) ASSERT user.myId IS UNIQUE
。
编辑:
按照this video中的说明,按照@ChristopheWillemsen的说明操作后,我机器上的执行时间从500 + ms下降到约40ms,100到500条记录。
答案 0 :(得分:0)
您要求数据库在关系上创建一些属性,因此有一些数据库命中是正常的。
如果你想减少DB的工作量,你可以在ON CREATE SET c = row
上显示你想要做的所有SET,如下所示:
MERGE (user)-[c:KNOWS]->(friend)
ON CREATE SET c.myId = row.myId,
c.userLastName=row.userLastName,
c.userAddressId=row.userAddressId,
c.hasImage=row.hasImage,
c.randomId=row.randomId