简体   繁体   中英

MySQL query with joins works in Phpmyadmin but not in php

I have a Mysql (mariaDB) query with some joins that works fine in phpmyadmin terminal. But if I try to execute in php (pdo) the database get huge workload till I get a bad gateway on nginx .

If I remove the joins from query, it works in my php script also.

So the issue must be with the joins but what is not ok here?

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 puts a limit on the results.

You simply have a HUGE resultset. PHP tries to load it all in memory:

return $this->stmt->fetchAll(PDO::FETCH_OBJ);

Ans apparently that is more than your current memory limit for PHP.

Solution?

Work in batches, or start using cursors:

What is PDO scrollable cursor?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM