douyou1857 2014-01-02 22:53
浏览 31
已采纳

内部循环内部循环达到内存限制

Disclaimer: I am aware that I have in my code deprecated mysql functions. That is on my todo list.

I have a MySql select giving me seasons for diferent items in a house booking system.

Kind of:

Low season: 2010-01-01, 2010-03-01, 100 //meaning start,end,price

This comes in my first sql:

while($season_row=mysql_fetch_assoc($season_res)){
    $seasonsArray[$season_row['id_item']][] = array(
        $season_row['season_start'], 
        $season_row['season_end'],
        $season_row['daily_price']
    );
}

The dates are defined here (arriving to the function as YYYY-mm-dd):

function seasonPrice($from,$to,$requested_item){

    $start = round(strtotime($from)/86400)*86400; // like 2008-01-01
    $end = round(strtotime($to)/86400)*86400;     // to 2015-01-01

    $formattedStart = date('Y-m-d', $start);
    $formattedEnd   = date('Y-m-d', $end);

Now I need to loop between 2 dates, between the items of the $seasonsArray and then check the price of that item in that specific day.

I did this with:

foreach($seasonsArray as $item=>$value){            
    for( $thisDay = $start; $thisDay < $end; $thisDay = $thisDay + 86400){

        foreach($value as $innerValue){     
            $season_start = roundToSeconds($innerValue[0]);
            $season_end = roundToSeconds($innerValue[1]);
            if($thisDay >= $season_start && $thisDay <= $season_end) {  
                $foundPrice[] = round($innerValue[2]);
            }
        }

        $thisSerie[] = array($thisDay * 1000, isset($foundPrice) ? $foundPrice[0] : 0);

        // security check to avoid double assigned seasons to same day
        if(count($foundPrice) > 1){ die('There is double bookings in item: '.$item);}   

        unset($foundPrice);
    }
    $seasonPrices[] = array(
        'data'=> $thisSerie,
        'label'=> 'House ID: '.$item, 
    );  
}

But I get: Fatal error: Allowed memory size of 100663296 bytes exhausted

Any suggestion on where my code can be improved to not need so much memory? Or is there a bug and I don't see it?

  • 写回答

2条回答 默认 最新

  • dsbifvcxr458755493 2014-01-03 00:23
    关注

    I'd generate a range of days and join against your seasons table, and use a single query to get the desired resulset, e.g.:

    SELECT dates.Date,
           coalesce(s.price, 0) AS price
    FROM
      (SELECT a.Date
       FROM
         ( SELECT curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Date, '0' AS price
          FROM
            (SELECT 0 AS a
             UNION ALL SELECT 1
             UNION ALL SELECT 2
             UNION ALL SELECT 3
             UNION ALL SELECT 4
             UNION ALL SELECT 5
             UNION ALL SELECT 6
             UNION ALL SELECT 7
             UNION ALL SELECT 8
             UNION ALL SELECT 9) AS a
          CROSS JOIN
            (SELECT 0 AS a
             UNION ALL SELECT 1
             UNION ALL SELECT 2
             UNION ALL SELECT 3
             UNION ALL SELECT 4
             UNION ALL SELECT 5
             UNION ALL SELECT 6
             UNION ALL SELECT 7
             UNION ALL SELECT 8
             UNION ALL SELECT 9) AS b
          CROSS JOIN
            (SELECT 0 AS a
             UNION ALL SELECT 1
             UNION ALL SELECT 2
             UNION ALL SELECT 3
             UNION ALL SELECT 4
             UNION ALL SELECT 5
             UNION ALL SELECT 6
             UNION ALL SELECT 7
             UNION ALL SELECT 8
             UNION ALL SELECT 9) AS c) a
       WHERE a.Date BETWEEN '$from' AND '$to'
       ORDER BY a.Date) dates
    LEFT JOIN seasons s ON dates.Date BETWEEN s.start AND s.END
    

    The complicated inner query avoids the creation of a temp table (taken from generate days from date range) and works for up to 1000 days, but creating a temp table would be fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向