简体   繁体   English

Neo4j 计数查询

[英]Neo4j count Query

match(m:master_node:Application)-[r]-(k:master_node:Server)-[r1]-(n:master_node) 
where (m.name contains '' and (n:master_node:DeploymentUnit or n:master_node:Schema)) 
return distinct m.name,n.name

Hi,I am trying to get total number of records for the above query.How I change the query using count function to get the record count directly.嗨,我正在尝试获取上述查询的总记录数。如何使用 count 函数更改查询以直接获取记录数。

Thanks in advance提前致谢

The following query uses the aggregating funtion COUNT .以下查询使用聚合函数COUNT Distinct pairs of m.name, n.name values are used as the "grouping keys".不同的m.name, n.name值对用作“分组键”。

MATCH (m:master_node:Application)--(:master_node:Server)--(n:master_node) 
WHERE EXISTS(m.name) AND (n:DeploymentUnit OR n:Schema)
RETURN m.name, n.name, COUNT(*) AS cnt

I assume that m.name contains '' in your query was an attempt to test for the existence of m.name .我假设m.name contains ''在您的查询中m.name contains ''是为了测试m.name的存在。 This query uses the EXISTS() function to test that more efficiently.此查询使用EXISTS()函数来更有效地进行测试。

[UPDATE] [更新]

To determine the number of distinct n and m pairs in the DB (instead of the number of times each pair appears in the DB):要确定 DB 中不同的nm对的数量(而不是每对出现在 DB 中的次数):

MATCH (m:master_node:Application)--(:master_node:Server)--(n:master_node) 
WHERE EXISTS(m.name) AND (n:DeploymentUnit OR n:Schema)
WITH DISTINCT m.name AS n1, n.name AS n2
RETURN COUNT(*) AS cnt

Some things to consider for speeding up the query even further:进一步加快查询速度需要考虑的一些事项:

  1. Remove unnecessary label tests from the MATCH pattern.MATCH模式中删除不必要的标签测试。 For example, can we omit the master_node label test from any nodes?例如,我们可以省略任何节点的master_node标签测试吗? In fact, can we omit all label testing for any nodes without affecting the validity of the result?事实上,我们是否可以在不影响结果有效性的情况下省略对任何节点的所有标签测试? (You will likely need a label on at least one node, though, to avoid scanning all nodes when kicking off the query.) (不过,您可能需要在至少一个节点上添加标签,以避免在开始查询时扫描所有节点。)

  2. Can you add a direction to each relationship (to avoid having to traverse relationships in both directions)?您能否为每个关系添加一个方向(以避免必须在两个方向上遍历关系)?

  3. Specify the relationship types in the MATCH pattern.MATCH模式中指定关系类型。 This will filter out unwanted paths earlier.这将更早地过滤掉不需要的路径。 Once you do so, you may also be able to remove some node labels from the pattern as long as you can still get the same result.一旦你这样做了,你也可以从模式中删除一些节点标签,只要你仍然可以获得相同的结果。

  4. Use the PROFILE clause to evaluate the number of DB hits needed by different Cypher queries.使用PROFILE子句来评估不同 Cypher 查询所需的数据库命中数。

You can find examples of how to use count in the Neo4j docs here您可以在此处的 Neo4j 文档中找到有关如何使用计数的示例

In your case the first example where:在你的情况下,第一个例子是:

count(*)

Is used to return a count of each returned item should work.用于返回每个返回项目应该工作的计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM