php-使用循环优化Cypher查询以构建时间树

我想为日历功能创建一个时间树-它可以工作,但是我意识到它非常慢(每20秒插入1次).也许有人暗示如何更改代码以获得更多性能?

for ($calYear=2012;$calYear<2016; $calYear++)
{
        $paramsYear =array(
        "pYear" => $calYear, 
        "UUID" => uniqid()            
    );


$queryString = '
    MERGE (y:calTime:calYear {name: {pYear}}) 
    SET 
    y.uuid= {UUID},
    y.created="'.time().'",
    y.active="Y"    
    return y;
    ';

    $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$params);
    $result = $query->getResultSet();    

    for ($calMonth=1;$calMonth<13; $calMonth++)
    {
                $paramsMonth =array(
                "pYear" => $calYear, 
                "pMonth" => $calMonth, 
                "UUID" => uniqid()            
            );            
        $queryString = '
            MATCH (y:calTime:calYear {name: {pYear}})
            MERGE (m:calTime:calMonth {name: {pMonth}) -[:IS_MONTH_OF]->(y) 
            SET 
            m.uuid= {UUID},
            m.created="'.time().'",
            m.active="Y"    
            return m;
            ';

            $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$paramsMonth);
            $result = $query->getResultSet();    

            $numberOfDays = date("t",mktime(0, 0, 0, $calMonth, 1, $calYear));

            for ($calDay=1;$calDay<=$numberOfDays; $calDay++)
            {
                    $paramsDay =array(
                    "pYear" => $calYear, 
                    "pMonth" => $calMonth, 
                    "pDay" => $calDay, 
                    "UUID" => uniqid()            
                );  

                $queryString = '
                    MATCH (y:calTime:calYear {name: {pYear}) -- (m:calTime:calMonth {name: {pMonth})
                    MERGE (d:calTime:calDay {name: {pDay}) -[:IS_DAY_OF]->(m)
                    SET 
                    d.uuid= {UUID},
                    d.created="'.time().'",
                    d.active="Y" 
                    return d;
                    ';

                    $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$paramsDay);
                    $result = $query->getResultSet();                   

                    for ($calHour=1;$calHour<=24; $calHour++)
                    {
                    $paramsHour =array(
                    "pYear" => $calYear, 
                    "pMonth" => $calMonth, 
                    "pDay" => $calDay, 
                    "pHour" => $calHour, 
                    "UUID" => uniqid()            
                );  

                        $queryString = '
                            MATCH (y:calTime:calYear {name: {pYear}) -- (m:calTime:calMonth {name: {pMonth}) -- (d:calTime:calDay {name: {pDay}) 
                            MERGE (h:calTime:calHour {name: {pHour}) -[:IS_HOUR_OF]->(d)
                            SET 
                            h.uuid= {UUID},
                            h.created="'.time().'",
                            h.active="Y"  
                            return h;
                            ';

                            $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$paramsHour);
                            $result = $query->getResultSet();

                    } // End Hour

            } // End Day
    } // End Month                
} // End Year

解决方法:

这不足为奇,您的代码将为每个Cypher语句执行x个不同的http查询,而这并没有利用http事务端点的所有好处.因此,对于每个查询,您都会将http延迟计入查询时间.

>将要执行的所有查询组成一个数组,然后一次执行所有查询,我不知道neo4jphp将如何执行,但是在NeoClient PHP中,您可以使用PreparedTransaction对象轻松地执行它
>有一些neo4j插件可以为您处理时间树的创建/更新,https://github.com/graphaware/neo4j-timetree和一个NeoclientPHP timetree extension为您提供了与neo4j时间树插件API配合使用的必要方法

上一篇:java-Neo4j:正确测试螺栓驱动程序的单元


下一篇:java-Neo4j密码计算并显示两个给定节点之间的所有关系