我正在进行公交时刻表搜索。我创建了一些简单的数据进行测试:10辆公交车,每辆公交车有2-5个时刻表,每个时刻表有一个位置关系如下:
bus-[:SCHEDULED]->(schedule)-[scheduleLocation :ORIGIN|TRANSIT|DESTINATION]->location
我想在指定的时间范围内在指定的位置搜索星期一的公交车,我想知道为什么需要很长时间来运行查询(超过10秒)。
以下是查询:
MATCH bus-[:SCHEDULED]->(schedule)-[scheduleLocation :ORIGIN|TRANSIT|DESTINATION]->location
where location.code! = 'KING' AND
(
(bus.runsOnMonday = true AND ((specifiedScheduleLocation.arrivalTime! >='+00:00:16:00' OR specifiedScheduleLocation.departTime! >='+00:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+00:14:42:00' OR specifiedScheduleLocation.departTime! <='+00:14:42:00')))
OR (bus.runsOnTuesday = true AND ((specifiedScheduleLocation.arrivalTime! >='+06:00:16:00' OR specifiedScheduleLocation.departTime! >='+06:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+06:14:41:00' OR specifiedScheduleLocation.departTime! <='+06:14:41:00')))
OR (bus.runsOnWednesday = true AND ((specifiedScheduleLocation.arrivalTime! >='+05:00:16:00' OR specifiedScheduleLocation.departTime! >='+05:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+05:14:41:00' OR specifiedScheduleLocation.departTime! <='+05:14:41:00')))
OR (bus.runsOnThursday = true AND ((specifiedScheduleLocation.arrivalTime! >='+04:00:16:00' OR specifiedScheduleLocation.departTime! >='+04:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+04:14:41:00' OR specifiedScheduleLocation.departTime! <='+04:14:41:00')))
OR (bus.runsOnFriday = true AND ((specifiedScheduleLocation.arrivalTime! >='+03:00:16:00' OR specifiedScheduleLocation.departTime! >='+03:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+03:14:41:00' OR specifiedScheduleLocation.departTime! <='+03:14:41:00')))
OR (bus.runsOnSaturday = true AND ((specifiedScheduleLocation.arrivalTime! >='+02:00:16:00' OR specifiedScheduleLocation.departTime! >='+02:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+02:14:41:00' OR specifiedScheduleLocation.departTime! <='+02:14:41:00')))
OR (bus.runsOnSunday = true AND ((specifiedScheduleLocation.arrivalTime! >='+01:00:16:00' OR specifiedScheduleLocation.departTime! >='+01:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+01:14:41:00' OR specifiedScheduleLocation.departTime! <='+01:14:41:00')))
)
return distinct bus,specifiedScheduleLocation
答案 0 :(得分:0)
AFAIK查询where where子句基于属性的速度很慢,因为Neo4j必须加载所有匹配的节点/关系,然后扫描其属性以过滤它们。
为了获得更好的结果,您可能希望将属性移动到节点和关系中,并尝试仅通过基于关系路径和节点标签过滤路径来查找结果。
也许最明显的例子是使用时间树而不是时间戳属性(link)。
如果您不需要管理特定日期,即星期几和时间,则可以为一周中的每一天设置24小时时间树。
答案 1 :(得分:0)
地点有标签吗?如果没有,请给它贴上标签Location
,然后运行
CREATE INDEX ON :Location(code)
等待几秒钟/分钟(取决于数据库的大小)。
然后将查询更改为:
MATCH bus-[:SCHEDULED]->(schedule)-[scheduleLocation :ORIGIN|TRANSIT|DESTINATION]->(location:Location {code:'KING'})
where (
(bus.runsOnMonday = true AND ((specifiedScheduleLocation.arrivalTime! >='+00:00:16:00' OR specifiedScheduleLocation.departTime! >='+00:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+00:14:42:00' OR specifiedScheduleLocation.departTime! <='+00:14:42:00')))
OR (bus.runsOnTuesday = true AND ((specifiedScheduleLocation.arrivalTime! >='+06:00:16:00' OR specifiedScheduleLocation.departTime! >='+06:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+06:14:41:00' OR specifiedScheduleLocation.departTime! <='+06:14:41:00')))
OR (bus.runsOnWednesday = true AND ((specifiedScheduleLocation.arrivalTime! >='+05:00:16:00' OR specifiedScheduleLocation.departTime! >='+05:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+05:14:41:00' OR specifiedScheduleLocation.departTime! <='+05:14:41:00')))
OR (bus.runsOnThursday = true AND ((specifiedScheduleLocation.arrivalTime! >='+04:00:16:00' OR specifiedScheduleLocation.departTime! >='+04:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+04:14:41:00' OR specifiedScheduleLocation.departTime! <='+04:14:41:00')))
OR (bus.runsOnFriday = true AND ((specifiedScheduleLocation.arrivalTime! >='+03:00:16:00' OR specifiedScheduleLocation.departTime! >='+03:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+03:14:41:00' OR specifiedScheduleLocation.departTime! <='+03:14:41:00')))
OR (bus.runsOnSaturday = true AND ((specifiedScheduleLocation.arrivalTime! >='+02:00:16:00' OR specifiedScheduleLocation.departTime! >='+02:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+02:14:41:00' OR specifiedScheduleLocation.departTime! <='+02:14:41:00')))
OR (bus.runsOnSunday = true AND ((specifiedScheduleLocation.arrivalTime! >='+01:00:16:00' OR specifiedScheduleLocation.departTime! >='+01:00:16:00') AND (specifiedScheduleLocation.arrivalTime! <='+01:14:41:00' OR specifiedScheduleLocation.departTime! <='+01:14:41:00')))
)
return distinct bus,specifiedScheduleLocation
最后,如果有帮助,请告诉我们!它应该,因为您当前的查询扫描整个图形 - 它没有固定的具体节点。
干杯