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

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

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法