gpt4 book ai didi

php - PDO 内存耗尽

转载 作者:IT老高 更新时间:2023-10-28 23:47:54 33 4
gpt4 key购买 nike

这是一个常见的问题,但我没有选择这样编码只是为了在 Excel 文件中获得适当的标题和正文

这里是如何开始的

当请求打印时,我首先开始查询以获取数据库中的标题

SELECT instruments.in_id, instrument_parameters.ip_id,
CASE WHEN gv_x_ipid = -1 THEN 'datetime' ELSE '' END xlabel,
CASE WHEN ip_label LIKE '%Reservoir%' THEN 0 ELSE in_order END legendIndex,
CASE WHEN in_name = 'General' THEN ip_label ELSE in_name END ylabel
FROM graph_plot
LEFT JOIN attributes gptype ON gp_type = gptype.at_id
LEFT JOIN graph_value ON gp_id = gv_gpid
LEFT JOIN instrument_parameters ON gv_y_ipid = ip_id
LEFT JOIN attributes pmunit ON ip_unit = pmunit.at_id
LEFT JOIN instrument_reading yvalue ON gv_y_ipid = iv_ipid
LEFT JOIN instruments ON iv_inid = in_id
WHERE gp_diid = :di_id AND
gp_type = :rpt_type AND
iv_status = 'Y' AND
iv_inid in (".implode(",", $coll->inid).") AND
gv_y_ipid in (".implode(",", $coll->ipid).")
GROUP BY ylabel
ORDER BY legendIndex

这将产生许多标题,我将使其成为这样

日期 |仪器1 |仪器2 |仪器3

Instrument? 将根据上面的查询是动态的。我将其存储在新变量中。但是保存数据库结果的原始变量保持不变。

稍后,使用相同的参数,:di_id:rpt_type,还有另一个附加参数,startDtendDt 进行另一个查询只是为了返回数据库中可用日期的长列表。这基于 startDtendDt

$sql2 = "SELECT iv_reading FROM instrument_reading WHERE iv_inid = :inid AND iv_ipid = :ipid AND iv_date = :dt AND iv_status = 'Y'";    

当它完成获取日期时,我会像这样进行两个循环

foreach ($dates as $key => $dt) {       
foreach ($resp as $InstNo => $InstRow) {
try {
$stmt2->execute(array(':dt' => $dt, ':inid' => $InstRow->in_id, ':ipid' => $InstRow->ip_id));
$rowDb = $stmt2->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);
} catch(PDOException $e) {
echo '{"error":{"text":"'. $e->getMessage() .'"}}';
}
}
}

首先,它开始循环日期,其次它开始循环标题(基于在获取日期之前进行的查询)。我的问题一直卡在这里

$stmt2->execute(array(':dt' => $dt, ':inid' => $InstRow->in_id, ':ipid' => $InstRow->ip_id));

你怎么看?有没有更好的处理方法?

为了您的信息,我使用 Slim 和 PHPExcel。 PHPExcel 可能有内存问题,我正在考虑切换到 Spout但文件仍然是关于基本的东西。

最佳答案

在您的 SQL 中,您可以考虑使用 limit clause减轻内存负载如下:

$handle = fopen("file.csv", "wb");
$statement = "
SELECT instruments.in_id, instrument_parameters.ip_id,
CASE WHEN gv_x_ipid = -1 THEN 'datetime' ELSE '' END xlabel,
CASE WHEN ip_label LIKE '%Reservoir%' THEN 0 ELSE in_order END legendIndex,
CASE WHEN in_name = 'General' THEN ip_label ELSE in_name END ylabel
FROM graph_plot
LEFT JOIN attributes gptype ON gp_type = gptype.at_id
LEFT JOIN graph_value ON gp_id = gv_gpid
LEFT JOIN instrument_parameters ON gv_y_ipid = ip_id
LEFT JOIN attributes pmunit ON ip_unit = pmunit.at_id
LEFT JOIN instrument_reading yvalue ON gv_y_ipid = iv_ipid
LEFT JOIN instruments ON iv_inid = in_id
WHERE gp_diid = :di_id
AND gp_type = :rpt_type
AND iv_status = 'Y'
AND iv_inid in (".implode(",", $coll->inid).")
AND gv_y_ipid in (".implode(",", $coll->ipid).")
GROUP BY ylabel
ORDER BY legendIndex
LIMIT 250
";
$prep = $dbh->prepare($statement);
for ($i = 0; $prep -> rowCount < 250; $i+= 250) {
fputcsv(prep->fetchAll());
$prep = $dbh->prepare($statement.' OFFSET'.$i);
}
fclose($handle);

或者,您可以使用 system并调用SELECT INTO ,设置权限(如有必要)和 Bob 是你的叔叔。

关于php - PDO 内存耗尽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30723673/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com