MySQL operation buffer usage in PHP
This article mainly introduces the MySQL operation buffer usage in PHP, in the case of the form of a more detailed analysis of the MySQL operation buffer skills, with a certain reference value, the need for friends can refer to the next
This example describes the MySQL operation in PHP using the buffer usage. Share to everyone for your reference. The specific analysis is as follows:
There are three ways to connect PHP with MySQL, Mysql,mysqli,pdo. Regardless of the method used to connect, there is a difference between using buffer and not using buffer.
What do you mean by using buffer and not using buffer?
The client and the MySQL server query operation, when the query operation if the amount of data obtained is large, where is the query result?
There are two places to put: The client's buffer and the server's buffer.
What we're talking about here is the buffer of the client, and if the query results have been retrieved from the server and placed in the client's buffer, we'll call it using buffer. If the buffer is still stored on the server, we say that we are not using buffer (Unbuffer).
What is the difference between using buffer and not using buffer?
Primarily in terms of memory, using buffer increases the memory pressure on the client, and when the returned data results are particularly large, it can occupy a larger process called the client (which is actually a PHP process). Not using buffer nature is more stressful on the server side, which is what it says is the servers that provide the MySQL service.
For specific reference: PHP query MySQL large amount of data memory consumption analysis
How do I set three modes in PHP to use buffer?
MySQL default query is using buffer instead of using buffer, you need to use Mysql_unbuffer_query
Mysqli default query is not to use buffer, you need to set Mysqli_store_result to use buffer
The default Quey of PDO is not to use buffer, you need to set Mysql_attr_use_buffered_query to use buffer
The approximate relevant code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
$dbConfig = Array ( ' Host ' = ' 10.128.11.101 ', ' Port ' = ' 3306 ', ' User ' = ' test ', ' Pass ' = ' test ', ' db ' = ' Test ', ); $sql = ' select * from So_topic_app '; ---------MySQL----------// $db = mysql_connect ($dbConfig [' Host '], $dbConfig [' User '], $dbConfig [' Pass ']; mysql_select_db ($dbConfig [' db '], $db); Mysql_set_charset (' UTF8 ', $db); MySQL uses buffer $res = mysql_query ($sql, $db); $data = Array (); while ($row = Mysql_fetch_row ($res)) { $data [] = $row; } MySQL does not use buffer $res = Mysql_unbuffered_query ($sql, $db); $data = Array (); while ($row = Mysql_fetch_row ($res)) { $data [] = $row; } Mysql_close ($DB); ---------mysqli----------// $db = Mysqli_connect ($dbConfig [' Host '], $dbConfig [' User '], $dbConfig [' Pass '], $dbConfig [' db ']); Mysqli do not use buffer $result = Mysqli_query ($db, $sql); $data = Array (); while ($row = $result->fetch_array ()) { $data [] = $row; } Mysqli using buffer $result = Mysqli_query ($db, $sql, Mysqli_store_result); $data = Array (); while ($row = $result->fetch_array ()) { $data [] = $row; } Mysqli_free_result ($result); Mysqli_close ($DB); ---------PDO----------// $DSN = "mysql:dbname={$dbConfig [' DB ']};host={$dbConfig [' Host ']}"; $pdo = new PDO ($DSN, $dbConfig [' User '], $dbConfig [' Pass ']; PDO does not use buffer $stmt = $pdo->prepare ($sql); $stmt->execute (); $data = Array (); $data = $stmt->fetchall (); PDO uses buffer $pdo->setattribute (Pdo::mysql_attr_use_buffered_query, true); $stmt = $pdo->prepare ($sql); $stmt->execute (); $data = Array (); $data = $stmt->fetchall (); |
Subsequent
Of course, if the amount of data is very large, most people will consider using batches to extract and process data. So the fact that we need to focus on and use MySQL is that there are very few scenes that use buffer or do not use buffer.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/970758.html www.bkjia.com true http://www.bkjia.com/PHPjc/970758.html techarticle mysql operation buffer in PHP usage details This article mainly introduces the MySQL operation buffer usage in PHP, with the example form more detailed analysis of the MySQL operation of the buffer technique, with a certain reference ...