简体   繁体   中英

How does PHP PDO work internally?

I want to use pdo in my application, but before that I want to understand how internally PDOStatement->fetch and PDOStatement->fetchAll .

For my application, I want to do something like "SELECT * FROM myTable" and insert into csv file and it has around 90000 rows of data.

My question is, if I use PDOStatement->fetch as I am using it here:

// First, prepare the statement, using placeholders
$query = "SELECT * FROM tableName";
$stmt = $this->connection->prepare($query);

// Execute the statement
$stmt->execute();
var_dump($stmt->fetch(PDO::FETCH_ASSOC));

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
{
echo "Hi";
// Export every row to a file
fputcsv($data, $row);
}

Will after every fetch from database, result for that fetch would be store in memory ?

Meaning when I do second fetch, memory would have data of first fetch as well as data for second fetch.

And so if I have 90000 rows of data and if am doing fetch every time than memory is being updated to take new fetch result without removing results from previous fetch and so for the last fetch memory would already have 89999 rows of data.

  1. Is this how PDOStatement::fetch works ?
  2. Performance wise how does this stack up against PDOStatement::fetchAll ?

Update: Something about fetch and fetchAll from memory usage point of view

Just wanted to added some thing to this question as recently found something regarding fetch and fetchAll, hope this would make this question worthwhile for people would visit this question in future to get some understanding on fetch and fetchAll parameters.

fetch does not store information in memory and it works on row to row basis, so it would go through the result set and return row 1, than again would go to the result set and than again return row 2 mind here that it will not return row 1 as well as 2 but would only return row 2, so fetch will not store anything into memory but fetchAll will store details into the memories. So fetch is better option compared to fetchAll if we are dealing with an resultant set of around 100K in size.

PHP generally keeps its results on the server. It all depends on the driver. MySQL can be used in an "unbuffered" mode, but it's a tad tricky to use. fetchAll() on a large result set can cause network flooding, memory exhaustion, etc.

In every case where I need to process more than 1,000 rows, I'm not using PHP. Consider also if your database engine already has a CSV export operation. Many do.

I advice you to use PDO::FETCH_LAZY instead of PDO::FETCH_ASSOC for big data. I used it for export to csv row by row and it works fine. Without any "out of memory" errors.

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