php - 带有连接的 MySQL 查询在 Phpmyadmin 中有效,但在 php 中无效
问题描述
我有一个 Mysql (mariaDB) 查询,其中一些连接在 phpmyadmin 终端中运行良好。但是,如果我尝试在 php (pdo) 中执行,数据库会得到巨大的工作量,直到我在nginx
.
如果我从查询中删除连接,它也适用于我的 php 脚本。
所以问题一定出在连接上,但这里有什么问题?
public function exportCSVById($id){
$this->db->query('
SELECT DISTINCT
positions_list.id,
positions_list.order_id,
positions_list.position,
positions_list.comment,
positions_list.result,
positions_list.created,
positions_list.modified,
positions_list.releaseSuS1,
positions_list.releaseSuS2,
positions_list.releaseCuS1,
positions_list.releaseCuS2,
us1.lastname as SuS1,
us2.lastname as SuS2,
orderstates.keynr,
orderstates.content,
companies.id,
companies.company,
users.id,
users.lastname,
ordertype_positions.keynr,
ordertype_positions.p_company,
ordertype_positions.content as orderstate,
repkey.keynr,
repkey.p_company,
repkey.content as repkey,
rk2.keynr,
rk2.p_company,
rk2.content as repkey2
FROM positions_list
JOIN ordertype_positions
ON positions_list.optionindex=ordertype_positions.keynr
AND ordertype_positions.p_company = positions_list.comp_id
JOIN repkey
ON positions_list.keyindex=repkey.keynr
AND repkey.p_company = positions_list.comp_id
JOIN repkey as rk2
ON positions_list.keyindex2=rk2.keynr
AND rk2.p_company = positions_list.comp_id
JOIN companies
ON positions_list.comp_id=companies.id
JOIN users
ON positions_list.uid=users.id
JOIN users as us1
ON positions_list.releaseSuS1=us1.id
JOIN users as us2
ON positions_list.releaseSuS2=us2.id
JOIN orderstates
ON positions_list.repstate=orderstates.keynr
WHERE
positions_list.order_id =:id
ORDER BY
positions_list.created ASC
');
$this->db->bind(':id', $id);
$results = $this->db->resultSet();
return $results;
}
public function resultSet(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
解决方案
PHPMYADMIN 对结果进行了限制。
你只是有一个巨大的结果集。PHP 尝试将其全部加载到内存中:
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
显然,这超出了您当前对 PHP 的内存限制。
解决方案?
分批工作,或开始使用游标:
推荐阅读
- node.js - socket.io 和异步查询有问题
- authentication - 用于日历读取报告的图形 API 访问令牌 无权限
- azure-data-factory - Azure 数据工厂使用 Databricks 中的现有群集
- java - 如何有效地在数组中随机重新插入元素
- date - 将日期压缩为唯一的字母数字字符
- sql - SQL Server 存储过程不返回值
- python - 如何获取单元测试测试名称
- java - 我如何从 Json 对象和数组中获取数据
- unity3d - 如何在 Unity 运行时设置贴图(除了反照率、法线和金属)?
- c - 如何在 C 中处理错误时提供上下文?