neo4j improving cypher query performance

别来无恙 提交于 2020-01-05 10:16:39

问题


I have items graph db. each item is connected to multiple properties, which can be shared by multiple items. I add a search node which is defined by few properties.

So I have (search_node) connected to multiple (properties_nodes) connected to multiple (items_nodes)

Now I would like to get a list of items who answer this search by {x} properties or more. Ordered by number of matching properties.

start se=node:node_auto_index(name = {name}), pr = node:node_auto_index(type="item_property")
MATCH p=(se) -[rt:SEARCH]- > (pr)<-[r]-(item)
WHERE Has(item.type) and (item.type = "item")
WITH item, collect(distinct pr.name) as rs
where length(rs) > {x}
RETURN item.name as item_name, length(rs) as matching_properties
ORDER BY  matching_properties desc 

The performance issue for me, is that it will search for all matching items, even those who match for only one property, and then remove all the items who are matching less than {x}. If {x} is higher than 1, it's a big waste.

How can I "MATCH" only by {x} matching properties?

Created a matching sample : http://console.neo4j.org/?id=adrgsh

(neo4j version 1.9.2)


回答1:


IMHO to be able to match only items with > x properties, you still need to find all those items that have properties in common with the search node. Post that, you can filter out the matches with < x properties in common. What kind of numbers are you getting? In your worst case, how many items were matched and how many were discarded?

You can simplify your query (matched results on the console but it could be missing some details that are not specified in this question)

START se=node(11) 
MATCH (se)-[:SEARCH]- >(pr)<-[:HAS]-(item) 
WHERE HAS (item.type) AND (item.type = "item") 
WITH count(pr) AS matching_properties, item 
WHERE matching_properties>1 
RETURN item.name AS item_name, matching_properties 
ORDER BY matching_properties DESC


来源:https://stackoverflow.com/questions/20935393/neo4j-improving-cypher-query-performance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!