Neo4j - 嵌套的FOREACH查询失败

时间:2014-02-01 08:52:45

标签: datetime foreach neo4j cypher

问题:Neo4j浏览器锁定Executing query...或返回Unknown error。原因是我的查询中的第三个FOREACH,只有FOREACH定义而不是MERGE / CREATE条款。

我尝试制作一个Cypher查询,该查询将整年收集整数并创建年/月/日节点。我试图通过先使用集合/贴图然后嵌套FOREACH子句来减少这些行,这对于创建日期的FOREACH工作正常。

问题似乎来自使用映射的整数,如果我用普通整数替换m.num它将处理好几天,但只有一个月(多次)。

该查询基于Kenny Bastani的查询:
https://gist.github.com/kbastani/8519557

我还将CREATE更改为CREATE UNIQUE,这样可以防止与MERGE类似的重复关系/节点?

我应该以不同的方式编写查询吗?我确实有一个旧版本的查询工作,但后来锁定执行查询,在localhost和grapheneDB上。当初始测试查询(年:1999,2000,2004,2010,2400)在grapheneDB上工作时,它返回:

Added 1755 labels, created 1755 nodes, set 5195 properties, created 1800 relationships, returned 0 rows in 20049 ms

这是合理的时间吗?由于使用GrapheneDB并且它的256mb RAM,它会慢吗?或者通过将Cypher缩减为嵌套查询和集合/映射,我是否使查询效率低下呢?

这是我的问题:

//Date ranges in a month
WITH 
    range(1, 28) as Days_28, 
    range(1, 29) as Days_29, 
    range(1, 30) as Days_30, 
    range(1, 31) as Days_31

//Assign months date ranges
WITH 
    Days_31 as January, 
    Days_28 as February, 
    Days_29 as Leap_February, 
    Days_31 as March, 
    Days_30 as April, 
    Days_31 as May, 
    Days_30 as June,
    Days_31 as July, 
    Days_31 as August, 
    Days_30 as September, 
    Days_31 as October, 
    Days_30 as November, 
    Days_31 as December

//Mapping months to days keys - num key could be avoided by changing FOREACH to: 'FOREACH(m IN range(1,length(year.months)) |' or 'FOREACH(m IN range(1, 12) |'
WITH [
    {num: 1, days: January}, 
    {num: 2, days: February}, 
    {num: 3, days: March}, 
    {num: 4, days: April}, 
    {num: 5, days: May}, 
    {num: 6, days: June}, 
    {num: 7, days: July}, 
    {num: 8, days: August}, 
    {num: 9, days: September}, 
    {num: 10, days: October}, 
    {num: 11, days: November}, 
    {num: 12, days: December}
    ] as regular_year, Leap_February

//Create leap year
WITH [regular_year[0] , {num: 2, days: Leap_February} , regular_year[2..11]] AS leap_year, regular_year

//Years to create are stored in years collection - anyway to move this to the top without having to add it to the end of every WITH clause prior?
WITH [1996] as years,leap_year,regular_year
//Check if year is a leap year, if so map to leap_year, if not map regular_year
WITH [year IN years | CASE WHEN (year%4=0 AND NOT year%100=0) THEN {year:year, months:leap_year} WHEN (year%4=0 AND year%100=0 AND year%400=0) THEN {year:year, months:leap_year} ELSE {year:year, months:regular_year} END] AS yearMap

//Create nodes/relationships for years/months/days
FOREACH(year IN yearMap |
    MERGE (thisYear: Year {year: year.year})

    FOREACH(m IN year.months |
        MERGE (thisMonth :Month { month: m.num, year: year.year })
        CREATE UNIQUE (thisYear)-[:HAS_MONTH]->(thisMonth)

        MERGE (firstDay :Day { day: 1, month: m.num, year: year.year })
        CREATE UNIQUE (thisMonth)-[:FIRST]->(firstDay)

        //This FOREACH line is causing a problem, if replace m.num with an integer value it works, but then only processes that month
        FOREACH (d IN TAIL((year.months[m.num]).days) |
            MERGE (thisDay: Day { day: d, month: m.num, year: year.year })
            MERGE (lastDay: Day { day: d - 1, month: m.num, year: year.year })
            CREATE UNIQUE(lastDay)-[:NEXT]->(thisDay)
        )

    MERGE (lastDay: Day { day: last((year.months[m.num]).days), month: m.num, year: year.year })
    CREATE UNIQUE (thisMonth)-[:LAST]->(lastDay)
    )
)

更新:我设法发现了一些错误。从我的第一个版本的查询开始,我改变了创建leap_year集合的方式。查询工作正常的几年,它刚刚被闰年打破。这是因为误解了regular_year [2..11],我期待它切片[2] - [11],但是第二个整数是一个上限?将其更改为12可添加所有剩余月份。该方法还使leap_year的长度为3而不是12,通过将线路更改为此来解决此问题:
WITH [regular_year[0], {num: 2, days: Leap_February}] + filter(month in regular_year WHERE month.num>2) AS leap_year, regular_year

我收到的第二个问题是第三个FOREACH我正在使用(year.months[m.num]).days我应该使用m.num-1来正确访问集合,并再次使用MERGE }。然而,查询仍然在闰年中被打破..

更新2: 我认为我在EDIT中列出的修复方法已经完成了。在我的localhostDB中,除了我正在测试的那一年(2000年)之外,所有年份似乎都很好。我检查了类似的闰年(2400)并且有效。

我尝试在grapheneDB中对数据库进行查询并得到以下错误:
The pattern (thisMonth)-[1469:首先]->(firstDay) produced multiple possible paths, and that is not allowed

它所指的代码片段是:

FOREACH(m IN year.months |
 MERGE (thisMonth:Month { month: m.num, year: year.year })
 CREATE UNIQUE (thisYear)-[:HAS_MONTH]->(thisMonth)

 MERGE (firstDay:Day { day: 1, month: m.num, year: year.year })
 CREATE UNIQUE (thisMonth)-[:FIRST]->(firstDay)

我一直在grapheneDB中的数据库中进行此查询,创建一个新的数据库并使用查询一切正常,甚至是2000年。花了大约4000毫秒,然后我尝试了常规/闰年的混合查询共计6年。它无法完成查询,但存在大量节点。在一年(现有或不存在)上再次使用查询失败。我的查询必须以某种方式打破数据库?

1 个答案:

答案 0 :(得分:1)

我跟graphenedb家伙谈过:

一个免费的沙箱实例存在内存,CPU和执行时的某些资源限制。如果其中一个数据库超出了进一步的限制但无法恢复,则会重新启动。

他们还说,如果你遇到graphenedb问题,你应该联系他们的支持。 或者,如果您使用heroku附加组件,请通过heroku支持系统。