Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PHP PDO work internally?

Tags:

php

pdo

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.

like image 601
Rachel Avatar asked Mar 26 '10 21:03

Rachel


1 Answers

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.

like image 191
pestilence669 Avatar answered Nov 17 '22 21:11

pestilence669