简体   繁体   English

Neo4jPhp太慢

[英]Neo4jPhp too slow

Today i have written first basic program for Neo4j from PHP. 今天,我从PHP编写了Neo4j的第一个基本程序。 This was basically done to check out if we could use Neo4j in our new project from PHP by using Neo4jPhp. 这样做基本上是为了检查是否可以通过使用Neo4jPhp在PHP的新项目中使用Neo4j。 https://github.com/jadell/neo4jphp https://github.com/jadell/neo4jphp

here is my code 这是我的代码

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php

include 'neo4jphp.phar';
echo "Hello World!";

// Connecting to the default port 7474 on localhost
$client = new Everyman\Neo4j\Client();
$queryString = 
    "MATCH (n)".
    "RETURN n";
$query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
$result = $query->getResultSet();


foreach ($result as $row) {
    echo $row['n']->getProperty('name') . "\n";
}

?>

</body>
</html>

Now here i am just retrieving all the nodes with their property. 现在,我在这里检索所有具有其属性的节点。 Pretty simple. 很简单

if i run this from graphical console of Neo4j, it takes 86 ms. 如果我从Neo4j的图形控制台运行它,则需要86毫秒。 I have only 200 nodes and almost same property. 我只有200个节点,几乎拥有相同的属性。

match (n)
return n


Returned 50 rows in 86 ms

If i run this from above PHP file, it takes 2-4 seconds in total to dump data in browser. 如果我从上面的PHP文件运行此文件,则总共需要2-4秒在浏览器中转储数据。 Neo4j is running in same machine. Neo4j在同一台计算机上运行。

Please note that i have not done any changes in the configuration of both PHP and Neo4j. 请注意,我没有对PHP和Neo4j的配置进行任何更改。 Everything is default. 一切都是默认的。 Please tell me if this is the expected behaviour of Neo4j with PHP or something is really wrong with my code or configuration. 请告诉我这是否是Neo4j与PHP的预期行为,或者我的代码或配置确实有问题。

Thanks a lot 非常感谢

I have seen your question in the Neo4j Google Group as well and I asked if you could measure in PHP the execution time if instead of using the 我也在Neo4j Google网上论坛中看到了您的问题,我问您是否可以用PHP来衡量执行时间,而不是使用

echo $row['n']->getProperty('name') . echo $ row ['n']-> getProperty('name')。 "\\n";. “\\ n” 个;.

you use 你用

print_r($result); 的print_r($结果);

Let me explain below why. 让我在下面解释原因。 When I started to play around with Neo4j and PHP I had some concerns over the effectiveness of PHP in terms of speed. 当我开始使用Neo4j和PHP时,我对速度方面的PHP有效性有些担忧。 I recreated your issue like so. 我像这样重新创建了您的问题。 First I created 200 random nodes. 首先,我创建了200个随机节点。 Each node has a Label, 10 properties and each properties has a value of 10 characters. 每个节点都有一个Label,10个属性,每个属性的值都是10个字符。 This is te script I used. 这是我使用的脚本。

for ($x=1; $x<=200; $x++)
  {
  $queryString = "CREATE (n:User { name : '".substr(md5(rand()), 0, 10)."' , city : '".substr(md5(rand()), 0, 10)."' , date : '".substr(md5(rand()), 0, 10)."', age : '".substr(md5(rand()), 0, 10)."', country : '".substr(md5(rand()), 0, 10)."', language : '".substr(md5(rand()), 0, 10)."', origin : '".substr(md5(rand()), 0, 10)."', preference : '".substr(md5(rand()), 0, 10)."', color : '".substr(md5(rand()), 0, 10)."', graduate : '".substr(md5(rand()), 0, 10)."'})";
            $query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
            $result = $query->getResultSet();
  } 

Using the foreach loop I got the result like you did 使用foreach循环,我得到了像您一样的结果

foreach ($result as $row) {
    echo $row['n']->getProperty('name') . "\n";
}

and I measured the time executed using this code 我测量了使用此代码执行的时间

$time_start = microtime(true);

$queryString = "MATCH (n) RETURN n";
            $query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
            $result = $query->getResultSet();

foreach ($result as $row) {
    echo $row['n']->getProperty('name') . "\n";
}

$time_end = microtime(true);


$execution_time = ($time_end - $time_start)*1000;

//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' ms';

With 200 nodes I got both on webadmin and the php around 85ms. 有200个节点,我在85毫秒左右的时间都使用了webadmin和php。 The amount of data is not enough to get accurate results so I increased my nodes to 500. Time execution went up to 115ms both the webadmin and the php script. 数据量不足以获取准确的结果,所以我将节点数增加到500。webadmin和php脚本的执行时间都增加到115ms。 Increasing my nodes to 2000 I had execution time of 200ms but no significant differences between webadmin and php. 将节点增加到2000,我的执行时间为200ms,但是webadmin和php之间没有显着差异。 Finally I got my nodes up to 10000. Ok now we have some results. 最终,我的节点数达到了10000。现在我们有了一些结果。 Webadmin returns to me 10000 nodes in 1020ms. Webadmin在1020毫秒内向我返回了10000个节点。 Php is way too slow though. php太慢了。

Total Execution Time: 1635.6329917908 ms 总执行时间:1635.6329917908 ms

I think this is not what I expect. 我认为这不是我期望的。 Instead of using the $row['x'] method I print_r the results and the time increased to 我没有使用$ row ['x']方法,而是使用print_r结果和时间,

Total Execution Time: 2452.4049758911 ms 总执行时间:2452.4049758911 ms

So I think lets not print all the properties on the screen but just return the nodes and the count(n) and see what we have if we print the count of each one which will be a "1". 因此,我认为不要在屏幕上打印所有属性,而只是返回节点和count(n)并查看如果打印每个将为“ 1”的计数所得到的结果。

$queryString = "MATCH (n) RETURN n AS n, count(n) AS x";
            $query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
            $result = $query->getResultSet();
foreach ($result as $row) {
    echo $row['x'];
}

The result of the above code will be something like this. 以上代码的结果将是这样。

1111111111111111111111...... Total Execution Time: 1084.1178894043 ms 1111111111111111111111 ......总执行时间:1084.1178894043 ms

As you can see php and webadmin return 10000 results in the same time (for 10000 nodes I do not think that 60 ms are a major difference) and to conclude my big answer with this: in php and Neo4j we do not loose time to retrieve a large amount of data but we loose a lot of time to render this data on our browser from PHP. 如您所见,php和webadmin同时返回10000个结果(对于10000个节点,我认为60毫秒不是主要区别),并以此得出我的重要答案: 在php和Neo4j中,我们不会浪费时间来检索大量的数据,但是我们花了很多时间从PHP在浏览器上呈现这些数据。

Can you debug and measure what the REST request to neo4j server is actually taking? 您是否可以调试和衡量对neo4j服务器的REST请求实际上正在处理什么? It should be something like 86ms, rest should be in the PHP code? 应该是86ms之类的东西,其余的应该在PHP代码中吗? Also, please use parameters so you don't have the overhead of creating query plans for repeating cypher queries. 另外,请使用参数,以免产生重复进行密码查询的查询计划的开销。

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

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