doucan1996 2015-08-24 10:22
浏览 147
已采纳

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

I want to create a time tree for a calendar function - it works but I realize that it is very slow (1 insert per 20 seconds). Maybe anyone has a hint how the code shall be changed to gain more perofmance?

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
  • 写回答

1条回答 默认 最新

  • duanjunao9348 2015-08-24 12:30
    关注

    This is not surprising, your code will execute x distinct http queries for each Cypher statement, which doesn't use all the benefits of the http transactional endpoint. So for each query you have the http latency counting in the query times.

    1. Make an array of all the queries you want to execute and execute them all at once, I don't know how neo4jphp will do it but in NeoClient PHP you can easily do it with a PreparedTransaction object

    2. There are neo4j plugins that will handle the timetree creation/updates for you under the hood, https://github.com/graphaware/neo4j-timetree and there is a NeoclientPHP timetree extension providing you the required methods to work with the neo4j timetree plugin API

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏
  • ¥15 Wanted but not invoked:Actually, there were zero interactions with this moc
  • ¥20 怎么驱动tb6612
  • ¥15 Arcgis 3D效果点要素
  • ¥15 在执行yolo训练脚本的时候,没有报错,在扫描val这一步后就推出执行了
  • ¥15 delphi开发的WEBSERVER改用HTTPS协议
  • ¥15 pic16f877A单片机的计数器proteus仿真失效
  • ¥100 调查 Vitis AI 中验证 .xmodel 量化后的正确性
  • ¥30 codelite全屏时file、setting那一行消失了