PHP in MySQL operation buffer usage explained in detail, mysqlbuffer_php tutorial

Source: Internet
Author: User

PHP mysql operation buffer usage in detail, mysqlbuffer


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:

<?php$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 [' Hos T '], $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/970854.html www.bkjia.com true http://www.bkjia.com/PHPjc/970854.html techarticle in PHP, MySQL operation Buffer usage is explained in detail, Mysqlbuffer This example describes the MySQL operation in PHP using the buffer usage. Share to everyone for your reference. The specific analysis is as follows: PHP connection to MySQL is three ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.