为什么我会得到“笛卡尔积"?警告? [英] Why do I get a "Cartesian Product" warning?

查看:948
本文介绍了为什么我会得到“笛卡尔积"?警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在尝试理解为什么为什么针对neo4j中的某种查询而不是针对另一种格式的某种格式收到笛卡尔乘积警告.这就是我设置数据库的方式:

I am still trying to understand why I get a cartesian product warning for a certain format for a query in neo4j and not for another. This is how I set up my database:

CREATE (q:Form {version: "1.0"})
CREATE (q:Question {text: "Sector de la empresa", active: true})

然后我尝试了以下查询:

I then tried the following query:

MATCH
(f:Form {version: "1.0"}),
(q:Question {text: "Sector de la empresa"})
CREATE (f)-[:asks]->(q)
RETURN f, q

但是,我收到以下警告:

However, I get the following warning:

This query builds a cartesian product between disconnected patterns.
If a part of a query contains multiple disconnected patterns,
this will build a cartesian product between all those parts.
This may produce a large amount of data and slow down query processing.
While occasionally intended, it may often be possible to reformulate the
query that avoids the use of this cross product, perhaps by adding a
relationship between the different parts or by using OPTIONAL MATCH
(identifier is: (q))

当我使用以下查询时,它不会给我这个警告:

When I use the following query, it does not give me this warning:

MATCH (f:Form {version: "1.0"})
WITH f
(q:Question {text: "Sector de la empresa"})
CREATE (f)-[:asks]->(q)
RETURN f, q

当我使用此查询时也是如此:

nor when I use this query:

MATCH (f:Form {version: "1.0"})
MATCH (q:Question {text: "Sector de la empresa"})
CREATE (f)-[:asks]->(q)
RETURN f, q

我将以下这篇文章用作资源,但仍然不能完全回答我的问题:

I used this following article as a resource, but it still didn't fully answer my question: Why does neo4j warn: "This query builds a cartesian product between disconnected patterns"?

为什么对于某些查询格式而不是其他查询格式得到笛卡尔乘积?另外,我不完全了解笛卡尔乘积警告是什么.

Why do I get a cartesian product for some formats of a query and not others? Also, I do not fully understand what a cartesian product warning is.

推荐答案

如果MATCH使用两个不同的标签,但它们之间没有任何关系,则将收到此警告.原因是因为如果您这样做:

If you are MATCHing on two different labels without any relationships between them, then you'll get this warning. The reason is because if you do:

MATCH (a:Foo), (b:Bar)

Neo4j的工作是找到这两个节点的所有可能组合.因此,对于a的第一个匹配项,它将为b的每个匹配项返回一行,对于a的第二个匹配项,将再次为b的每个匹配项返回一行,依此类推.因此,您的结果中将获得(number of Foo nodes) x (number of Bar nodes)个总行.随着数据库的增长,这确实会降低性能.

It's Neo4j's job to find every possible combination of those two nodes. So for the first match of a it will return a row for every match of b, for the second match of a it will again return a row for every match of b, and so on. So you'll get (number of Foo nodes) x (number of Bar nodes) total rows in your result. As your database grows this is really bad for performance.

我可以看到您正在过滤version(对于Form)和text(对于Question),这将有所帮助.甚至可能只给您一个Form节点和一个Question节点.因此,只要您在Form(version)Question(text)上都有索引,查询就应该很快. Neo4j不能告诉(或者至少目前没有实现告诉)要返回多少行,因此它发出警告说您的查询可能很慢.

I can see that you're filtering on version for Form and text for Question, so that would help. That may even give you just one Form node and one Question node. So as long as you have an index on the Form(version) and Question(text) the query should be quite quick. Neo4j can't tell (or at least, isn't currently implemented to be able to tell) how many rows are going to be returned, so it gives a warning saying that your query could be potentially slow.

这篇关于为什么我会得到“笛卡尔积"?警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆